JavaScript Event Tracking
Last updated
Was this helpful?
Last updated
Was this helpful?
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 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:
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)
Use track
method to record actions that users take in your app.
For example, to log that one of your users sent a message, you'd use the following:
You can omit calling the identify
method by sending the user traits in the user
property of the track
method (see the example above).
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:
If you need the Anonymous user ID created by Encharge, use the EncTracking.getId()
method.
Opt-In and Opt-Out are two separate concepts for the Event Tracking.
When Opt-Out is enabled, it means that the Event Tracking script will not track any events for the current user, including form submission and any manually triggered event such as identify
and track
calls.
If you'd like to not track specific users, call the following snippet before the Encharge tracking has been loaded.
However, if Opt-In is not enabled, the Event Tracking will still record form submissions, and manual identify
and track
calls. However, these events will be recorded without placing a cookie or using local storage on the user's computer. Also, the user's IP address will not be passed to the Encharge backend.
Then, to enable tracking for the current visitor, call the following Javascript code from your cookie consent solution:
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.
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.
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 you'd like to clear all cookies for the current user (e.g. upon log out)use the following call.
The track
method uses the same request body as our .
When the user explicitly (or implicitly according to your settings) enables Opt-In, the events that they perform on your site will be tracked. This includes pageviews, form submissions, manual identify
and track
calls.
You can enable Opt-In by disabling "Wait for opt-in before tracking" when configuring your tracking script in . Alternatively, you can use the following code:
If you'd like to use your own consent mechanism, you need to configure your tracking script in like so: