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 I need to track across multiple domains?
- When your website checkout process occurs on a different domain (quite common in case of affiliate websites)
- 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 I can make Google Analytics track across multiple domains?
Share cookie information among multiple domains through following methods:
- _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 (like onClick, onSubmit)
- _LinkByPost() – This method is called through javascript handler
How I can make Google Analytics track across multiple domains with multiple sub-domains?
Share cookie information among multiple domains with multiple sub-domains through following methods:
- _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
How I can make Google Analytics track across primary domain and its sub-domains?
Share cookie information between primary domain and its sub-domain through _setDomainName() method.
How I can make Google Analytics track across primary domains and its top level domains?
Share cookie information between primary domain and its top level domains through _setDomainName() and _setAllowLinker() methods.
Which method is commonly used in any type of tracking across domains or sub-domains?
_setDomainName() method.
What is _setDomainName() ?
This method is used to share cookie information among:
- Multiple domains
- Multiple domains with multiple sub-domains
- Primary domains and its sub domains
- 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’,’.seotakeaways.com’]);
Here I am setting the primary domain name to seotakeaways.com
What is _setAllowLinker() ?
This method is used to share cookie information among:
- Multiple domains
- Multiple domains with multiple sub-domains
- Primary domains and its top level domains
Syntax: _gaq.push([‘_setAllowLinker’,true]);
What is_link()?
This method is used to share cookie information among:
- Multiple domains
- 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:
- Multiple domains
- 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’
What are different types of Google Analytics Tracking Code?
There are four different types of Google Analytics Tracking code:
1. Single Domain Tracking Code – It is the default Google Analytics tracking code and is used to track a single domain.
2. Multiple Sub-Domains Tracking Code – It is used to track one or more sub-domains of a primary domain. This tracking code contains one additional line of code in the basic GATC immediately after the _setAccount() method
_gaq.push([‘_setDomainName’, ‘YourSite.com’]);
3. Multiple Top Level Domains Tracking Code – It is used to track one or more top level domains of a primary domain. This tracking code contains two additional line of code in the basic GATC immediately after the _setAccount() method:
_gaq.push([‘_setDomainName’, ‘YourSite.com’]);
_gaq.push([‘_setAllowLinker’, true]);
4. Mobile Tracking code – This tracking code is used to track traffic on mobile websites. Add this tracking code to all the web pages of your mobile website. The code varies from one scripting language to another which is used to build the mobile website. For example there is a different mobile GATC for PHP, Perl, JSP and ASPX.
Note: Basic GATC, Multiple Sub Domains GATC and Multiple Top Level Domains GATC are also known as Desktop tracking codes.
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 advantage 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: 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 I 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 domains or multiple domains with multiple sub-domains.
What steps I should follow to track across multiple domains?
Step-1: 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. Add following lines of code immediately after the _setAccount() method to your basic Google Analytics tracking code:
_gaq.push([‘_setDomainName’, ‘none]);
_gaq.push([‘_setAllowLinker’, true]);
The final tracking code will look something like this:
<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-1023456-1’]);
_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’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Note: Use your web property ID (‘UA-1023456-1′) in this tracking code.
Step-2: Use _link() or _linkByPost() method as described above in the post wherever you transfer visitors from one domain to another via external links or forms.
Note: If you skip step-2 then your cross domain tracking won’t work.
What steps I should follow to track across multiple domains with multiple sub-domains?
Step-1: Again the first thing that you need to remember is that Google Analytics doesn’t provide any tracking code to track across multiple domains with multiple sub-domains through its interface.
You need to manually modify the Google Analytics Tracking code of all the web pages of each domain and its sub-domains. Add following lines of codes immediately after the _setAccount() to your basic Google Analytics tracking code of all the web pages of the first domain (say domain1.com) and all of its sub-domains:
_gaq.push([‘_setDomainName’, ‘domain1.com]);
_gaq.push([‘_setAllowLinker’, true]);
Note: replace domain1.com with your website name.
The final tracking code will look something like this:
<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-1023456-1’]);
_gaq.push([‘_setDomainName’, ‘domain1.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’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Note: Use your web property ID in this tracking code.
Step-2: Add following lines of codes immediately after the _setAccount() to your basic Google Analytics tracking code of all the web pages of the second domain (say domain2.com) and all of its sub-domains:
_gaq.push([‘_setDomainName’, ‘domain2.com]);
_gaq.push([‘_setAllowLinker’, true]);
Note: replace domain2.com with your second website name.
Step-3: Use _link() or _linkByPost() method as described above in the post wherever you transfer visitors from one domain to another via external links or forms.
Note: If you skip step-3 then your cross domain tracking won’t work.
Step-4: Repeat steps 2 and 3 to modify the GATC of all other domains and its sub-domains.
Quick Recap of different types of tracking in Google Analytics
1. If you want to track traffic of your mobile website then just copy paste the Mobile GATC on all the web pages of your mobile site. This code is provided by Google Anaytics.
2. If you want to track only a single domain then copy paste the basic GATC (which is also the default tracking code of Google Analytics) on all the web pages of your website. This code is provided by Google Anaytics.
3. If you want to track one domain with multiple sub-domains then copy paste the ‘One domain with multiple subdomains’ tracking code provided by Google Analytics to all the web pages of your main domain and all of its sub-domains.
4. If you want to track one domain with multiple top level domains then copy paste the ‘multiple top level domains’ tracking code provided by Google Analytics to all the web pages of your main domain and all of its top level domains.
5. TRICKY– Tracking across multiple domains or multiple domains with multiple sub-domains is not straightforward and you need to customize your Google Analytics Tracking code as described in the post above.