Skip to content

Commit

Permalink
Include example track_event plugin in docs, refs #2240
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent 7e95feb commit 56d50a3
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,33 @@ The ``event`` object will always have the following properties:
- ``name``: a string representing the name of the event, for example ``logout`` or ``create-table``.
- ``actor``: a dictionary representing the actor that triggered the event, or ``None`` if the event was not triggered by an actor.

Other properties on the event will be available depending on the type of event. TODO: Link to documentation of these.
Other properties on the event will be available depending on the type of event. You can also access those as a dictionary using ``event.properties()``.

**TODO: Link to documentation of default core events**

This example plugin logs details of all events to standard error:

.. code-block:: python
from datasette import hookimpl
import json
import sys
@hookimpl
def track_event(event):
name = event.name
actor = event.actor
properties = event.properties()
msg = json.dumps(
{
"name": name,
"actor": actor,
"properties": properties,
}
)
print(msg, file=sys.stderr, flush=True)
.. _plugin_hook_register_events:

Expand Down Expand Up @@ -1829,4 +1855,6 @@ The plugin can then call ``datasette.track_event(...)`` to send a ``ban-user`` e

.. code-block:: python
await datasette.track_event(BanUserEvent(user={"id": 1, "username": "cleverbot"}))
await datasette.track_event(
BanUserEvent(user={"id": 1, "username": "cleverbot"})
)

0 comments on commit 56d50a3

Please sign in to comment.