Skip to content
Last updated

Anonymous Visitor IDs With JavaScript SDK

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 AI recommends you verify the implementation of any new features or functionality at your site using the Treasure Data JavaScript SDK version 3 before you start using it in production. 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/td.min.js.

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.

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.

  • First-Party Cookie ID: td_client_id

  • Third-Party Cookie ID: td_global_id

td_client_id is an ID generated as First-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)

  • ID persists in the cookie depending on the browser:

Browser How Long td_client_id Persists
Chrome / EdgeNo cap — persists for the cookie's configured Expires/Max-Age value, until the visitor clears cookies
FirefoxNo cap on a genuine first-party cookie such as td_client_id
SafariCapped to 7 days without a return visit to the site, because the SDK sets td_client_id with document.cookie. Use a server-side cookie to extend this.

See About Treasure AI and Cookie Tracking for the full comparison of browser cookie and storage restrictions, including why Safari's cap exists.

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"

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) — but only in browsers that allow third-party cookies

  • Safari blocks all third-party cookies by default, with no exceptions, so td_global_id is never persisted on any Safari browser, PC or mobile

  • Firefox sets the cookie, but Total Cookie Protection double-keys it to each top-level site, so the same value is not actually shared across domains

  • Chrome and Edge still allow third-party cookies by default as of 2026, so the ID persists and is shared across domains unless visitors clear cookies explicitly

For more details, check Cross Domain Tracking and About Treasure AI and Cookie 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 the fetchGlobalID function are available in the fetchGlobalID reference.