Guide To Cross Domain Tracking – Google Analytics (analytics.js)

Cross-Domain-Tracking - Subdomain

Here, we’ll be understanding the concept of Cross Domain Tracking.

In this guide, I’ll also explain behind the scenes processes.

Simply put…

The main goal of Cross Domain Tracking is

when the user goes from siteA to siteB, we want the result to be

USER : 1 & SESSION : 1 


I will consider different scenarios so that we can understand how the metrics would be reported in each.

Some Questions answered here would be:

  1. How do cookies work across domains?
  2. How to do Cross Domain Tracking for sub-domains?
  3. How to do Cross Domain Tracking for 3rd party domains?
  4. When does the Cross Domain Tracking won’t work?
    & more 


I’ll explain each step as to why you should implement it & what it will result into.

So.. let’s dive in

Understanding Google Analytics Cookies

and it all starts with cookies…

I am assuming the code installed is analytics.js, also known as Universal Analytics.
You can also check for the following red line for confirmation.

Eg:
<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’,’https://www.google-analytics.com/analytics.js‘,’ga’);

ga(‘create’, ‘UA-89611791-1’, ‘auto’);
ga(‘send’, ‘pageview’);
</script>


So, when a new user lands on your website, say www.my-A-site.com, the analytics code will store a cookie in their browser.

This cookie is represented as

_ga=GA1.2.1737304625.1491863707

Here,

  • Name of the Cookie = ‘_ga’
  • Value of the Cookie = ‘GA1.2.1737304625.1491863707’
  • ClientID = ‘1737304625.1491863707’

This ClientID is used by google analytics to distinguish between users.

Want to check the cookies stored in your browser?? Sure..

You can open your console (Ctrl+Shift+I) & paste the following code to check these values.

  • GA Cookie = /_ga=[^;]+/i.exec(document.cookie)[0] 
  • ClientID = ga.getAll()[0].get(‘clientId’)

If the cookie is written on my-A-site.com. It can be read by all its subdomains. Eg:

  • www.my-A-site.com
  • blog.my-A-site.com
  • shop.my-A-site.com
  • sub.my-A-site.com
  • & so on.

But CANNOT be read by

  • www.my-B-site.com
  • shop.my-B-site.com
  • blog.my-C-site.com
  • my-D-site.com
  • & so on
Cross-Domain-Tracking - Subdomain

Google Analytics by default writes cookies on the hostname.

For Eg: digishuffle.com

You can verify by opening the console & pasting – ga.getAll()[0].get(‘cookieDomain’)

These cookies can be read across sub-domains. (blog.digishuffle.com , shop.digishuffle.com, etc)

cookieDomain google analytocs - Digishuffle

Google Analytics Cross Domain Tracking for Subdomains

There is not much to do while tracking across subdomains as I have already mentioned that the cookies are shared.

Just follow these 2 steps below.

Step – 1

  • If you are using analytics.js manual code placement, the cookieDomain part should be set to ‘auto’ (which is already there by default) in the line
    ga(‘create’, ‘UA-12345678-1’,‘auto’)
    It will make sure the cookie is stored on the hostname itself. (abc.com)
  • If you are using Tag manager, make sure include cookieDomain to ‘auto’ in fields to set.

Step – 2

  • Make sure the property ID is same across domains. (UA-12345678-1)
  • In Google Analytics, Referral Exclusion already contains your domain name by default. (digishuffle.com). It will exclude all the subdomains automatically. 
    There is nothing to modify here.

Subdomain Tracking Scenario

Just to understand the process, let us look at the following scenario:

  • A user lands on www.my-A-site.com for first time.
  • The analytics.js stores a new cookie on the user’s browser at my-A-site.com. (So it can be read across all xxxxx.my-A-site.com, where xxxxx is variable)
  • When user goes to shop.my-A-site.com, the analytics.js code searches for the ga cookies. It extracts the ClientID & uses the same.
Cross-Sub-Domain-Tracking - Digishuffle

RESULT= USER :1 SESSION : 1

Google Analytics Cross Domain - Other/3rd party Domains Scenario

If you have different domains like www.my-A-site.com & www.my-B-site.com, the cookies set by one cannot be read by other.

Scenario #1 - Same anaytics.js code on two different domian (without Cross Domain Tracking)

Lets us see what happens in this scenario:

  • A user lands on www.my-A-site.com for first time.
  • The analytics.js stores a new cookie on the user’s browser at my-A-site.com
  • Now, when user goes to www.my-B-site.com, the analytics code there will search for the ga cookies.
  • The domain my-B-site.com will not be able to read the cookie stored by my-A-site.com (remember same-origin policy)
  • It will treat this user as new user & store new ClientID in its browser.

RESULT= USER :2 SESSION : 2

Cross-Domain-Tracking-Different-Domains - Digishuffle

Solution By Google:

The solution here by google is to attach the clientID to the destination domain URLs in the source domain & then fetch it when a user lands on destination domains.

