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. Browser Tracker
  7. Browser 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:

import { disableAnonymousTracking } from '@snowplow/browser-tracker'; disableAnonymousTracking();
Code language: JavaScript (javascript)

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

import { disableAnonymousTracking } from '@snowplow/browser-tracker'; disableAnonymousTracking({ configuration: { stateStorageStrategy: 'cookieAndLocalStorage' } });
Code language: JavaScript (javascript)
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:

import { enableAnonymousTracking } from '@snowplow/browser-tracker'; enableAnonymousTracking();
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:

import { enableAnonymousTracking } from '@snowplow/browser-tracker'; enableAnonymousTracking({ configuration: { 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:

import { enableAnonymousTracking } from '@snowplow/browser-tracker'; enableAnonymousTracking({ configuration: { options: { withSessionTracking: true } } });
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:

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:

setUserId('joe.blogs@email.com');
Code language: JavaScript (javascript)

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”:

setUserIdFromLocation('id');
Code language: JavaScript (javascript)
setUserIdFromReferrer

setUserIdFromReferrer functions in the same way as setUserIdFromLocation, except that it uses the referrer querystring rather than the querystring of the current page.

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

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:

setCustomUrl('http://mysite.com/checkout-page');
Code language: JavaScript (javascript)

To set a custom referrer, use the setReferrerUrl method:

setReferrerUrl('http://custom-referrer.com');
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:

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.