Build an activity analyzer
Write and deploy the plugin that transforms each activity before it is stored.
Your goal
Implement an activity analyzer plugin: one endpoint that receives an activity and returns the activity you want stored, or asks the platform to discard it.
Expected outcome
A deployed plugin version your users can attach to a channel, reformatting or enriching every real-time activity on it.
Before you start
Read Activity analyzers for what an analyzer does, where it sits in the pipeline, and what it may and may not change.
Read Plugin architecture for the plugin, version, worker, and instance model.
Check whether an event rule already covers your case - a rule needs no code.
Create the plugin with type ACTIVITY_ANALYZER, following the procedure in Build a custom feed plugin.
The one endpoint to implement
POST myworker/v1/activity_analysisCalled any time an activity is processed by the platform. The analyzer receives an activity and responds with a new activity, or asks the platform to discard it.
activity_analyzer_id
String
The ID of the analyzer instance to use. Used to retrieve the instance properties.
datamart_id
String
The ID of the datamart.
activity
Object
The UserActivity object to analyse.
In most cases, respond with the modified activity object to store:
To have the platform discard the activity instead of storing it:
On an unexpected error:
Drop an activity
To discard a whole activity - unwanted traffic such as browser bots, for instance - respond { "status": "drop" } with an HTTP 200. With the Plugin SDK, return the response built by core.dropActivity():
A drop is a successful response, not an error. What it changes for the activity and for the analyzers after yours on the channel, and why your filtering must run before any forwarding to a third party, is in Dropping an activity.
Read your instance
The same version is attached to several channels, and each attachment is a separate instance with its own properties. Use activity_analyzer_id from the request to fetch them:
The first returns the instance - name, organisation, plugin ID, group ID, artifact ID. The second returns its properties, each with a technical name, value, type, origin, and whether it is writable.
Cache the properties rather than fetching them per activity: this endpoint is called for every activity on the channel.
Design guidance
Latency
The analyzer sits in the ingestion path. An external lookup per activity puts that provider's latency between your users and their stored data - cache aggressively.
External calls
Any host you call must be declared as an external service and enabled by mediarithmics. See Build a custom feed plugin.
Rate limits
If you call a partner API, configure a throttling on the version.
Bulk imports
Analyzers do not run on bulk activity imports. If a transformation is mandatory for correctness, imported activities must already be formatted correctly. See Import activities.
Edge
Properties that must be usable in Edge segment queries must be stored exactly as pushed - they cannot pass through your analyzer. See Edge.
Watch it in production
Everything written through the Plugin SDK's Logger class is available as logs, in a stream dedicated to the plugin's organisation.
Alongside logs, watch the ACTIVITY_ANALYZER and DOCUMENT_STORE steps in ingestion monitoring: a gap between them is the activities your analyzer dropped.
Verify
A test activity returns transformed as expected, with identifiers untouched.
Two instances on two channels behave according to their own properties.
The
ACTIVITY_ANALYZERandDOCUMENT_STOREcounts are consistent, once the activities you drop on purpose are accounted for.A test activity you mean to discard returns
{ "status": "drop" }and appears on no timeline.Logs can be filtered down to one instance.
An error in your code returns the error shape rather than timing out.
Related pages
Last updated
Was this helpful?