Cross Domain Tracking in Google Analytics – Complete Guide

Table of Contents for Cross Domain Tracking in Google Analytics – Complete Guide

  1. What is cross-domain tracking in Google Analytics?
  2. Why do you need cross-domain tracking?
  3. When do you need cross-domain tracking?
  4. Requirements for setting up cross-domain tracking
  5. Introduction to Google Analytics cookies
  6. Introduction to client ID
  7. Introduction to domains
  8. Introduction to domain levels
  9. Introduction to cookie domains
  10. Automatic cookie domain configuration: analytics.js
  11. Automatic cookie domain configuration: gtag.js
  12. Introduction to cookie path
  13. Introduction to cookie expires
  14. Referral exclusion list
  15. Implementing tracking across a primary domain and its sub-domains
  16. Requirements for setting up cross-domain tracking between primary domains
  17. Introduction to cross-domain links
  18. Introduction to ‘allowLinker’
  19. Introduction to ‘autoLink’ plugin
  20. Introduction to ‘linker:autoLink’ command
  21. Introduction to linker parameters
  22. Introduction to decorating links
  23. Cross-domain tracking code
  24. Implementing cross-domain tracking between two primary domains
  25. Implementing cross-domain tracking between three primary domains
  26. Tracking across top-level domains
  27. Using the same Google Analytics tracking code on multiple primary domains for cross-domain tracking
  28. Cross-domain tracking and roll-up reporting
  29. Code for tracking a primary domain
  30. Code for tracking across a primary domain and its sub-domains
  31. Code for tracking across a primary domain and its top-level domains
  32. Code for tracking across multiple primary domains
  33. What is _setDomainName() ?
  34. What is _setAllowLinker() ?
  35. What is_link() ?
  36. What is _linkBypost() ?
  37. When is traffic from a sub-domain treated as referral traffic by Google Analytics?
  38. Disadvantage of tracking across sub-domains using a single website profile
  39. Best practices for tracking across subdomains?
  40. When should you not add sub-domain Google Analytics tracking code to all the web pages of a sub-domain?
  41. Should you use _link() and _linkByPost() methods for links and forms between the main domain and its sub-domain?

What is cross-domain tracking in Google Analytics?

In the context of Google Analytics, cross-domain tracking is the tracking of website users and their activities across two or more domains.

By default, Google Analytics is not able to track website users and their activities across multiple domains. That is because Google Analytics uses first-party cookies that can be read by only that domain that issued it. You would need to manually set up cross-domain tracking.

Why do you need cross-domain tracking?

Without cross-domain tracking set up, you won’t be able to understand and track a user’s journey that spans multiple domains.

For example, consider the following scenario:

A user landed on your website, say abc.com, via the search term ‘top men shoes’ on Google.

He then navigated to another website, say xyz.com/abc-cart, to make a purchase as both your shopping cart and the order confirmation page are hosted on this website.

