Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. Javascript Tracker
  6. Tracker Setup
  7. Additional options

Additional options

Toggling Anonymous Tracking (2.15.0+)

The JavaScript Tracker can be initialised with anonymousTracking: true or anonymousTracking: { withSessionTracking: true }. 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');
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 full anonymous tracking.

If you want to enable anonymous tracking with session tracking, then you can use:

snowplow('enableAnonymousTracking', { withSessionTracking: true });
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');

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: setUserIdsetUserIdFromLocationsetUserIdFromReferrer, 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);