Skip to content

Event Module

The event module provides an interface to the datalayer and the event bus in a namespace. The module is made to be robust for a variety of events and parameters.

This module is available directly from the namespace:

var event_module = namespace.event();

Architecture

The event module contains two core components that its API builds off of: the datalayer and the event bus.

The datalayer is a concept used by most tag managers. It's a centralized location to store the current state of the page and/or the visitor, and the Web SDK follows suit. It stores both an array of events added to the datalayer as well as the compiled state as an object. However, it also adds another level of functionality: the event bus.

The event bus is an additional layer wrapped around the datalayer. You can subscribe to events, publish events, and even control whether the event reaches the datalayer.

API

The event API wraps these components in a few simple functions to work with events.

track

The track method should be used when you want to track an event that occurred. This method does two things:

  • push the event to subscribers.
  • push the event to the datalayer.

Usage

event_module.track(event, eventModel);

Parameters

Parameter Example Description
event
'view'
The event action being tracked
eventModel
{
object: 'page',
label: 'page view',
vendor: 'Orbee',
product: 'Site Tracking'
}
The event model (meets Event Specification) for the event

pub

The pub method should be used when you want to publish an event that should not be added to the datalayer. This includes events that are Web SDK-based, like resources being provided.

Usage

event_module.pub(eventName, eventObject);

Parameters

Parameter Example Description
eventName
'newTemplateProvided'
The name of the event being published
eventObject
{
id: 'abc123',
name: 'testTemplate',
tempalte: 'hello {{ .world }}'
}
The event being published

push

The push method should be used to push state into the datastore without an official event tied to it. These will be published with an event name of 'datalayer.push'.

Usage

event_module.push(state);

Parameters

Parameter Example Description
state
{
user: {
age: '42'
}
}
The state to push into the datalayer



NOTE: do not include a first-level key of event.

sub

The sub method should be used to subscribe to events from the event bus. A special notation for event can be used to get all events: '*'. You can also include in the options that you only want events from a certain publishing method -- e.g. 'track' events only.

Usage

event_module.sub(event, listener, [options]);

Parameters

Parameter Example Description
event
'view'
The event to subscribe to
listener
function(event) {
...
}
The callback listener for the event being subscribed to
options
{
method: 'track'
}
A list of options for the subscription

Supported types for method parameter:
- push,
- track,
- pub and
- all (default)