Now without cross-domain tracking set up, Google Analytics will report the same user as two different users (one user visited abc.com, and a different user visited xyz.com/abc-cart.

And the user session that actually spans across the two domains will be counted as two different GA sessions instead of a single session.

So the GA report of abc.com may tell you that a user visited abc.com via the search term ‘top men shoes‘ on google but did not make a purchase.

The GA report of the website xyz.com may tell you that a user visited xyz.com/abc-cart from abc.com and then made a purchase.

So, without a cross-domain tracking setup, abc.com (i.e. your own website) would end up getting all the credit for conversion instead of the search term ‘top men shoes‘ and google organic search traffic.

Get the E-Book (50 Pages)

Get the FREE E-Book (50+ Pages)

Consider another scenario:

A user landed on your website abc.com via the search term ‘top men shoes’ on Google.

He then navigated to another website xyz.com/abc-cart because your shopping cart is hosted there.

After completing the purchase, he returned back to your website abc.com, as the order confirmation page is hosted on your website.

In this case, GA may attribute sales to xyz.com/abc-cart instead of the organic search term ‘top men shoes‘.

Thus, without a cross-domain tracking setup, you may have a hard time determining the original source of your goal conversion and/or ecommerce transaction.

Without a cross-domain tracking setup, most of your conversions could end up being attributed to direct traffic or the wrong traffic source.

When do you need cross-domain tracking?

You need cross-domain tracking when your Google Analytics session spans across two or more domains, and you want Google Analytics to treat GA sessions spanned across different domains as a single session.

Your Google Analytics session can span across two or more domains in the following scenarios:

#1 When your checkout process occurs on a different domain (quite common, when you use a third-party shopping cart like Shopify)

#2 When your goal conversions or ecommerce transactions take place on a different domain (quite common in the case of affiliate websites).

Requirements for setting up cross-domain tracking

In order to set up cross-domain tracking, you would first need to understand the following terms:

  1. Google Analytics Cookies
  2. Client ID
  3. Domains and subdomains
  4. Cookie’s configurations

In cross-domain tracking, we share cookie information between two or more domains. This domain can be a primary domain or subdomain.

The cookie information that we pass from one domain to another is the client ID.

This client ID is stored in the Google Analytics cookie _ga.

The client ID behaves according to the cookie’s configurations (cookie domain, cookie path and expiry date).

So before we are in the position to understand and implement cross-domain tracking, we would need to first understand Google Analytics cookies, client ID and the concept of domains and subdomains.

Note: Cross-domain tracking is highly technical in nature. Depending upon a website’s front-end and/or back-end configuration, implementing cross-domain tracking can be either a walk in the park or a tough nut to crack.

Introduction to Google Analytics cookies

In the context of Google Analytics, a cookie is a text file that is used to:

  1. Identify unique users
  2. Identify unique sessions
  3. Store information (like traffic source) about users’ GA sessions and campaigns.
  4. Throttle the request rate

There are two types of GA cookies:

#1 First-Party GA Cookies – It is the GA cookie issued by the website being visited, and only the website which issued the cookie can read it. Examples of first-party GA cookies: _ga and _gat

#2 Third-Party GA Cookies – It is the GA cookie issued by the website other than the website being visited. Example of third-party GA cookies: DoubleClick cookies.

Google Analytics uses cookies based on the JavaScript library being used.

#1 If you are using the classic Google Analytics, then you are using the ga.js JavaScript library.

#2 If you are using the latest advanced Google Analytics version (Universal Analytics), then you are using the analytics.js JavaScript library.

#3 If you are using the latest Google Analytics version (Universal Analytics) then, you are using the gtag.js JavaScript library.

The ga.js JavaScript library sets the following first-party cookies:

  1. _utma (unique visitor cookie)
  2. _utmb (session cookie)
  3. _utmc (session cookie)
  4. _utmt
  5. _utmv (visitor segmentation cookie)
  6. _utmx (Google Analytics Content Experiment cookie)
  7. _utmz (Campaign cookie)

The analytics.js JavaScript library sets the following cookies:

#1  _ga cookie (it is used to identify unique users and it expires after 2 years)

#2  _gat cookie (it is used to throttle the request rate and it expires after 10 minutes)

#3 Doubleclick cookies (these are used when you use Google display advertising features like remarketing)

The gtag.js JavaScript library sets the following cookies:

#1  _ga cookie (it is used to identify unique users and it expires after 2 years)

#2  _gid cookie (it is used to distinguish users and it expires after 24 hours)

#3 _ga_<container-id>  cookie (it is used persist session state and it expires after 2 years)

Google Analytics set up a cookie on your hard disk, as soon as you visit a webpage, on which a valid Google Analytics tracking code is installed.

Introduction to client ID

When a user visits your website (say website A) for the first time, a unique ID is assigned to him called the ‘client ID’.

Google Analytics identifies a user as unique through a combination of a unique random number and the first timestamp (i.e. the time of the first visit). This combination is called the client ID.

This client ID is stored in the Google Analytics cookie _ga.

_ga cookie is made up of the following four fields:

The first field is the version number, for example, GA1.

The second field is the number of components at the domain separated by a dot.

By default, the _ga cookie is set on the top-level domain with the root level (/) path. So, if you have set a cookie at the top-level domain like optimizesmart.com then the second field would have a value of 2 as there are two components separated by a dot. One component is ‘optimizesmart’ and the second component is ‘com’.

If you have set up a cookie at the sub-domain level, like analytics.optimizesmart.com, then the second field would have a value of 3 as there are now three components separated by a dot. The first component is ‘analytics’. The second component is ‘optimizesmart’. The third component is ‘com’

The third field is a random unique ID (randomly generated number). Here 1368576646 is the random unique ID.

The fourth field is the first timestamp i.e. the time when the cookie was first set for the user. Here 1596648643 is the first timestamp.

The third and fourth fields together make the client ID. So, the client ID would be 1368576646.1596648643 

This ‘client ID’ is used to identify the user as new and unique and to combine all of his hits (pageviews, screenviews, events, transactions etc) together to form a GA session.

When the same user later, again visit your website, Google Analytics is able to identify the user as returning user through his client ID.

When the same user visits another website say website ‘B’ (which also has got GA installed), the GA tag of the website ‘B’ sets a completely different ‘Client ID’ for the user and thus the same user is treated as a different user by website B.

Since _ga cookie is a first-party cookie, it can only be read by the website which issued it. Consequently,   _ga cookie set up by website ‘A’ can not (by default) be read/shared by website ‘B’ and vice versa.

Because of this reason, by default, cross-domain tracking between two or more primary domains is not possible.

Also worth noting, that by default, cross-web browsers tracking and cross-device tracking is also not possible.

The _ga cookie can exist only on the device and browser where it has been set. Since client ID is stored in a GA cookie, the client ID will also exist only on the device and browser where it has been set. Because of this reason, by default, GA can’t identify unique users across different web browsers and devices.

So by default, cross-web browsers tracking and cross-device tracking is not possible.

Note: Client ID can be used to differentiate one _ga cookie from another. So if two _ga cookies have got the same client ID then they are most likely the same cookie.

Introduction to domains

A Uniform Resource Locator (URL) is used to uniquely identify a web resource (like a web page) on the internet.

Consider the following URL: https://www.optimizesmart.com/best-excel-charts

  • https:// is the internet protocol
  • www.optimizesmart.com is the hostname (also known as a domain name or domain)
  • /best-excel-charts is the request URI

Thus a URL is made up of internet protocol, hostname and request URI.

Note: By default, Google Analytics reports only the request URI in its reports.

A domain name is a string that is used to uniquely identify a website on the internet. It is made up of two or more substrings separated by the (dot) symbol ‘.’

For example, the domain name www.optimizesmart.com is made up of the following three sub-strings:

  • www
  • optimizesmart
  • com

Similarly, the domain name learn.optimizesmart.com is made up of the following three sub-strings:

  • learn
  • optimizesmart
  • com

Introduction to domain levels

A domain name is made up of at least two levels: a top-level domain and a primary domain.

For example, the domain name optimizesmart.com is made up of the following two domain levels:

  • com (top-level domain)
  • optimizesmart (primary domain)

The domain name www.optimizesmart.com is made up of the following three domain levels:

  • com (top-level domain)
  • optimizesmart (primary domain)
  • www (sub-domain)

Similarly, the domain name learn.optimizesmart.com is made up of the following three domain levels:

  • com (top-level domain)
  • optimizesmart (primary domain)
  • learn (sub-domain)

The domain name js.learn.optimizesmart.com is made up of the following four domain levels:

  • com (top-level domain)
  • optimizesmart (primary domain)
  • learn (sub-domain)
  • js (sub-sub-domain)

Technically there is no limit to the number of domain levels you can use. But I have not seen more than four domain levels being used in a URL.

The Top Level Domain (TLD)

The highest level domain is called the top-level domain (or TLD).

For example, in the domain name www.optimizesmart.com, the ‘.com’ part is the TLD.

The top-level domain can be generic, location-specific or business/industry-specific.

Examples of generic TLDs: .com, .net, .edu, .info etc.

Examples of location-specific TLDs: .uk, .fr, .de, .in etc.

Examples of business/industry-specific TLDs: .travel, .tv, .restaurant etc.

Primary Domain (or second-level domain)

The second-highest level domain is called the primary domain.

For example, in the domain name www.optimizesmart.com, ‘optimizesmart’ is the primary domain (without the www prefix).

Similarly, in the domain name www.google.com, ‘google’ is the primary domain.

Subdomain

The third-highest level domain is called the subdomain.

For example, in the domain name www.optimizesmart.com, ‘www’ is the subdomain of the primary domain.

Similarly, in the domain name learn.optimizesmart.com, ‘learn’ is the subdomain of the primary domain.

Sub Subdomain

The fourth highest level domain is called the sub subdomain.

For example, in the domain name js.learn.optimizesmart.com, ‘js’ is the sub subdomain of the primary domain.

Introduction to cookie domains

In the context of Google Analytics, a cookie domain is a domain at which the Google Analytics cookie _ga is set up.

You can set up _ga cookie at a domain by simply installing Google Analytics tracking code on the domain.

Setting up Cookie domain

By default, Google Analytics cookie ‘_ga’ is set up at the second-highest level domain of the current hostname of a web page.

The default Google Analytics tracking code (i.e. the code without any modification done to it for the analytics.js library) looks like the one below:

<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-XXXXX-YY', 'auto');

ga('send', 'pageview');

</script>

 

The same code for gtag.js will look like below

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

           window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

           gtag('js', new Date());

           gtag('config', 'UA-XXXXX-YY');

</script>

If you place the aforementioned Google Analytics Tracking code on a web page whose URL is say: http://music.abc.com/jazz

Then Google Analytics will set up ‘_ga’ cookie at ‘.abc.com’

In the URL: http://music.abc.com/jazz

#1 The current hostname is: ‘music.abc.com

#2 The request URI is: /jazz

#3 Top-level domain (or TLD) is: ‘.com’. Google Analytics can not set up a cookie at the TLD level.

#4 The second-highest level domain (also called the primary domain) is: ‘.abc.com’ (without the ‘www’ prefix). By default, Google Analytics sets up cookies at this level.

#5 The third highest level domain (also called sub domain) is: ‘music.abc.com

Similarly, if you place the aforementioned Google Analytics Tracking code on a web page whose URL says: http://asian.music.abc.com/

Then the second-highest level domain of the current hostname (asian.music.abc.com) will still be ‘.abc.com’

Since, by default, Google Analytics setup cookie at the second-highest level domain of the current hostname, so the _ga cookie will be set at ‘.abc.com’

Now, if you place the aforementioned Google Analytics Tracking code on a web page whose URL is: http://www.abc.com/

Then also Google Analytics will set up ‘_ga’ cookie at ‘.abc.com’ as the second-highest level domain of the current hostname (www.abc.com) is ‘abc.com

Automatic cookie domain configuration: analytics.js

Now let’s look at the default Google Analytics tracking code (i.e. the code without any modification done to it for the analytics.js library) once again:

<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-XXXXX-YY', 'auto');

  ga('send', 'pageview');

</script>

Note that the ‘auto’ configuration parameter used in the tracking code.

This configuration parameter tells Google Analytics to automatically find the second-highest level domain and always set the _ga cookie at the second-highest level domain.

The advantage of setting up the _ga cookie at the second-highest level domain (aka primary domain) is that the client ID will always be shared among the primary domain and its subdomains, whether a user navigates from the primary domain to one of its subdomain or vice versa,  provided:

#1 both primary domain and its subdomains, use the same Google Analytics Tracking Code (in other words, use the same GA property ID)

#2 both the primary domain and their subdomains use the ‘auto’ configuration parameter in their Google Analytics Tracking Code.

So by default, you don’t need to set up cross-domain tracking between primary domains and their subdomains as cookie information is automatically shared between them, provided both primary domain and its subdomains, use the same Google Analytics Tracking Code and use the ‘auto’ configuration parameter in their Google Analytics Tracking Code.

Now, if we remove the ‘auto’ configuration parameter from our Google Analytics Tracking Code (GATC), then the tracking code will look like the one below:

<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-XXXXX-YY');

ga('send', 'pageview');

</script>

Now since the ‘auto’ configuration parameter is missing from the tracking code, Google Analytics will not automatically find the second-highest level domain and may not always set the _ga cookie at the second-highest level domain.

What Google Analytics will now do is, set the _ga cookie at the current level domain, which may or may not be the second-highest level domain.

So if the current level domain is the second-highest level domain, then only _ga cookie will be set at the second-highest level domain, otherwise not.

Without ‘auto’ configuration parameter there is no guarantee that the cookie information will always be shared among the primary domain and its subdomains.

For example, if you use the Google Analytics tracking code without ‘auto’ configuration parameter on a web page whose URL is say: http://music.abc.com/jazz

Then Google Analytics will set up the ‘_ga’ cookie at the current domain level which is  ‘.music.abc.com’ (instead of ‘.abc.com’, the second-highest level domain).

You can share this _ga cookie with subdomains of ‘.music.abc.com’ like ‘asian.music.abc.com’

However, you won’t able to share the ‘_ga’ cookie set up at  ‘music.abc.com’ with a higher level domain like ‘abc.com

So what will happen, when a user navigates from ‘music.abc.com’ to a higher level domain like ‘abc.com’, the client ID won’t pass from ‘music.abc.com’ to ‘abc.com’ and hence a new GA session will start and the same user will be counted as a different user on the ‘abc.com’ website. In other words, cross-domain tracking won’t work.

Now, when you use the Google Analytics tracking code without ‘auto’ configuration parameter on a web page whose URL is: http://www.abc.com/

Then Google Analytics will set up the ‘_ga’ cookie at the current domain level which is now ‘.abc.com’ and you can share this cookie with all of the subdomains like music.abc.com.

So what will happen, when a user navigates from ‘abc.com’ to a lower level domain like ‘music.abc.com’, the client ID will pass from ‘abc.com’ to ‘music.abc.com’ and hence no new GA session will start and the same user will not be counted as a different user on the ‘music.abc.com’ website. In other words, cross-domain tracking will work.

What we can conclude is that, if the ‘auto’ configuration parameter is missing from the Google Analytics tracking code, the client ID will be shared among the primary domain and its subdomains, only when a user navigates from the primary domain to one of its subdomain and both primary domain and its subdomain, use the same Google Analytics Tracking Code.

But when the same user navigates back from the subdomain to its primary domain, the client ID will not be shared and hence new GA session will start and the same user will be counted as two different users.

Similarly, if you use the Google Analytics tracking code without ‘auto’ configuration parameter on a web page whose URL is say: http://games.abc.com/

Then Google Analytics will set up the ‘_ga’ cookie at the current domain level which is now ‘.games.abc.com’.

You can share the ‘_ga’ cookie set up at  ‘.games.abc.com’ with its subdomain like ‘a1.games.abc.com’ but not with any higher-level domain like ‘.abc.com’ or same level domain like ‘music.abc.com

So what will happen, when a user navigates from ‘games.abc.com’ to the same level domain like ‘music.abc.com’, the client ID will not pass from ‘games.abc.com’ to ‘music.abc.com’ and hence new GA session will start and the same user will be counted as a different user on the ‘music.abc.com’ website. In other words, cross-domain tracking will not work between the two subdomains.

So if you want to make sure that cookie information is always passed whether a user navigates from the primary domain to its subdomain and vice versa or whether a user navigates back and forth between two or more subdomains, then use the ‘auto’ configuration parameter in your Google Analytics Tracking Code.

Automatic cookie domain configuration: gtag.js

In the case of the gtag.js library, cookie domain configuration is by default enabled.

When the webpage is loaded, gtag.js will set a cookie on the highest level domain, for example, if the website URL is “http://games.abc.com/” then gtag.js will set a cookie on “abc.com”.

If you are running gtag.js on a locally hosted server, the gtag.js will automatically set the cookie domain to “none”.   

Cookie domain needs to be the ancestor of the destination domain(s)

The cookie domain is the domain at which a Google Analytics cookie is set up.

Destination domain(s) is the domain to which you want to pass the client ID.

If you wish to set up cross-domain tracking in Google Analytics, then always make sure that the cookie domain is the ancestor of the destination domain(s).

A primary domain is always the ancestor of its subdomain(s).

So, if you wish to set up cross-domain tracking between a primary domain like www.abc.com and its subdomain like music.abc.com then set up _ga cookie at the ‘.abc.com’ domain as it is the ancestor of ‘music.abc.com’ subdomain.

You can set up _ga cookie at the ‘.abc.com’ domain by adding the following line of code in the Google Analytics tracking code meant for www.abc.com and music.abc.com:

ga(‘create’, ‘UA-12345-1’, {‘cookieDomain’: ‘.abc.com’}); // use this code if you are using analytics.js library

OR

gtag(‘config’, ‘UA-12345-1’, {‘cookie_domain’: ‘abc.com’}); // use this code if you are using gtag.js library

Similarly, if you wish to set up cross-domain tracking between music.abc.com and asian.music.abc.com then set up _ga cookie either at the ‘music.abc.com’ domain or ‘.abc.com’ domain as both are the ancestor of ‘asian.music.abc.com’ subdomain.

If you wish to set up cross-domain tracking between two subdomains like music.abc.com and games.abc.com, then set up _ga cookie at the primary domain ‘.abc.com’ as it is the ancestor of both ‘music.abc.com’ and games.abc.com subdomains.

Introduction to cookie path

The cookie path is the path at which the cookie is set up.

By default, the _ga cookie is set on the second-highest level domain with the root level (/) path. Because of that reason, the cookie set up at the primary domain level can be shared with all of its web pages and subdirectories.

But if, for some reason, you want the _ga cookie set up at the primary domain level to be shared only with a particular subdirectory, then you can do that by using the ‘cookiePath’ configuration parameter.

For example, let us suppose you want the _ga cookie set up at www.abc.com to be shared only with one of its subdirectory named ‘gin’. To do that, set the ‘cookiePath‘ field to ‘/gin/’:

ga(‘create’, ‘UA-XXXX-Y’, {‘cookiePath’: ‘/gin/’});

Then add this line of code in the Google Analytics Tracking Code meant for www.abc.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','https://www.google-analytics.com/analytics.js','ga');

  ga(‘create’, ‘UA-XXXX-Y’, {‘cookiePath’: ‘/gin/’});

  ga('send', 'pageview');

</script>

Introduction to cookie expires

Cookie Expires is the time in seconds, after which the _ga cookie will expire.

By default, _ga cookie expires after 2 years from the date it was last refreshed.

Every time a new hit is sent to google analytics, the _ga cookie is refreshed.

You can set your own cookie expiration time by using the cookieExpires field:

ga(‘create’, ‘UA-XXXX-Y’, {‘cookieExpires’: 180}); // use this code if you are using analytics.js library

gtag('config', ‘UA-XXXX-Y’, {'cookie_expires': 180}); // use this code if you are using gtag.js library

Here we set the _ga cookie to expire after two minutes.

The cookie expires, play an important role in cross-domain tracking.

For example, if the _ga cookie expires after two minutes, then the same user can be treated as a new user by Google Analytics even before he navigates to a different domain.

Referral exclusion list

A referral exclusion list is the list of domains whose incoming traffic will be treated as direct traffic (instead of referral traffic) by Google Analytics.

SourceHow to correctly use the referral exclusion list in Google Analytics

Direct traffic is a web session (or visit) that starts without a referrer being passed by a user’s browser.

By default, all referrals trigger a new web session in Google Analytics.

This can affect the total number of web sessions in your analytics reports.

For example: let us suppose a user started his journey (GA session) on www.xyz.com website.

He then navigates to www.abc.com website from xyz website. As soon as the user arrived on the abc website, he will be treated as a new user (as client ID is not shared across domains by default), and his existing GA session (with xyz website) will continue, but a new GA session (with abc website) will start.

When the user navigates back to xyz website from abc website, his existing GA session (with abc website) will continue and a new GA session (with xyz website) will start.

Now, if you do not want the new GA session to be triggered for abc website, when the user navigates from xyz website to abc website then you need to add the domain xyz.com in the referral exclusion list of the GA view meant for abc website.

Similarly, if you do not want the new GA session to be triggered for xyz website when the user navigates from abc website to xyz website then you need to add the domain abc.com in the referral exclusion list of the GA view meant for xyz website.

Implementing tracking across a primary domain and its sub-domains

Follow the steps below to track across the primary domain (say abc.com) and its sub-domains (say music.abc.com and games.abc.com):

Step-1: Use the Google Analytics tracking code meant for the primary domain on all of the web pages of the sub-domains.

Step-2: In the Google Analytics view of the primary domain (abc.com), add ‘abc.com’ in the referral exclusion list so that it automatically puts all the subdomains of abc.com in the referral exclusion list.

Step-3: In the Google Analytics view of the sub-domain: ‘music.abc.com’, add ‘abc.com’ in the referral exclusion list.

Step-4: In the Google Analytics view of the sub-domain: ‘games.abc.com’, add ‘abc.com’ in the referral exclusion list.

Requirements for setting up cross-domain tracking between primary domains

Before you can implement cross-domain tracking between two or more primary domains, you need to get familiar with the following concepts:

  1. Cross-Domain Links
  2. allowLinker
  3. autoLink plugin
  4. linker:autoLink Command
  5. Linker Parameter
  6. Decorating links

Introduction to cross-domain links

A cross-domain link is a hyperlink that can move the traffic from one domain to another.

In cross-domain tracking, a user’s client ID is passed from one domain to another via cross-domain links and/or via form submission.

For example, if you have set up cross-domain tracking between abc.com and xyz.com then all the links on abc.com that go to xyz.com and all the links on xyz.com that go to abc.com, are called cross-domain links.

Cross-domain traffic is the traffic that moves back and forth between different domains via cross-domain links.

The cookie information (Client ID) is attached to the query string of the cross-domain links which you can see in the browser window:

Google Analytics tag is designed to look for client ID in the query string and use it if it exists. So if a user has got the same client ID across two or more domains, then Google Analytics can identify the user across multiple domains.

In order to track a user across domains, you need to share users’ cookie information (Client ID) among domains. The easiest way to do that is by using the autoLink plugin.

Introduction to ‘allowLinker’

If you are using analytics.js ‘allowLinker’ is a tracker configuration parameter that must be set to true to allow a domain to pass/accept client ID.

In the case of cross-domain tracking, we have got two categories of primary domains:

1) Source domain

