Cross-domain tracking

Report two sites on different base domains as one project by passing the visitor's anonymous id across the link between them

Attribution recognises a returning visitor by the anonymous id its snippet stores in the browser. That storage belongs to one base domain. A visitor who clicks from promo.myproduct.com to app.myproduct.com is still one visitor, because both are myproduct.com. A visitor who clicks from myproduct.com to myproduct.us becomes two: the second site's snippet finds no id and makes a new one, and a purchase there has no visits behind it.

Cross-domain tracking closes that gap. It is not a cross-domain cookie; browsers do not allow one. Instead, the script on this page adds the visitor's anonymous id to every link that points at your other site, as an ajs_aid parameter. When the visitor lands there, the snippet reads the parameter and adopts the id, so the visits on both sites belong to one visitor in one project.

📘

Which sites count as cross-domain

blog.example.com and app.example.com are not cross-domain: they share the base domain example.com and need nothing from this page. promo.myproduct.com and promo.myproduct.us are, because myproduct.com and myproduct.us are different base domains.

Two setups need it:

  • Two or more sites, one project. You want return on ad spend for all of them on one dashboard.
  • Landing pages on their own domain. Ads land on getmyproduct.com and the purchase happens on myproduct.com. The landing visit carries the ad's parameters; passing its id along is what lets the purchase be credited to that ad.

A sample script

Every site runs the same Attribution snippet with the same project id, from Setup Instructions. Then something on every page has to add the parameter to the links that lead to your other sites.

The script below is a sample for a developer, not a drop-in: it shows the approach, waiting for the snippet to be ready and rewriting the href of each link whose hostname is in crossDomains. How you add it, and whether links are rendered the way it expects, depends on the platform your sites are built on, so read it, adapt it and test it before it goes live.

// Add Attribution 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');
    }
  }
});

Run on a page, it leaves links within the same site alone and adds ajs_aid= with the visitor's current id to every link whose hostname is in the list.

What the sample does not cover

As written, the sample tags the links that exist when it runs. It does not see:

  • Links added to the page later, by a script or a framework.
  • Navigation done in JavaScript, form submissions, and server-side redirects.

For those, add the parameter at the moment of the jump: append ajs_aid= and the value of Attribution.user().anonymousId() to the destination URL, the same thing the sample does for links.

Check it

Open your first site, click a link to the second and look at the address bar: the URL carries ajs_aid= and a value. Then open Test your install and watch the Events Stream: the page view on the second site shows the same anonymous id as the visit on the first, and a visitor's timeline lists visits from both sites.

If you have any questions, write to [email protected].