JavaScript Event Tracking

Track events that happen on your app's frontend or on your site using the Encharge Javascript tracking code.

The Javascript tracking code will automatically record pageviews.

Make sure you have installed the Encharge JS snippet by following the instructions in the app.

If there is a chance that the Encharge JS snippet will be run after code that calls it, add the following line of code before calling any of its methods:

if (!window.EncTracking) window.EncTracking={queue:[],track:function(e){this.queue.push({type:"track",props:e})},identify:function(e){this.queue.push({type:"identify",props:e})}};

Identify

The identify method can be used to:

  • Uniquely identify the current person.

  • Enrich his or her profile with additional traits

We recommend calling identify on the following events:

  • After a user registers

  • After a user logs in

  • When a user updates their info (for example changes his plan or updates his address)

EncTracking.identify({
email: "[email protected]",
userId: "123"
});

You can pass comma-separated tags property to the identify method to easily add tags to the user. For example: EncTracking.identify({ email: "[email protected]", tags: "tag1, tag2"});

Track

Use track method to record actions that users take in your app.

Please note that pageviews are tracked automatically, so you don't need to emit events for them.

The track method uses the same request body as our Ingest API.

For example, to log that one of your users sent a message, you'd use the following:

// Make sure this code is placed after the Encharge Tracking JS snippet
window.EncTracking.track(
{
// Name of this event (required)
"name": "Sent Message",
// Properties of this event (optional)
"properties": {
"numberOfRecipients": 12
},
// Fields for the current user performing the event (required)
"user": {
// `email` or `userId` is required to uniquely identify this person
"email": "[email protected]",
// Any other fields will be added to this person.
"name": "Michael Scott",
}
}
);

The Encharge Javascript tracking code sends these events to the Encharge Ingest API, and is subject to the same terms.

You can omit calling the identify method by sending the user traits in the user property of the track method (see the example above).

Single-page app Pageviews

Usually, the Encharge JS snippet tracks pageviews automatically. However, in some single-page apps, you might need to explicitly trigger pageview. You can do so as follows:

// Make sure this code is called after the Encharge Tracking JS snippet has loaded
window.EncTracking.client.recordEvent('pageviews',
{
"page": {
"url": "https://google.com",
// Page title is optional
"title": "New Page"
}
}
)

Get Anonymous ID

If you need the Anonymous user ID created by Encharge, use the EncTracking.getId() method.

Opt-out of tracking

If you'd like to not track specific users, call the following snippet before the Encharge tracking has been loaded.

if (!window.EncTracking) window.EncTracking = {};
window.EncTracking.explicitOptOut = true;

Disable form tracking

The Encharge Tracking code automatically captures fields named "email" in any forms on your site. If you don't want to create people from all forms on your site, you can disable ALL form tracking by calling the following snippet before the Encharge tracking has been loaded.

if (!window.EncTracking) window.EncTracking = {};
window.EncTracking.recordForms = false;

Disable pageview tracking

The Encharge Tracking code automatically records pageviews. You can disable all pageviews tracking by calling the following snippet before the Encharge tracking has been loaded.

if (!window.EncTracking) window.EncTracking = {};
window.EncTracking.recordPageViews = false;

Track pageviews on page open

The Encharge Tracking code automatically records pageviews when the user leaves the page. This is done to track time spent on the page. In certain cases, it might be beneficial to track pageviews right after the page has been opened. You can do so by using the following snippet before the Encharge tracking has been loaded:

if (!window.EncTracking) window.EncTracking = {};
window.EncTracking.recordPageViewsOnExit = false;

If you'd like to use your own consent mechanism, you need to configure your tracking script in Site Tracking like so:

Then, to enable tracking for the current visitor, use the following Javascript code:

window.EncTracking.optIn();