Google Analytics Cross Domain Tracking – Complete Guide

 

This post is second in the series of frequently asked questions about various issues in Google Analytics.  Instead of replying to the individual emails I thought of addressing all the issues in one go and at the same time share my understanding of Google analytics with others. The first post in the series was How Google Analytics Uses Cookies which talks about Google Analytics cookies in great details.

When you need to track across multiple domains?

  1. When your website checkout process occurs on a different domain (quite common in case of affiliate websites)
  2. When your web session spans across multiple domains

 

Why Google Analytics can’t track across multiple domains, sub domains or top level domains by default?

This is because Google Analytics uses first party cookies which can be read by only that domain (website) which issued it.

Note: In the domain name www.abc.com, the top level domain (or TLD) is .com, the second level domain (also known as primary domain) is ‘abc’ and the sub domain is ‘www’.

 

How you can track across domains?

In order to track across domains you 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

 This is the default Google Analytics Tracking Code. Replace ‘UA-XXXX-Y’ by 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 primary domain and its sub-domains

This code is also known as the sub-domain tracking code. Replace ‘UA-XXXX-Y’  by your account number and ‘yourwebsite.com’ by 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:

subdomains-tracking

 

Code for tracking across primary domain and its top level domains

This code is also known as the top level domain tracking code. Replace ‘UA-XXXX-Y’  by your account number and ‘yourwebsite.com’ by 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:

tld-tracking

 

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 couple of minutes. If you are not using template files than you need to manually modify tacking code of all the web pages of each domain.

Replace ‘UA-XXXX-Y’  by 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 its sub domains
  4. Primary domains and its top level domains

Syntax:

When you want to share cookie information among multiple domains or primary domain and its top level domains, then 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 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 its 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 _link() method to all the links between the domains.

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

Example:

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

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

 

How exactly _link() method sends 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 _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 _linkByPost() method sends cookie information to other domains?

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

 

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

When the web pages of the sub-domain contain the Basic Google Analytic 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 page of the sub-domains.

 

What is the disadvantage of tracking across sub domains using single website profile and how this disadvantage can 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 http://fun.abc.com/home.html  is              20

Let’s say visits from http://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 overcomed 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: http://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 sub-domains?

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

Append hostname to the request URI by applying 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 only filter’ to include only the traffic from a particular sub-domain. For example:

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

 

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

When you want to treat 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 a 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.

Next article in this series: Cross Domain Tracking in Universal Analytics – demystified

Other Posts you may find useful:

 

Subscribe to my blog
Join my free newsletter and learn to avoid the analytics mistakes everyone seems to be making over and over again.

 



Subscribe to my YouTube Channel > > >

 

About the Author:

Please feel free to endorse/forward my LinkedIn Profile to your clients, colleagues, friends and others you feel would benefit from PPC, Web Analytics or Conversion Optimization.

For any question/comment Contact me