Across domain & device tracking

Ole Dallerup Updated by Ole Dallerup

Across domain & device tracking

Dreamdata supports a few different ways to track users across domains and devices. This article will help you get an overview of how to ensure you are tracking your users best in your setup.

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, it's essential you identify users as frequently as possible so that Dreamdata can use those identifies to associate users using multiple devices. You can 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 different domains 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 links across websites the Dreamdata analytics.js will be able to associate activities on both domains as the same user. Similar if the user had been identified with the same email on both domains as 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;
}
// ** 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(url.host)) {
anchors[i].href = addParam(anchors[i].href, 'ajs_aid', analytics.user().anonymousId());
}
}
});
</script>

Using 3rd party cookies

If either of the above methods works for you, you can also not move your websites under the same root domain example moving company.co.uk to uk.company.com or company.com/uk. There is one last resort.

The solution requires you to use 3rd party cookies, which Dreamdata else doesn't use or recommends using. 3rd party cookies are already blocked in Safari and Firefox and will disappear in other browsers. Google has announced this to happen in 2024 for Chrome. Dreamdata does not recommend using this method, and in all cases, it will work only in browsers still supporting 3rd party cookies (for example, Google Chrome until 2024).

Dreamdata does not recommend using 3rd party cookies. You should only consider this option if the above methods have been tested and evaluated and do not work for you. Many of your visitors should be using a browser where 3rd party cookies are allowed by default such as Chrome.
How it works

The Dreamdata analytics script will load normally when a user visits your website. However, in addition to the standard cookies, the script will load a 3rd party cookie and insert a 1x1 pixel iframe pointing to all the websites you want to track across.

To use the below script, first, you need to set up a CNAME to point your domain to the Dreamdata analytics infrastructure described here. Then use the script below.

<script>
!function(){window.analytics||(window.analytics=[]),window.analytics.methods=["identify","track","trackLink","trackForm","trackClick","trackSubmit","page","pageview","ab","alias","ready","group","on","once","off","setAnonymousId"],window.analytics.factory=function(a){return function(){var t=Array.prototype.slice.call(arguments);return t.unshift(a),window.analytics.push(t),window.analytics}};for(var a=0;a<window.analytics.methods.length;a++){var t=window.analytics.methods[a];window.analytics[t]=window.analytics.factory(t)}analytics.load=function(a,t){if(!document.getElementById("dreamdata-analytics")){t=t||"cdn.dreamdata.cloud",window.a=window.analytics;var e=document.createElement("script");e.async=!0,e.id="dreamdata-analytics",e.type="text/javascript",e.src="https://"+t+"/scripts/analytics/v1/dreamdata.min.js";var n=function(){if(analytics&&analytics.initialize)for(analytics.initialize({"Dreamdata.io":{apiKey:a,host:t}});window.a.length>0;){var e=window.a.shift(),n=e.shift();analytics[n]&&analytics[n].apply(analytics,e)}};e.addEventListener?e.addEventListener("load",function(){n()},!1):e.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&n()};var i=document.getElementsByTagName("script")[0];i.parentNode.insertBefore(e,i)}},

analytics.load("<API_KEY>", "<TRACKING_HOST>");

var getCookie = function(cookieName) {
let cookie = {};
document.cookie.split(';').forEach(function(el) {
let [key, value] = el.split('=');
cookie[key.trim()] = value;
})
return cookie[cookieName];
}
const cross_domain_cookie = 'ajs_cd_id';
var ajs_cdid = getCookie(cross_domain_cookie);
if (ajs_cdid && !sessionStorage.getItem(cross_domain_cookie)) {
analytics.setAnonymousId(ajs_cdid);
}
analytics.page();

analytics.ready(function() {
if (!sessionStorage.getItem(cross_domain_cookie)) {
var topDomain = function(url) {
return url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i)[1].split('.').slice(-2).join('.');
};
var domain = '.' + topDomain(window.location.href);
if (domain === '.') domain = null;
var expires = new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24 * 365);

var ajs_aid = analytics.user().anonymousId();
document.cookie = cross_domain_cookie + "=" + ajs_aid + ";SameSite=None;Secure;domain=" + domain + ";path=/;expires=" + expires.toUTCString();
sessionStorage.setItem(cross_domain_cookie, ajs_aid);
// TODO: replace domains
var domains = ['w2.dreamdata.info', 'w1.dreamdata.eu'];
for (let i = 0; i < domains.length; i++) {
var domain = domains[i];
const iframe = document.createElement("iframe");
iframe.title = domain;
iframe.style.border = '0px';
iframe.width = '1px';
iframe.height = '1px';
iframe.src = 'https://' + domain + '/scripts/iframe/v1/index.html?ajs_cd_id=' + ajs_aid;
document.body.appendChild(iframe);
}
}
});
}();
</script>

How did we do?

Tracking SPAs (Single Page Applications)

Advanced Identification of users and companies

Contact