Skip to main content

Create an event

You could create event action using Event class. Like this:

note

This's not a recommended way to create an event. Please read below for better options.

const ga4 = require("dataform-ga4-sessions");// Define your configconst config = {  dataset: "analytics_XXXXXX",  incrementalTableName: "events_XXXXXX",  incrementalTableEventStepWhere: "event_name = 'purchase'",  nonIncrementalTableEventStepWhere: "event_name = 'purchase'",};// Create event objectlet purchase = new ga4.Event(eventConfig);purchase.target = { tableName: "purchase" };purchase.publish();

In this example, config variable has where condition for incremental and non-incremental tables to filter only needed events (purchase in our case). And also we manually set target table name to purchase.

You could achieve the same result using setEventName method, like this:

const ga4 = require("dataform-ga4-sessions");// Define your configconst config = {  dataset: "analytics_XXXXXX",  incrementalTableName: "events_XXXXXX",};// Create event objectlet purchase = new ga4.Event(eventConfig);purchase.setEventName("purchase");purchase.publish();

setEventName method automatically sets target table name and add WHERE conditions the same as in the previous example.

Actually, you could use practically all methods you could use for Session objects, because they both extend the same DataformActio class. So for example you could add columns:

note

The huge difference between using methods like addColumns on Event and Session objects, is that for Session the value will be session-scope (the first non null value from events during a session), but for Event it will be event-scope (value for this particular event).

GA4 provides the list of recommended events and automatically-collected events. And for these events not only names but also event parameters are defined. That's why the package provides an EventFactory class to simplify the creation of any recommended events. And in general, even for custom events it's recommended to use EventFactory methods instead of Event constructor.