Additional options
Documentation for latest release
The documentation listed here is for Version 2 of the JavaScript Tracker. Version 3 is now available and upgrading is recommended.
Toggling Anonymous Tracking (2.15.0+)
The JavaScript Tracker can be initialised with anonymousTracking: true
or anonymousTracking: { withSessionTracking: true }
or anonymousTracking: { withServerAnonymisation: true }
(2.17.0+). You can read more about the anonymous tracking features here.
You may wish to toggle this functionality on or off during a page visit, for example when a user accepts a cookie banner you may not want to disable anonymous tracking, or when a user logs in to your site.
Disable Anonymous Tracking (2.15.0+)
To do this you can call the following methods:
snowplow('disableAnonymousTracking');
or, if you wish to also adjust the stateStorageStrategy
when enabling:
snowplow('disableAnonymousTracking', 'cookieAndLocalStorage');
Note: If configuring the tracker with stateStorageStrategy: 'localStorage'
and anonymous tracking using withSessionTracking: true
, then if you change to a stateStorageStrategy
which prefer cookies such as cookie
or cookieAndLocalStorage
then the session identifiers will reset. To maintain session identifiers, ensure you use the same stateStorageStrategy
.
Enable Anonymous Tracking (2.15.0+)
Note: Enabling Anonymous tracking will clear all current user, session and page data.
If you wish to enable Anonymous Tracking, you can call:
snowplow('enableAnonymousTracking');
which will enable client side anonymous tracking.
For full, cookieless, anonymisation (2.17.0+), including anonymising data within the Snowplow Collector (cookies and ip address), then you can enable server anonymisation too:
snowplow('enableAnonymousTracking', { withServerAnonymisation: true }); // Available from v2.17.0
Server Anonymisation requires the Snowplow Stream Collector v2.1.0+. Using a lower version will cause events to fail to send until Server Anonymisation is disabled.
If you want to enable anonymous tracking with session tracking, then you can use:
snowplow('enableAnonymousTracking', { withSessionTracking: true });
From v2.18.0 it's also possible to change the stateStorageStrategy
when enabling Anonymous Tracking, allowing you to switch off storage when turning anonymous tracking on:
snowplow('enableAnonymousTracking', { }, 'none'); // Available from v2.18.0
Clear user data (2.15.0+)
If you wish to clear all the cookies and local storage values which contain user data when switching on anonymous tracking, or triggered by other actions on your site, you can call the following:
snowplow('clearUserData');
From v2.18, this will also clear in memory session and user identifiers too. This ensures all possible identifiers are cleared and even if tracking is resumed you will see new session and user identifiers. If you’d like to preserve the in-memory session and user identifiers, for future events should you continue tracking after clearing the cookies, you can do so:
snowplow('clearUserData', true, true);
Setting the user ID
The JavaScript Tracker automatically sets a domain_userid
based on a first party cookie.
There are many situations, however, when you will want to identify a specific user using an ID generated by one of your business systems. To do this, you use one of the methods described in this section: setUserId
, setUserIdFromLocation
, setUserIdFromReferrer
, and setUserIdFromCookie
.
Typically, companies do this at points in the customer journey when the user identifies him / herself e.g. if he / she logs in.
Note: this will only set the user ID on further events fired while the user is on this page; if you want events on another page to record this user ID too, you must call setUserId
on the other page as well.
setUserId
setUserId
is the simplest of the four methods. It sets the business user ID to a string of your choice:
snowplow('setUserId', 'joe.blogs@email.com');
Note: setUserId
can also be called using the alias identifyUser
.
setUserIdFromLocation
setUserIdFromLocation
lets you set the user ID based on a querystring field of your choice. For example, if the URL is http://www.mysite.com/home?id=user345
, then the following code would set the user ID to “user345”:
snowplow('setUserIdFromLocation', 'id');
setUserIdFromReferrer
setUserIdFromReferrer
functions in the same way as setUserIdFromLocation
, except that it uses the referrer querystring rather than the querystring of the current page.
snowplow('setUserIdFromReferrer', 'id');
setUserIdFromCookie
Use setUserIdFromCookie
to set the value of a cookie as the user ID. For example, if you have a cookie called “cookieid” whose value is “user123”, the following code would set the user ID to “user123”:
snowplow('setUserIdFromCookie', 'cookieid');
Setting a custom page URL and referrer URL
The Snowplow JavaScript Tracker automatically tracks the page URL and referrerURL on any event tracked. However, in certain situations, you may want to override the one or both of these URLs with a custom value. (For example, this might be desirable if your CMS spits out particularly ugly URLs that are hard to unpick at analysis time.)
To set a custom page URL, use the setCustomUrl
method:
snowplow('setCustomUrl', 'http://mysite.com/checkout-page');
To set a custom referrer, use the setReferrerUrl
method:
snowplow('setReferrerUrl', 'http://custom-referrer.com');
On a single-page app, the page URL might change without the page being reloaded. Whenever an event is fired, the Tracker checks whether the page URL has changed since the last event. If it has, the page URL is updated and the URL at the time of the last event is used as the referrer. If you use setCustomUrl
, the page URL will no longer be updated in this way. If you use setReferrerUrl
, the referrer URL will no longer be updated in this way.
If you want to ensure that the original referrer is preserved even though your page URL can change without the page being reloaded, use setReferrerUrl
like this before sending any events:
snowplow('setReferrerUrl', document.referrer);