Web tracking: Across domain & device tracking

Updated by Ole Dallerup

Please only implement the scripts below IF you are currently using the legacy version 1.0 of Dreamdata's Web Tracking script. Otherwise, we recommend using the new Web Tracking script, version 2.0, which has built-in support for form tracking, intent sources and other integrations.

This article will help you get an overview of how to ensure you track your users best in your setup.

Across domain & device tracking

Dreamdata supports a few different ways to track users across domains and devices.

Across domain tracking

You don't have to do any when you are looking to track users across domains, for example, www.company.com and blog.company.com. Dreamdata will set the cookie on the root domain company.com; Users will be tracked across any sub-domain automatically as long as the Dreamdata analytics script is installed on all websites.

Across device tracking

To track users across devices, you must identify users as frequently as possible so that Dreamdata can use those identities to associate users using multiple devices. You'll be able to read more about how to track forms here.

When a user is identified on multiple devices, Dreamdata automatically associates sessions across devices as the same user.

Across website tracking

If you want to track users across two domains, for example, company.com and company.co.uk, cookies won't do it as they are not shared cross-domain.

However, if you add an anonymous ID to all website links, Dreamdata analytics.js can associate activities on both domains as the same user. It is similar if the user had been identified with the same email on both domains described here.

Below is a script that will add the anonymous ID to all links on that site that point to company.com or company.co.uk. The script will load when the Dreamdata analytics.js script has been loaded.

<script>
window.analytics.ready(function() {
const addParam = function(url, param, value) {
param = encodeURIComponent(param);
var a = document.createElement('a');
param += (value ? "=" + encodeURIComponent(value) : "");
a.href = url;
a.search += (a.search ? "&" : "") + param;
return a.href;
}
const getRootDomain = function(url) {
const domain = url.hostname;
const elems = domain.split('.');
const iMax = elems.length - 1;
const elem1 = elems[iMax - 1];
const elem2 = elems[iMax];
const isSecondLevelDomain = iMax >= 2 && (elem1 + elem2).length <= 5;
return (isSecondLevelDomain ? elems[iMax - 2] + '.' : '') + elem1 + '.' + elem2;
}

// ** TODO: update host list **
const hosts = ["company.com", "company.co.uk"];

var anchors = document.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++) {
var url;
try {
url = new URL(anchors[i].href);
} catch (e) {
continue;
}

if (window.location.host != url.host && url.protocol.includes("http") && hosts.includes(getRootDomain(url))) {
anchors[i].href = addParam(anchors[i].href, 'ajs_aid', analytics.user().anonymousId());
}
}
});
</script>


How did we do?