Across website tracking
If you want to track users across two domains, for example, example.com
and
example.co.uk
, cookies won't suffice since they are not shared across domains.
However, if you append an anonymous ID to all website links, Dreamdata Analytics can associate activities on both domains as belonging to the same user. This is similar to how the user would be identified by the same email address on both domains.
Below is a script that will add the anonymous ID to all links on the site that
point to example.com
or example.co.uk
.
The script will execute once Dreamdata's analytics.js has fully loaded.
<script>
window.dreamdata.ready(function() {
var addParam = (url, param, value) => {
var encodedParam = encodeURIComponent(param);
var encodedValue = encodeURIComponent(value || "");
var link = document.createElement('a');
link.href = url;
link.search += (link.search ? "&" : "") + encodedParam + (value ? "=" + encodedValue : "");
return link.href;
};
var getRootDomain = (url) => {
var domain = url.hostname.split('.');
var length = domain.length;
var isSecondLevelDomain = length > 2 && domain[length - 2].length <= 3;
return (isSecondLevelDomain ? domain[length - 3] + '.' : '') + domain[length - 2] + '.' + domain[length - 1];
};
var hosts = ["example.com", "example.co.uk"];
document.querySelectorAll('a').forEach(anchor => {
var url;
try {
url = new URL(anchor.href);
} catch (error) {
return;
}
var currentDomain = window.location.host;
var targetDomain = url.host;
if (currentDomain !== targetDomain && url.protocol.startsWith("http") && hosts.includes(getRootDomain(url))) {
var anonymousId = dreamdata.user().anonymousId();
anchor.href = addParam(anchor.href, 'ajs_aid', anonymousId);
}
});
});
</script>