track_event() mechanism for analytics and plugins

* Closes #2240
* Documentation for event plugin hooks, refs #2240
* Include example track_event plugin in docs, refs #2240
* Tests for track_event() and register_events() hooks, refs #2240
* Initial documentation for core events, refs #2240
* Internals documentation for datasette.track_event()
This commit is contained in:
Simon Willison 2024-01-31 15:21:40 -08:00 committed by GitHub
commit bcc4f6bf1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 614 additions and 10 deletions

View file

@ -593,6 +593,26 @@ Using either of these pattern will result in the in-memory database being served
This removes a database that has been previously added. ``name=`` is the unique name of that database.
.. _datasette_track_event:
await .track_event(event)
-------------------------
``event`` - ``Event``
An instance of a subclass of ``datasette.events.Event``.
Plugins can call this to track events, using classes they have previously registered. See :ref:`plugin_event_tracking` for details.
The event will then be passed to all plugins that have registered to receive events using the :ref:`plugin_hook_track_event` hook.
Example usage, assuming the plugin has previously registered the ``BanUserEvent`` class:
.. code-block:: python
await datasette.track_event(
BanUserEvent(user={"id": 1, "username": "cleverbot"})
)
.. _datasette_sign:
.sign(value, namespace="default")