2) Destination domain(s)

The default value for allowLinker is ‘false’. So by default:

#1 A source domain is not allowed to pass the client ID to the destination domain.

#2 A destination domain is not allowed to accept the client ID from the source domain.

You can set up cross-domain tracking between a source domain and a destination domain only when the following two conditions are met:

#1 The source domain is allowed to pass the client ID to the destination domain.

#2 The destination domain is allowed to accept the client ID from the source domain.

If you are using gtag.js, then you don’t need the ‘allowlinker’ parameter. In the gtag.js, the ‘linker’ parameter is used along with a list of domains to pass client ID. To accept the client ID on the destination domain, in gtag.js you need to pass the command ‘accept_incoming’: true after the linker parameter. You don’t need any additional parameters than this in the case of gtag.js

#FAQ: How you can allow the source domain to pass client ID to the destination domain?

For Analytics.js:

Add the following line of code in the GATC (Google Analytics Tracking Code) of the source domain:

ga('create', 'UA-XXXXX-YY', 'auto',{'allowLinker': true }); // allow source domain to pass cookie information

So the final code may look like the one below:

 <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-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

.

.

ga('send', 'pageview');

</script>

 

For gtag.js

Add the following line of code in the GATC (Google Analytics Tracking Code) of the source domain:

            gtag('config', 'UA-XXXX-YY, {

          'linker': {

             'domains': ['abc.com']

  }

});

