Cross-Domain Tracking with JS SDK

Table of Contents

Prerequisites

Enable Cross-Domain Tracking

Cross-Domain tracking is not enabled by default for JavaScript SDK. You need to call td.set('$global', 'td_global_id', 'td_global_id'); to enable the feeature. You need JavaScript SDK v1.7.1 or newer.

<script type="text/javascript">
  // 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');
  // Track pageview information to 'pageviews' table
  td.trackPageview('pageviews');
</script>

This line will add additional column called td_global_id to the destination table. You might want to check out Sessionization functions to count sessions as well.

3rd Party Cookie Limitation

Since this feature uses 3rd party cookie, you cannot track the browsers who prohibit the use of 3rd party cookie by default. Here’s a sample of each browser’s ‘default’ configuration we observe as of Sep 2016. Most notably, Mobile Safari prohibits it by default.

Browser Version OS 3rd Party Cookie
(Never Visited the Site)
3rd Party Cookie
(Ever Visited the Site)
Chrome 50 Mac OS X [X] [X]
Chrome 46, 50 Windows [X] [X]
Chrome Mobile 28, 49 Android [X] [X]
Chrome Mobile 47 iOS [X] [X]
Safari 9.1 Mac OS X [X]
Safari 5.1 Windows [X]
Safari Mobile 9.0 iOS [X]
Firefox 46 Mac OS X [X] [X]
Firefox 46 Windows [X] [X]
Microsoft Edge 13 Windows [X] [X]
Microsoft IE 11 Windows
Opera 37 Mac OS X [X] [X]
Opera 37 Windows [X] [X]

Device Fingerprint

Device Fingerprint is a commonly used techniques to create hash value from device information to create unique value not depending on the 3rd party cookie.

The example code below is using fingerprintjs2 library to generate the device fingerprint and add the column called td_fingerprint_id.

<!-- Load fingerprint2 library -->
<script src="//cdn.jsdelivr.net/fingerprintjs2/1.4.4/fingerprint2.min.js"></script>
<script type="text/javascript">
  // 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');
  // Enable device fingerprint
  new Fingerprint2().get(function (result) {
    td.set('$global', 'td_fingerprint_id', result)
  });
  // Track pageview information to 'pageviews' table
  td.trackPageview('pageviews');
</script>

Please note that generating device fingerprint takes extra time, usually 500ms (You can try from here) with extra CPU resource for your visitors.

There’s a lot of options to optimize which information sources to generate device fingerprint. If you need further optimization, please refer to fingerprint2 library’s documentation as well.

Fetch td_global_id value from JavaScript

You might want to receive td_global_id variable from JavaScript to do something like content personalization. The snippet below will receive td_global_id by hitting 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 browser turns on Do No Track feature. The more technical details of fetchGlobalID function is available here.


Last modified: Jan 04 2017 11:20:26 UTC

If this article is incorrect or outdated, or omits critical information, please let us know. For all other issues, please see our support channels.