Visitor IDs can be associated with persistent cookies for a visitor to your website. When someone visits your site, a persistent cookie is set by the web server by including it in the HTTP response to the browser. If the cookie is not in the request, the server generates a unique visitor ID, sets it as a cookie in the HTTP response header, and sends it back with the request.


Prerequisites

  • Basic knowledge of JavaScript / HTML

  • Basic knowledge of Treasure Data

  • Basic knowledge of Treasure Data JavaScript SDK


Treasure Data recommends that you implement any new features or functionality at your site using the Treasure Data JavaScript SDK version 3 Beta. It manages cookies differently. Be aware when referring to most of these articles that you need to define the suggested event collectors and Treasure Data JavaScript SDK version 3 calls in your solutions.

For example, change //cdn.treasuredata.com/sdk/2.5/td.min.js to //cdn.treasuredata.com/sdk/3.0.0-beta/td.min.js.

Cookie Basics

A cookie is a small bit of text placed on the hard drive of your computer by the server of a website that you visit. The cookie is placed there for the purpose of recognizing your specific browser or remembering information specific to your browser, where you to return to the same site.

Differences between 1st Party Cookie and 3rd Party Cookie

All cookies have an owner who tells you who the cookie belongs to. The owner is the domain specified in the cookie.

In “third-party cookie”, the word “party” refers to the domain as specified in the cookie; the website that is placing the cookie. So, for example, if you visit widgets.com and the domain of the cookie placed on your computer is widgets.com, then this is a first-party cookie.

If, however, you visit widgets.com and the cookie placed on your computer says stats-for-free.com, then this is a third-party cookie.


List of Anonymous IDs

Treasure Data’s JavaScript SDK generates two types of anonymous Cookie IDs for all new visitors.

  • 1st Party Cookie ID: td_client_id

  • 3rd Party Cookie ID: td_global_id


1st Party Cookie ID: td_client_id

td_client_id is an ID generated as 1st Party Cookie. The characteristics of this ID is:

  • ID is persisted across the same top-level domains (e.g. is the same ID for a.example.com and b.example.com)

  • ID is NOT persisted across different domains (e.g. are different IDs for test.com and example.com)

  • For iOS 11 Safari, ID persists in the cookie up to 30 days

  • For other browsers, ID persists in the cookie unless visitors clear the browser cookies explicitly

Retrieving td_client_id

You can receive your own td_client_id by calling the following function from the browser’s JavaScript console.

> td.getCookie('_td');
"9eeeed71-8eb8-40f3-9a41-bd8b1f096474"


3rd Party Cookie ID td_global_id

td_global_id is an ID generated as 3rd Party Cookie. The characteristics of this ID is:

  • ID is persisted across the same top-level domains (e.g. is the same ID for a.example.com and b.example.com)

  • ID is persisted across different domains (e.g. is the same ID for test.com and example.com)

  • However, for ALL Safari browsers including PC and Mobile, ID is NOT persisted at all.

  • For other browsers, ID persists in the cookie unless visitors clear the browser cookies explicitly

For more details, check Cross Domain Tracking.

Retrieving td_global_id

You might want to receive td_global_id variable from JavaScript to do something like content personalization. The following code example receives td_global_id by tapping our JavaScript endpoint.

// Configure an instance for your database
var td = new Treasure({
  host: 'in.treasuredata.com',
  writeKey: 'YOUR_WRITE_ONLY_APIKEY_IS_HERE',
  database: 'DATABASE_NAME'
});
// Enable cross-domain tracking
td.set('$global', 'td_global_id', 'td_global_id');

// Receive td_global_id    
var successCallback = function(td_global_id) {
  console.log({
    'td_global_id': td_global_id
  });
};
var errorCallback = function(err) {
  console.log(err);
};

// Track pageview information to 'TABLE_NAME' table
var afterPageviewCallback = function() {
  td.fetchGlobalID(successCallback, errorCallback);
};
td.trackPageview('TABLE_NAME', afterPageviewCallback);

This function is not working if the browser is set to the Do Not Track feature. More technical details about fetchGlobalIDfunction are available success-failure-forcefetch.

  • No labels