So, the final code may look like the one below:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

          gtag('config', 'UA-1556844-26', {

          'linker': {

          'domains': ['abc.com']

  }

});

</script>

#FAQ: How you can allow the destination domain to accept client ID from the source domain?

For Analytics.js

Add the following line of code in the GATC of the destination domain:

ga('create', 'UA-XXXX-YY', 'auto',{'allowLinker': true }); // allow destination domain to accept cookie information.

 So the final code may look like the one below:

<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-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

.

.

ga('send', 'pageview');

</script>

For gtag.js

Add the following line of code in the GATC of the destination domain:

gtag('config', 'UA-XXXX-YY', {

  'linker': {

             'accept_incoming': true

  }

});

So the final code may look like the one below:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

          gtag('config', 'UA-1556844-27', {

  'linker': {

             'accept_incoming': true

           }

});

</script>

Introduction to ‘autoLink’ plugin

autoLink plugin is a Google Analytics plugin that is used to share client ID among two or more primary domains. This plugin is required if you are using the analytics.js library.

This plugin provides the easiest way to implement cross-domain tracking in Google Analytics. Google recommends implementing cross-domain tracking via the autoLink plugin.

You can also implement cross-domain tracking without using this plugin, i.e. manually, but it is more prone to errors.

The word ‘autoLink’ means ‘automatic linking’.

