Set up cross-domain tracking

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

In this article:

Using Google Tag Manager

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

Modifying the Google Analytics tracking code

To set up cross-domain tracking for multiple top-level domains, you need to modify the Google 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 Google Analytics account.
    For cross-domain tracking, set up one property in your Google 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 web pages, 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 web pages.

  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 that 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 what this looks like in full context

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

<script>

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

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

ga('send', 'pageview');

</script>
  • Edit the tracking code on the secondary domain.

    Find the create line in the snippet. Make the following changes to the snippet (the changes that 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 that 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 what this looks like in full context

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

    <script>

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

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

    ga('send', 'pageview');

    </script>
  • Set up reporting views and add filters

    By default, Google 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 Google 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: Hostname Extract A: (.*)
    • Field B: Request URI Extract: (.*)
    • Output To: Request URI Constructor: $A1$B1

    Click Save to create the filter.

    Related resources

    google-developers-svg

    Cross-Domain Tracking – Web Tracking (analytics.js)

    gtm-logo-sm-svg

    Cross-domain tracking