Skip to main content

For AI agents: a Markdown version of this page is at https://docs.snowplow.io/docs/get-started/custom-events.md. The index of all documentation pages is at https://docs.snowplow.io/llms.txt, and the index for this section is at https://docs.snowplow.io/docs/cdi/llms.txt.

Create your first custom events

Snowplow provides many events and entities out of the box. To match your business more closely, define your own: a self-describing event for something the user does, and an entity for the context it happens in. Each has a schema that the pipeline validates the data against.

This page follows on from Track your first events, and uses the web tracker.

Create the data structures in Console​

In Console, go to Data collection > Data structures and click Create data structure. Create two data structures with the builder:

  1. An event, for example product_viewed, with the properties productId (string) and price (decimal)
  2. An entity, for example user, with the property userType (string)

For each one, fill in the name, description, and vendor, then add the properties. Note the tracking URL that Console generates, such as iglu:com.example_company/product_viewed/jsonschema/1-0-0. See managing data structures for every option.

Clicking Save saves each data structure as a draft. Drafts can't be used for validation, so deploy both data structures to your development environment.

Track the event​

Call trackSelfDescribingEvent with the event's tracking URL and data. Attach the entity in the context array, which must be an array even if you only attach one entity.

javascript
import { trackSelfDescribingEvent } from '@snowplow/browser-tracker';

trackSelfDescribingEvent({
event: {
schema: 'iglu:com.example_company/product_viewed/jsonschema/1-0-0',
data: {
productId: '12345',
price: 10.99
}
},
context: [{
schema: 'iglu:com.example_company/user/jsonschema/1-0-0',
data: {
userType: 'tester'
}
}]
});

Instead of writing these calls by hand, you can generate typed tracking code from your data structures with Snowtype.

For more examples, including entities on page views, see custom event tracking on web.

Check the event​

Open your site with Snowplow Inspector installed, and trigger the event. Inspector shows the event with its entity. To check the data against your schemas in Inspector, add your schemas.

To see what happens when data doesn't match a schema, send an event with a string in the price field, then inspect your first failed events.

Next steps​

To learn more, see the self-describing events documentation, or this tutorial. You can also ask an AI assistant connected to the Snowplow MCP server to design your custom events and entities and create their schemas in your account. Run npx plugins add snowplow/skills to install the MCP server and the Snowplow skills in your coding agent.

Was this page helpful?