This plugin automatically implements cross-domain linking (not to be confused with the word ‘cross-domain tracking’) between two or more primary domains.

In order to set up this plugin, you need to add the plugin code to your Google Analytics tracking code.

Following the line of code is used to load the ‘autoLink’ plugin:

 ga('require', 'linker');

You need to add this line of code to the Google Analytics tracking code of both your source and destination domains.

So the final Google Analytics tracking code for the source domain may look like the one below:

<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-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

.

ga('send', 'pageview');

</script>

Similarly, the final Google Analytics tracking code for the destination domain may look like the one below:

<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-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

.

ga('send', 'pageview');

</script>

#FAQ: Is it required to set up the autolink plugin on the destination domain(s)?

No, it is not required, but you should, in order to implement bi-directional cross-domain tracking. So that you can track traffic from Website A going to Website B and vice versa.

Note: Your website pages can block the ‘autoLink plugin’ from working or working correctly. So, test the plugin on one page before implementing it sitewide.

Introduction to ‘linker:autoLink’ command

The purpose of the linker:autoLink command is to define the domain(s) that need to be automatically cross-linked.

In GA lingo, ‘automatically cross-linked’ is known as ‘auto-linked’.

There are two methods through which you can define the domains that need to be automatically cross-linked while setting up the autoLink plugin:

1) by using an array of domain substrings.

2) by using an array of domain regular expressions.

 

Array of domain substrings

Following are examples of array of domain substrings:

#1 [‘destination.com’]

#2 [‘destination1.com’, ‘destination2.com’]

#3 [‘destination1.com’, ‘destination2.com’,’destination3.com’]

Here ‘destination.com’, ‘destination1.com’, ‘destination2.com’, ‘destination3.com’ are the domains that need to be auto-linked.

For example:

ga(‘linker:autoLink’, [‘xyz.com’]); => autolink the source domain with ‘xyz.com’ destination domain.

ga(‘linker:autoLink’, [‘xyz.com’, ‘pqr.com’]); => autolink the source domain with ‘xyz.com’ and ‘pqr.com’ destination domains.

ga(‘linker:autoLink’, [‘xyz.com’, ‘pqr.com’,’drt.net’]); => autolink the source domain with ‘xyz.com’, ‘pqr.com’ and ‘drt.net’ destination domains.

The array of domain regular expressions

Following is an example of an array of domain regular expression:

[/^example\.(com|net)$/]

Here example.com and example.net are the domains that need to be auto-linked.

For example:

ga('linker:autoLink', [/^example\.(com|net)$/]);

If www.xyz.com is your source domain and www.pqr.com is your destination domain, and you want to set up cross-domain tracking between the two domains, then your Google Analytics tracking code for the source domain may look like the one below:

<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-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

ga('linker:autoLink', ['pqr.com’]);

ga('send', 'pageview');

</script>

Similarly, your Google Analytics tracking code for the destination domain (pqr.com) may look like the one below:

<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-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

ga('linker:autoLink', ['xyz.com’]);

ga('send', 'pageview');

</script>

Introduction to linker parameters

A linker parameter is a query parameter that is attached to a cross-domain link, once it is clicked.

For example, in the following destination domain URL:

https://www.destination-1.com/productA.php?_ga=1.153530484435.69385205830.8479429413352

Here the linker parameter is: _ga=1.153530484435.69385205830.8479429413352

Introduction to decorating links

Decorating a link means adding a linker parameter to the link.

Following is an example of an undecorated link:

https://www.destination-1.com/productA.php

Following is an example of a decorated link:

https://www.destination-1.com/productA.php?_ga=1.153530484435.69385205830.8479429413352

Note: You can decorate links manually or through the autolink plugin. 

Cross-domain tracking code

The cross-domain tracking code in Google Analytics is made up of:

For Analytic.js

#1 ‘allowLinker’ tracker configuration parameter

#2  ‘autoLink’ Plugin

#3 ‘linker:autoLink’ Command

Following is an example of a cross-domain tracking code in GA:

ga('create', 'UA-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

ga('linker:autoLink', ['xyz.com’]);

This cross-domain tracking code is added to the Google Analytics tracking code of the source and destination domains.

For gtag.js

The cross-domain tracking code for the gtag.js library is made up of

#1 Linker Parameter

#2 Domain List

#3 accept_incoming command

Implementing cross-domain tracking between two primary domains

Using analytics.js

The fundamental requirement for tracking across primary domains is that you need to share client ID with the destination domain, and the destination domain must give permission to do that.

For the purpose of giving an example, let us suppose we need to set up cross-domain tracking between xyz.com and pqr.com, where xyz.com is the source domain, and pqr.com is the destination domain.

Follow the steps below to track across two primary domains:

Step-1: In the GA view of xyz.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘xyz.com’ and ‘pqr.com’ to the ‘referral exclusion’ list. xyz.com is added to avoid self-referral issues

Step-2: In the GA view of pqr.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘pqr.com’  and ‘xyz.com’ to the ‘referral exclusion’ list. pqr.com is added to avoid self-referral issues.

Step-3: Create a cross-domain tracking code for your source domain (xyz.com) according to your understanding of:

#1 ‘allowLinker’ tracker configuration parameter

#2  ‘autoLink’ Plugin

#3 ‘linker:autoLink’ Command

The cross-domain tracking code for your source domain (xyz.com) may look like the one below:

ga('create', 'UA-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', ['pqr.com']); // auto link to pqr website

Step-4: Create a cross-domain tracking code for your destination domain (pqr.com)

ga('create', 'UA-1556844-76', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup linker plugin

 ga('linker:autoLink', ['xyz.com']); // link to xyz website

Step-5: Add the cross-domain tracking code meant for your source domain (xyz.com) to the Google Analytics tracking code of your source domain (xyz.com). For example:

<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-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', ['pqr.com']); // auto link to pqr website

ga('send', 'pageview');

</script>

Step-6: Add the cross-domain tracking code meant for your destination domain (pqr.com) to the Google Analytics tracking code of your destination domain (pqr.com).

For example:

<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-1556844-76', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup linker plugin

 ga('linker:autoLink', ['xyz.com']); // link to xyz website

ga('send', 'pageview');

</script>

Using gtag.js

Follow the steps below, to track across two primary domains:

Step-1: In the GA view of xyz.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘xyz.com’ and ‘pqr.com’ in the ‘referral exclusion’ list. xyz.com is added to avoid self-referral issues

Step-2: In the GA view of pqr.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘pqr.com’  and ‘xyz.com’ in the ‘referral exclusion’ list. pqr.com is added to avoid self-referral issues.

Step-3: Create a cross-domain tracking code for your source domain (xyz.com)

gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': ['pqr.com']

    ‘accept_incoming’: true

  }

});

Step-4: Create a cross-domain tracking code for your source domain (pqr.com)

gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': ['xyz.com']

    ‘accept_incoming’: true

  }

});

Step-5: Add the cross-domain tracking code meant for your source domain (xyz.com) to the Google Analytics tracking code of your source domain (xyz.com). For example:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': ['pqr.com']

    ‘accept_incoming’: true

           }

});

</script>

Step-6: Add the cross-domain tracking code meant for your destination domain (pqr.com) to the Google Analytics tracking code of your destination domain (pqr.com).

For example:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  gtag('config', 'UA-1556844-76', {

  'linker': {

    'domains': ['xyz.com']

    ‘accept_incoming’: true

           }

});

</script>

Implementing cross-domain tracking between three primary domains

Using analytics.js

If you want to set up cross-domain tracking between 3 primary domains say: abc.com, xyz.com and pqr.com, then follow the steps below:

Step-1: In the GA view of abc.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’ and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-2: In the GA view of xyz.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’ and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-3: In the GA view of pqr.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’ and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-4: Create a cross-domain tracking code for the domain abc.com.This code may look like the one below:

ga('create', 'UA-1556844-25', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', [‘xyz.com’,'pqr.com']); // auto link to ‘xyz’ and ‘pqr’ websites.

Step-5: Create a cross-domain tracking code for the domain xyz.com. This code may look like the one below:

ga('create', 'UA-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', [‘abc.com’,'pqr.com']); // auto link to ‘abc’ and ‘pqr’ websites.

Step-6: Create cross-domain tracking for the domain pqr.com. This code may look like the one below:

ga('create', 'UA-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

ga('linker:autoLink', [‘abc.com’,'xyz.com']); // auto link to ‘abc’ and ‘xyz’ websites

Step-7: Add the cross-domain tracking code meant for the domain abc.com to the Google Analytics tracking code of the domain abc.com. For example:

<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-1556844-25', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', [‘xyz.com’,'pqr.com']); // auto link to ‘xyz’ and ‘pqr’ websites.

ga('send', 'pageview');

</script>

Step-8: Add the cross-domain tracking code meant for the domain xyz.com to the Google Analytics tracking code of the domain xyz.com. For example:

<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-1556844-26', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', [‘abc.com’,'pqr.com']); // auto link to ‘abc’ and ‘pqr’ websites.

ga('send', 'pageview');

</script>

Step-9: Add the cross-domain tracking code meant for the domain pqr.com to the Google Analytics tracking code of the domain pqr.com. For example:

<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-1556844-27', 'auto',{'allowLinker': true }); // set allowLinker to true

 ga('require', 'linker'); // setup autoLink plugin

 ga('linker:autoLink', [‘abc.com’,'xyz.com']); // auto link to ‘abc’ and ‘xyz’ websites.

ga('send', 'pageview');

</script>

Using gtag.js

If you want to set up cross-domain tracking between 3 primary domains say: abc.com, xyz.com, and pqr.com, then follow the steps below:

Step-1: In the GA view of abc.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’, and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-2: In the GA view of xyz.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’, and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-3: In the GA view of pqr.com, go to Admin > Property > Tracking Info > Referral exclusion list. Add ‘abc.com’, ‘xyz.com’, and ‘pqr.com’ in the ‘referral exclusion’ list.

Step-4: Create a cross-domain tracking code for the domain abc.com.This code may look like the one below:

gtag('config', 'UA-1556844-25', {

  'linker': {

    'domains': [‘xyz.com’,'pqr.com']

    ‘accept_incoming’: true

  }

});

Step-5: Create a cross-domain tracking code for the domain xyz.com. This code may look like the one below:

gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': [‘xyz.com’,'pqr.com']

    ‘accept_incoming’: true

  }

});

Step-6: Create cross-domain tracking for the domain pqr.com. This code may look like the one below:

gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': [‘xyz.com’,'abc.com']

    ‘accept_incoming’: true

  }

});

Step-7: Add the cross-domain tracking code meant for the domain abc.com to the Google Analytics tracking code of the domain abc.com. For example:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  gtag('config', 'UA-1556844-25', {

  'linker': {

    'domains': [‘xyz.com’,'pqr.com']

    ‘accept_incoming’: true

           }

});

</script>

Step-8: Add the cross-domain tracking code meant for the domain xyz.com to the Google Analytics tracking code of the domain xyz.com. For example:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  gtag('config', 'UA-1556844-26', {

  'linker': {

    'domains': [‘abc.com’,'pqr.com']

    ‘accept_incoming’: true

           }

});

Step-9: Add the cross-domain tracking code meant for the domain pqr.com to the Google Analytics tracking code of the domain pqr.com. For example:

<!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  gtag('config', 'UA-1556844-27', {

  'linker': {

    'domains': [‘abc.com’,'xyz.com']

    ‘accept_incoming’: true

           }

});

Tracking across top-level domains

Top-level domain (or TLD) is the last part of the domain name. For example, in the domain name www.abc.com, the top-level domain is ‘com’. Similarly, in the domain name www.abc.org, the top-level domain is ‘org’.

