Setting the user ID
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:
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:
setUserId can also be called using the alias
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”:
setUserIdFromReferrer functions in the same way as
setUserIdFromLocation, except that it uses the referrer querystring rather than the querystring of the current page.
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”:
Setting a custom page URL and referrer URL
To set a custom page URL, use the
To set a custom referrer, use the
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: