# observe\_analytics\_events(Notification, Acc, Context)

Source: https://zotonic.com/docs/3148/observe_analytics_events-notification-acc-context

URI: https://zotonic.com/id/3148

Language: en



Declare events that Site Analytics can record. This is a `foldl` notification, starting with an empty list. Prepend your declarations to the accumulator and return the resulting list; preserve declarations from other observers.

Include `zotonic_mod_analytics/include/analytics.hrl` to use `#analytics_events{}`. The notification record has no fields. Enabled modules export this observer:

```erlang
observe_analytics_events(#analytics_events{}, Acc, _Context) ->
    [#{name => <<"mod_newsletter:sent">>, version => 1,
       title => <<"Newsletter sent">>,
       dimensions => #{<<"kind">> => [<<"digest">>, <<"single">>],
                       <<"language">> => text}} | Acc].
```

Each declaration contains `name`, a namespaced binary of at most 100 bytes; `version`, a positive integer; `title`, a binary of at most 200 bytes; and `dimensions`, a map of at most eight fields. Dimension names are nonempty binaries of at most 40 bytes. A type is `text` or an enum of at most 100 nonempty UTF-8 binaries, each at most 255 bytes. Properties must match the declared schema. Use coarse categories, not personal data or free-form input. Fields named `ip`, `user_id`, `session_id` and `user_agent` are rejected. Only the first 64 module declarations are considered; duplicate names and invalid declarations are ignored. Increment the version when the schema or meaning changes: existing recordings must not silently start collecting a different event.

After a successful domain operation, record a background event:

```erlang
m_analytics:record_event(<<"mod_newsletter:sent">>,
    #{<<"kind">> => <<"digest">>, <<"language">> => <<"en">>}, Context).
```

For a request-associated event, pass `z_context:get_reqdata(Context)` as the third argument to `record_event/4`. Request exclusions and consent policy then apply; request dimensions declared in the schema are derived from the context and cannot be overridden. The three-argument API does not implicitly associate a request. Events are best-effort aggregate measurements, not durable audit records.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/3148?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/3148?language=en>)