Tracking across top-level domains is similar to tracking across different primary domains.

Using the same Google Analytics tracking code on multiple primary domains for cross-domain tracking

Wouldn’t it be easy to track across multiple primary domains, just by using the same GA tracking code on all of the primary domains?

Yes, that would be the easiest solution, but it comes with its own set of critical issues which can not be fixed:

Issue #1: Self-referrals – expect a significant level of self-referral issues in your GA reports if you used the same GA tracking code on multiple websites. Even if you exclude all of the domains that are part of the cross-domain tracking set up, from referral traffic (via referral exclusion list), it will still not fix your problem.

Then Google Analytics will treat the self-referral traffic as direct traffic. So you could then end up with an abnormally large amount of direct traffic in your GA reports.

Issue #2: Inaccurate data – a significant level of self-referral issues would greatly skew your analytics data and any decision based on inaccurate data can not produce optimum results. Since by default all referrals trigger a new web session in Google analytics, expect inflated sessions data.

Issues #3: Attribution issues – you may have a hard time determining the original source of your conversions and/or ecommerce transaction. The majority of your conversions could end up being attributed to direct traffic or your own website (self-referral).

Cross-domain tracking and roll-up reporting

Cross-domain tracking and roll-up reporting are not the same things. Just because you have implemented cross-domain tracking does not mean that you will automatically see the cross-domain traffic data in your GA reports.

Roll-up reporting is the reporting of data in an aggregated form from multiple digital properties (websites, mobile apps).

For example, if you have set up separate websites for each country, then through roll-up reporting, you can aggregate all of your website’s data in one view and see aggregated global performance metrics and/or compare the performance of various country-specific websites to each other.

In Google Analytics, a website or mobile application is called a property (also known as web property or digital property). Each property is identified by a unique ID known as property ID (or tracking ID).

This tracking ID is in the format: UA-XXXX-YY

For example: UA-1235-21

Each property contains one or more views, and each view contains multiple reports:

A roll-up property is the web property whose hit data comes from other Google Analytics properties.

If you have implemented roll-up reporting then with the help of multiple trackers, you can get cross-domain traffic in your roll-up property:

This is a bit advanced cross-domain tracking set up, which I explained in great detail in this article: Understanding trackers in Google Analytics.

To learn more about roll-up reporting, read this article: Implementing roll-up reporting in Google Analytics.

This post is second in the series of frequently asked questions about various issues in Google Analytics.

The first post in the series was How Google Analytics Uses Cookies which talks about Google Analytics cookies in great detail.

Google Analytics Cross-Domain Tracking (ga.js)

In order to track across domains, you would need to share cookie information among domains. You can do that by using one or all of the following methods in your tracking code:

_setDomainName() – This method is called in Google Analytics Tracking Code
_setAllowLinker()– This method is called in Google Analytics Tracking Code
_Link() – This method is called through javascript handler
_LinkByPost() – This method is called through javascript handler

Code for tracking a primary domain

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

