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 Trackers (Web and Node.js)
  6. JavaScript Tracker
  7. JavaScript Tracker v3 Reference
  8. Tracker Setup
  9. Additional options

Additional options

Toggling Anonymous Tracking

The JavaScript Tracker can be initialised with anonymousTracking: true or anonymousTracking: { withSessionTracking: true } or anonymousTracking: { withServerAnonymisation: 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

To do this you can call the following methods:

Code language: JavaScript (javascript)

or, if you wish to also adjust the stateStorageStrategy when enabling:

snowplow('disableAnonymousTracking', { stateStorageStrategy: 'cookieAndLocalStorage' });
Code language: JavaScript (javascript)

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

Note: Enabling Anonymous tracking will clear all current user, session and page data.

If you wish to enable Anonymous Tracking, you can call:

Code language: JavaScript (javascript)

which will enable client side anonymous tracking.

For full, cookieless, anonymisation, including anonymising data within the Snowplow Collector (cookies and ip address), then you can enable server anonymisation too:

snowplow('enableAnonymousTracking', { options: { withServerAnonymisation: true } });
Code language: JavaScript (javascript)

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', { options: { withSessionTracking: true } });
Code language: JavaScript (javascript)

From v3.1.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', { options: {}, stateStorageStrategy: 'none' }); // Available from v3.1.0
Code language: JavaScript (javascript)
Clear user data

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:

Code language: JavaScript (javascript)

From v3.1, 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', { preserveSession: true, preserveUser: true });
Code language: JavaScript (javascript)

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 is the simplest of the four methods. It sets the business user ID to a string of your choice:

snowplow('setUserId', '');
Code language: JavaScript (javascript)

Note: setUserId can also be called using the alias identifyUser.


setUserIdFromLocation lets you set the user ID based on a querystring field of your choice. For example, if the URL is, then the following code would set the user ID to “user345”:

snowplow('setUserIdFromLocation', 'id');
Code language: JavaScript (javascript)

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');
Code language: JavaScript (javascript)

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');
Code language: JavaScript (javascript)

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', '');
Code language: JavaScript (javascript)

To set a custom referrer, use the setReferrerUrl method:

snowplow('setReferrerUrl', '');
Code language: JavaScript (javascript)

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);
Code language: JavaScript (javascript)

If you’d like to learn more about Snowplow Insights you can book a demo with our team, or if you’d prefer, you can try Snowplow technology for yourself quickly and easily.