Cross-domain tracking

How to track multiple domains against one project

If you have multiple different domains you want to track as a single site then this hook will work for you. It works by adding anonymousId parameter to all links which point to your cross-domain list. It also sets ajs_aid if the one is present in current URL. The code below should work for most setups, please update crossDomains variable to include all of your sites' domains (hostnames) you want to track. This code need to be placed on every site.

πŸ“˜

What are cross-domains?

If you have two websites - blog.example.com and app.example.com these are not cross-domains because they share the same base domain name example.com.

Cross-domains are for example promo.myproduct.com and promo.myproduct.us because they have different base domains myproduct.com and myproduct.us

Example script

The script below is an example of what a cross-domain script may look like. You should be aware to adjust crossDomains for tracking. Also it only tracks HTML links, if you have JavaScript navigation or redirects this script might not be sufficient.

// Add Attribution App snippet code first
// https://dashboard.attributionapp.com/goto/settings/instructions

Attribution.ready(function() {
  // list of your hostnames that you want to share visitors
  const crossDomains = ['hostA.com', 'www.exampleB.net', 'otherC.com'];

  const currentUrl = new URL(window.location.href);

  // check every link and tag external links to our crossDomains with ajs_aid
  const links = document.body.getElementsByTagName('a');
  for (var i = 0; i < links.length; i++) {
    let link = links[i].getAttribute('href');
    if (link == null) {
      continue;
    } else if (link.startsWith('//')) {
      // inherit current protocol
      link = currentUrl.protocol + link;
    } else if (link.startsWith('/')) {
      continue; // local link
    }

    // we use try here because not every link is a valid url
    try {
      const url = new URL(link);

      // if not current hostname and hostname is in our cross-domain list
      if ((url.hostname !== currentUrl.hostname) && (crossDomains.indexOf(url.hostname) > -1)) {
        const newLink = link + ((link.indexOf('?') > -1) ? '&' : '?') + 'ajs_aid=' + Attribution.user().anonymousId();
        // update links to contain anonymous_id in 'ajs_aid' param
        links[i].setAttribute('href', newLink);
      }
    }
    catch (e) {
      // for debugging
      // console.warn(link + ' is not an URL');
    }
  }
});

Once you've adjusted the snippet above you can install it on all your pages to begin tracking cross-domain as one project. You should use this in a case where you may have domainA & domainB, but you want to see return on ad spend for both in one project.

If you have any questions please reach out to [email protected]