[UA] Set up cross-domain measurement (analytics.js) [Legacy]

You are viewing a legacy article about Universal Analytics. Learn more about Google Analytics 4 replacing Universal Analytics.

Cross-domain measurement 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 measurement, you'll need to be comfortable editing HTML and JavaScript, or have help from an experienced web developer.

Learn more about how to set up your code.

In this article:

Cross-domain measurement overview

To measure 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 you measure sessions across multiple domains, the Client-ID value has to be transferred from one domain to the other. To do this, the Analytics 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 measurement using Google Tag Manager

If you use Google Tag Manager, follow the instructions in Cross-domain measurement.

Set up cross-domain measurement by modifying analytics.js

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

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

    You need to edit the snippet for cross-domain measurement 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 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 Google Analytics ID (UA-XXXXXX-Y) with your own ID, and replace the example secondary domain (example-2.com) with your own secondary domain name.



    The 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 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','//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>
  3. Edit the 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 Google Analytics ID (UA-XXXXXX-Y) with your own ID, and replace the example primary domain (example-1.com) with your own primary domain name.

    The 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 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','//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, 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 measurement 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 measure a single session across multiple domains, you need to add your domains to the referral exclusion list.

Check that cross-domain measurement Works

The best way to validate that cross-domain measurement 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 measurement is not setup properly.

Related resources

analytics.js

gtag.js

 

Was this helpful?

How can we improve it?
true
Choose your own learning path

Check out google.com/analytics/learn, a new resource to help you get the most out of Google Analytics 4. The new website includes videos, articles, and guided flows, and provides links to the Google Analytics Discord, Blog, YouTube channel, and GitHub repository.

Start learning today!

Search
Clear search
Close search
Google apps
Main menu
10010993179213362215
true
Search Help Center
true
true
true
true
true
69256
false
false