Set up cross-domain tracking (analytics.js)

30/11/2019

Cross-domain tracking makes it possible for Analytics to see sessions on two related sites (such as an ecommerce site and a separate shopping cart site) as a single session. This is sometimes called site linking.

To set up cross-domain tracking, you'll need to be comfortable editing HTML and coding in JavaScript, or have help from an experienced web developer.

Learn more about setting up your basic tracking code.

In this article:

Cross-domain tracking overview

To track sessions, Analytics collects a Client-ID value in every hit. Client-ID values are stored in cookies. Cookies are stored on a per-domain basis, and websites on one domain cannot access cookies set for another domain. When tracking sessions across multiple domains, the Client-ID value has to be transferred from one domain to the other. To do this, the Analytics tracking code has linking features that allow the source domain to place the Client-ID in the URL parameters of a link, where the destination domain can access it.

Set up cross-domain tracking using Google Tag Manager

If you use Google Tag Manager to manage your Analytics tracking, follow the instructions in Cross-Domain Tracking.

Set up cross-domain tracking by modifying the tracking code

To set up cross-domain tracking for multiple top-level domains, you need to modify the Analytics tracking code on each domain. You should have a basic knowledge of HTML and JavaScript or work with a developer to set up cross domain tracking. The examples in this article use the Universal Analytics tracking code snippet (analytics.js).

  1. Set up a property in your Analytics account.
    For cross-domain tracking, set up one property in your Analytics account. Use the same tracking code snippet and tracking ID from that property for all of your domains.

    You need to edit the tracking-code snippet for cross-domain tracking to work. If you haven’t already included the snippet on all your webpages, you might want copy and paste it into a text editor before continuing with the instructions here. This way, you only have to make the changes once before including the modified snippet on all your webpages.

  2. Edit the tracking code for the primary domain.
    Find the create line in the snippet. For a website called example-1.com, it looks like this:

     

      ga('create', 'UA-XXXXXXX-Y', 'example-1.com');

    Make the following changes to the snippet (the changes you need to make are in bold red text):

      ga('create', 'UA-XXXXXXX-Y', 'auto', {'allowLinker': true});
      ga('require', 'linker');
      ga('linker:autoLink', ['example-2.com']);

    Remember to replace the example tracking ID (UA-XXXXXX-Y) with your own tracking ID, and replace the example secondary domain (example-2.com) with your own secondary domain name.



    The tracking-code snippet must contain these changes every place it appears on your primary domain.

     

    For three or more domains

    Follow the example above, but add the other domains to the autoLink plugin. Even the additional comma here is important:

    ga('linker:autoLink', ['example-2.com', 'example-3.com'] );

    See an example of the complete code snippet

    The tracking code snippet on your primary domain should look like this:

  3. Edit the tracking code on the secondary domain.

    Find the create line in the snippet. Make the following changes to the snippet (the changes you need to make are in bold red text):

      ga('create', 'UA-XXXXXXX-Y', 'auto', {'allowLinker': true});
      ga('require', 'linker');
      ga('linker:autoLink', ['example-1.com'] );

    Remember to replace the example tracking ID (UA-XXXXXX-Y) with your own tracking ID, and replace the example primary domain (example-1.com) with your own primary domain name.

    The tracking-code snippet must contain these changes every place it appears on your secondary domain.

    For three or more domains

    Follow the example above, but add the other domains to the autoLink plugin. Even the additional comma here is important:

    ga('linker:autoLink', ['example-1.com', 'example-3.com'] );

    See an example of the complete code snippet

    The tracking-code snippet on your secondary domain(s) should look like this:

Set up reporting views and add filters

By default, Analytics only includes the page path and page name, not the domains name. For example, you might see your pages appear in the Site Content report like this:

  • /about/contactUs.html
  • /about/contactUs.html
  • /products/buy.html

Because the domain names aren’t listed, it might be hard to tell which domain each page belongs to.

To get the domain names to appear in your reports you need to do two things: Create a copy of your reporting view that includes data from all your domains in it and add an advanced filter to that new view. The filter will tell Analytics to display domain names in your reports.

Follow this example to set up a view filter that displays domain names in your reports when you have cross domain tracking set up. For some fields, you need to select an item from the dropdown menu. For others, you need to input the characters here:

  • Filter Type: Custom filter > Advanced
  • Field A --> Extract A: Hostname = (.*)
  • Field B --> Extract B: Request URI = (.*)
  • Output To --> Constructor: Request URI = $A1$B1

Click Save to create the filter.

You can validate that filters are working as you expect using Google Tag Assistant Recordings. Tag Assistant Recordings can show you exactly how your filters change your traffic.

Add domains to the Referral Exclusion List

When a user journey crosses from your first domain to your second domain, Analytics interprets that as the user having been referred by your first domain to your second domain, and Analytics creates a new session. If you want to be able to track a single session across multiple domains, you need to add your domains to the referral exclusion list.

Check that cross-domain Tracking Works

The best way to validate that cross-domain tracking is set up correctly is to use Google Tag Assistant Recordings. When you make a session that crosses domains, it can tell you instantly whether it worked or not.

Here’s an example Tag Assistant Recordings report that shows what it looks like when cross-domain tracking is not setup properly.

Related resources

analytics.js

gtag.js

 

* Nguồn: Google Analytics