This is the default Google Analytics tracking code. Replace ‘UA-XXXX-Y’ with your account number and then copy-paste this code in the head section (<head> ….</head> of all the web pages of your primary domain.

Note: You can also get this tracking code by clicking on Admin > Tracking Info in your Google Analytics account.

Code for tracking across a primary domain and its sub-domains

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_setDomainName', 'yourwebsite.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

This code is also known as the sub-domain tracking code. Replace ‘UA-XXXX-Y’ with your account number and ‘yourwebsite.com’ with your primary domain name. Now copy-paste this code in the head section (<head> ….</head> of all the web pages of your primary domain and all the web pages of your sub-domains.

Note: You can also get this tracking code by clicking on Admin > Tracking Info in your Google Analytics account and then clicking on the ‘on’ button next to the ‘subdomains’ row:

Code for tracking across a primary domain and its top-level domains

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_setDomainName', 'yourwebsite.com']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

This code is also known as the top-level domain tracking code. Replace ‘UA-XXXX-Y’ with your account number and ‘yourwebsite.com’ with your primary domain name. Now copy-paste this code in the head section (<head> ….</head> of all the web pages of your primary domain and all the web pages of your top-level domains.

Note: You can also get this tracking code by clicking on Admin > Tracking Info in your Google Analytics account and then clicking on the ‘on’ button next to the ‘top-level domains’ row:

Code for tracking across multiple primary domains

The first thing that you need to remember is that Google Analytics doesn’t provide any tracking code to track across multiple domains through its interface.

You need to manually modify the Google Analytics Tracking code of all the web pages of each domain. If you are using template files, then it will take only a couple of minutes. If you are not using template files, then you need to manually modify the tracking code of all the web pages of each domain.

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Replace ‘UA-XXXX-Y’ with your account number. Now copy-paste this code in the head section (<head> ….</head> of all the web pages of each of your primary domain.

Note: You also need to use the _link() or _linkByPost() method (as described later in this post) to enable tracking across multiple primary domains.

What is _setDomainName() ?

This method is used to share cookie information among:

  1. Multiple domains
  2. Multiple domains with multiple sub-domains
  3. Primary domains and their sub-domains
  4. Primary domains and their top-level domains

Syntax:

When you want to share cookie information among multiple domains or primary domain and its top-level domains, then the syntax of _setDomainName() is:

_gaq.push([‘_setDomainName’,’none’]);

When you want to share cookie information among multiple domains with multiple sub-domains or primary domain and its sub-domains, then the syntax of _setDomainName() is:

_gaq.push([‘_setDomainName’,’.yourwebsite.com’]);

Here ‘yourwebsite.com’ is the name of your website, and the _setDomainName() is used to set the primary domain name.  For example:

_gaq.push([‘_setDomainName’,’.optimizesmart.com’]);

Here I am setting the primary domain name to optimizesmart.com

What is _setAllowLinker() ?

This method is used to share cookie information among:

  1. Multiple domains
  2. Multiple domains with multiple sub-domains
  3. Primary domains and their top-level domains

Syntax: _gaq.push([‘_setAllowLinker’,true]);

What is_link() ?

This method is used to share cookie information among:

  1. Multiple domains
  2. Multiple domains with multiple sub-domains

If you transfer visitors from one domain to another via links, then add the _link() method to all the links between the domains.

Syntax: _gaq.push([‘_link’,’URL parameter’]);

Example:

<a href=”https://www.eventplannigforum.net” onClick=”_ gaq.push([‘_link’, ‘https://www.eventplanningforum.net’]); return false;”>Event Planning Forum</a>

<a href=”https://www.eventeducation.com” onClick=”_ gaq.push([‘_link’, ‘https://www.eventeducation.com’]); return false;”>Event Education</a>

How exactly does the _link() method send cookie information to other domains?

The _link() method sends cookie information via ‘URL parameter’

What is _linkBypost() ?

This method is used to share cookie information among:

  1. Multiple domains
  2. Multiple domains with multiple sub-domains

If you are transferring visitors from one domain to another via forms, then add the _linkByPost() method to all the forms between the domains.

Syntax: _ gaq.push([‘_linkByPost’,this]);

Example:

<form action=”processing.php” name=”lead-form” method=”post” onSubmit=”_ gaq.push([‘_linkByPost’,this]);”>

….

</form>

How exactly does the _linkByPost() method send cookie information to other domains?

The _linkByPost() method sends cookie information via form action.

When is traffic from a sub-domain treated as referral traffic by Google Analytics?

When the web pages of the sub-domain contain the Basic Google Analytics tracking code (GATC) instead of the ‘sub-domain Google Analytics tracking code’.

 When my main website profile also includes traffic from its sub-domains (which is not treated as referral traffic)?

This happens when you add ‘sub-domain GATC’ to all the web pages of the sub-domains.

What is the disadvantage of tracking across sub-domains using a single website profile, and how can this disadvantage be overcome?

Google Analytics may consolidate the visits from identically named pages of different sub-domains and show it as one in its reports. For example:

Let’s say visits from https://fun.abc.com/home.html  is              20

Let’s say visits from https://music.abc.com/home.html is           70

Google Analytics may consolidate the visits from home.html of the two sub-domains and show it like

/home.html            90

This disadvantage can be overcome by appending hostname to the request URI using Google Advanced filters:

Note: In Request URI, the URI stands for universal resource indicator. It is the path part of a URL. For example: in the URL: https://www.abc.com/fun/music/index.php, the request URI is ‘/fun/music/index.php’.

Once you applied this filter to the main website profile, Google analytics will show the report as:

Fun.abc.com/home.html         20

Music.abc.com/home.html      70

Note: By default, Google Analytics displays only the request URI.

What are the best practices for tracking across subdomains?

If you want to track all the sub-domains through a single website profile

Append hostname to the request URI by applying an advanced filter to the main website profile.

If you want to track each sub-domain through a different website profile

Duplicate your main website profile for each sub-domain and then apply ‘include the only filter’ to include only the traffic from a particular sub-domain. For example:

Note: All the profiles have the same profile number (the number after the second hyphen), that is ‘1’, which shows they all are basically the same profile. But the last two profiles are using filters, so they are getting only the filtered data.

Related Article: Google Analytics Subdomain Tracking Tutorial

When should you not add sub-domain Google Analytics tracking code to all the web pages of a sub-domain?

When you want to treat a sub-domain as a different website or do not want to include the sub-domain traffic in your main website profile.

Should you use _link() and _linkByPost() methods for links and forms between the main domain and its sub-domain?

No. These methods are required only in the case of tracking across multiple primary domains or multiple primary domains with multiple sub-domains.

Another article on cross-domain tracking – How to set up cross-domain tracking in Google Analytics 4

Testing Cross-domain tracking in Google Analytics

In order to troubleshoot cross-domain tracking in Google Analytics, you need to make sure that the following requirements are met:

  • Check if both domains are validated under the same property.
  • Validate source domain in the referral exclusion list in Google Analytics
  • Check if the allow linker in GTM is set to true
  • The target domain should have a linker parameter in the URL.

To learn more about testing cross-domain tracking in Google Analytics, check out this article: How to check cross-domain tracking in Google Analytics

Articles on specialized tracking in Google Analytics

  1. Google Analytics Ecommerce Tracking Tutorial
  2. Google Tag Manager Event Tracking Tutorial
  3. Google Analytics Event Tracking Tutorial
  4. Google Analytics Store Visits Tracking Tutorial
  5. Offline Conversion Tracking in Google Analytics – Tutorial
  6. Ecommerce Tracking Google Tag Manager (GTM) – Tutorial
  7. Tracking Virtual Pageviews in Google Tag Manager – Tutorial
  8. Google Tag Manager YouTube Video Tracking
  9. How to unlock not provided keywords in Google Analytics?
  10. Google Analytics Virtual Pageviews Tutorial
  11. Google Analytics and YouTube Integration Tutorial
  12. Google Analytics for Facebook Tutorial
  13. Cross Domain Tracking in Google Analytics – Complete Guide
  14. How to use two Google Analytics codes on one page
  15. The one thing that you don’t know about PayPal.com and the referral exclusion list
  16. Google Analytics Calculated Metrics – Tutorial
  17. Creating your own Google Analytics Tag Auditing System
  18. Google Tag Manager Search Tracking without Query Parameter
  19. Tracking Google Analytics Paypal Referral and other payment gateways
  20. How to Track Phone Calls in Google Analytics – Call Tracking Tutorial
  21. How to track leads in Google Analytics via CRM
  22. Postbacks in Google Analytics Explained
  23. Subscription & Recurring Revenue Analytics in Google Analytics
  24. Track the Impact of Google Analytics Cookie Consent on Website Traffic
  25. Tracking Offline Conversions in Google Ads
  26. Implementing Scroll Tracking via Google Tag Manager
  27. Scroll Depth Tracking in Google Tag Manager – Tutorial
  28. Site Search Tracking In Google Analytics Without Query Parameters
  29. Google Tag Manager Youtube Video Tracking via YouTube Video Trigger
  30. How to Correctly Measure Conversion Date & Time in Google Analytics
  31. Google Analytics Social Tracking – Twitter, Facebook, Google Plus and LinkedIn
  32. Google Analytics Linkedin & Twitter Tracking
  33. Creating Content Group in Google Analytics via tracking code using gtag.js
  34. Google Analytics Site Search Tracking via Query Parameters
  35. Google Analytics Site Search Tracking Tutorial
  36. Creating and Using Site Search Funnel in Google Analytics
  37. Learn to Setup Facebook Pixel Tracking via Google Tag Manager
  38. AMP Google Analytics Tracking – Learn to track AMP pages
  39. Setting up Sales Funnel across websites in Google Analytics
  40. Regex Google Analytics & Google Tag Manager – Tutorial

Frequently Asked Questions About Cross Domain Tracking in Google Analytics

What is cross-domain tracking in Google Analytics?

In the context of Google Analytics, cross-domain tracking is the tracking of website users and their activities across two or more domains.

Does Google Analytics support cross-domain tracking by default?

By default, Google Analytics is not able to track website users and their activities across multiple domains. That is because Google Analytics uses first-party cookies which can be read by only that domain which issued it. You would need to manually set up cross-domain tracking.

Why do you need cross-domain tracking?

Without cross-domain tracking set-up, you won’t be able to understand and track a user’s journey which spans across multiple domains.

Without cross-domain tracking set up, Google Analytics will report the one user who visited your site abc.com and your shopping cart xyz.com/abc-cart as two different users. Plus, the user session that actually spans across the two domains will be counted as two different GA sessions instead of a single session.

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."



Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.



   

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade
error: Alert: Content is protected !!