So, when a user clicks a link on www.my-A-site.com to www.my-B-site.com. The clientID will be appended to www.my-B-site.com as the parameter. Eg:
www.my-B-site.com/?_ga=1.198767889.435432323.1249578624

On destination domain, ‘allowLinker’ will be used to fetch the clientID from URL & use it.

Simple, isn’t it…

Step 1 - Use Linker Plugin

Google has provided a plugin to automate this work. You need to install the following code on www.my-A-site.com.

  • Linker plugin
    • Linker Plugin code :
      ga('require', 'linker');
      ga('linker:autoLink', ['www.my-B-site.com'], false, true);
      

      The above code will simply append the clientID parameter to all URLs on source.com (www.my-A-site.com) pointing to destination.com (www.my-B-site.com) . 
      Eg: www.my-B-site.com/?_ga=xxxxxxxxxx.xxxxxxxx

      ga('require', 'linker');
      ga('linker:autoLink', ['my-B-site.com','my-C-site.com'], false, true);
      

      For multiple domains, use the above format.

    • Format:
      ga('linker:autoLink', Array of Domain, useAnchor, decorateForms);

 

  • To Test it out, Open your console (Ctrl+Shift+I) & paste the following code:
    ga('create','UA-1234551-1','auto')
    ga('require','linker')
    ga('linker:autoLink', ['example.com'], false, true);
    

    Now, click the below button

  • You will see the parameter attached at the end of URLs.  This should happen to all the URLs pointing to the destination domain.
Cross_Domain_Tracking_ClientID - Digishuffle
  • You can use the above code to test your website too. Just replace the example.com in the code with your destination domain & paste it in the console.
  • Now try clicking on the destination domain link. The _ga parameter should get appended
  • This setup does not work in iframes.

Step 2 - Set AllowLinker To True

On destination domain, modify the following code

ga('create','UA-1234551-1','auto')
 TO
ga('create','UA-1234551-1','auto', {'allowLinker': true})

 This will result in the destination domain using the clientID in the URL.

Cross-Domain-Tracking-plugins - Digishuffle

Step 3 - The Final Code

The code on Souce Domain. (www.my-A-site.com)

<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', ['my-B-site.com'], false, true);

ga('send', 'pageview');

</script>

The code on Destination Domain. (www.my-B-site.com)

<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', ['my-A-site.com'], false, true );

ga('send', 'pageview');

</script>

Step 4 - Referral Exclusion in Google Analytics

  • Lastly, Go To Google Analytics > Property Settings > Tracking Info > Referral Exclusion List > Add Your Destination Domain.

Scenario #2

Now that we have completely setup cross domain tracking, lets look at the following scenario:

  • When a new user lands on www.my-A-site.com, analytics stores a new clientID.
  • The same user clicks on the link to www.my-B-site.com. Due to linker plugin, analytics will append the clientID to all my-B-site.com links on my-A-site.com.
  • The user lands on www.my-B-site.com/?_ga=xxxxxxx.xxxxxxx. Now due to allowlinker set to ‘true’, analytics will check the URL for clientID & use the same. This will result in User identified as the same throughout 2 domains.

 

Cross-Domain-Tracking-Other Domains - Digishuffle
  • Lastly, make sure you have source & destination domain as referral exclusion in analytics interface
    else..
    it will create a new sessions when the user migrates from domain to domain.

Before Adding Referral Exclusion.

RESULT= USER :1 SESSION : 2

After Adding Referral Exclusion

RESULT= USER :1 SESSION : 1

Verify Cross Domain Tracking in Google Analytics

The best way to check is to go step by step. Start by verifying

  1. The links you click to destination domain have clientID appended to it.
    (Check above example by clicking the button to example.com)
  2. Then make sure when you land on destination domain the clientID matches the source domain’s clientID.
    You can use network tab in browser debugger. Filter the requests by ‘collect’ to get hit data sent to analytics.
    cid & tid must be same for both the domains for a single user. 
  • Lastly, In analytics reports you should see User : 1 & Session : 1 , for the process we followed now.
  • If you see Sessions : 2, make sure to add destination & source domain in referral exclusion list.

Some of the reasons Cross domain tracking will not work with this method if..

  • You have destination site embedded in an iframe.
  • You have a server side redirect to the destination domain.
  • You have an event based redirect to the destination domain.
  • Query parameters are stripped off in the destination domain before analytics fires,
  • & so on

Here’s a guide to tackle iframes cross domain tracking by Simo

Conclusion

  • It is important to understand the concept behind the cross domain tracking.
  • If nothing works, you can manually implement cross domain tracking by tagging the URLs on source domain with clientIDs & use them on destination.com. 
    You can use this concept for other tracking tools too.
  • You can even use advanced view filter to combine the hostname + pagepath in analytics reporting interface.
Hostname_PagePath_Google Analytics - Digishuffle
  • Lastly, don’t forget to share with your friends & colleague. 

Ritwik is a product owner at DigiXport. He also loves to write technical & easy to understand blogs for Marketers & Entrepreneurs. A Firm Believer in Teaching -> Learning -> Growing. :)