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’,’.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:

  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.

Other Posts you may find useful:

 

 

Himanshu Sharma About the Author: is the founder of seotakeaways.com which provides SEO Consulting, PPC Management and Analytics Consulting services to medium and large size businesses. He holds a bachelors degree in ‘Internet Science’, is a member of 'Digital Analytics Association', a Google Analytics Certified Individual and a Certified Web Analyst. He is also the founder of EventEducation.com and EventPlanningForum.net.

My business thrives on referrals, so I really appreciate recommendations to people who would benefit from my help. Please feel free to endorse/forward my LinkedIn Profile to your clients, colleagues, friends and others you feel would benefit from SEO, PPC or Web Analytics.

 

 

  • http://www.analyticsforjoomla.com Martijn van Vreeden

    Hi Himanshu,

    Excellent post. However, I would remove the information about _setAllowHash(), because it is deprecated.

  • http://www.seotakeaways.com/ Himanshu

    Thanks for pointing it out Martijn. I will update the post.

  • Katie

    Thank you for all of your technical posts on Analytics and tracking. I have found your site to be extremely helpful.

    As a php developer, I’ve always been irritated when analytics code directives come from non-technical SEO guys and then get blindly implemented by non-developers.

    That said, I’ve recently been asked to implement the Multiple Top Level domain code throughout all of our websites, even if our websites are not using multiple domains (or even subdomains). For me the reasoning “so that if we add multiple domains down the road we don’t have to change the code” is not enough. Multi domain configuration will take customization either way. I figure if there were no side effects to implementing this code on all sites, wouldn’t google provide the multi-top level domain code to all customers to make the process of generating the code even easier? But even my reasoning is not sufficient for me.

    Do you see any drawbacks to implementing the multi-top level domain code everywhere even when it is not being used?

    • seohimanshu

      I have never faced such situation before. But i think it should not cause any problem.

    • seohimanshu

      I think it will work just fine.

  • Arvind Katke

    Hi Himanshu,
    I have one query:
    I already have Cross Domain Tracking Analytics Account and tracking both my domains traffic under one account. I split these traffic with the help of filters to track and differentiate between different domain traffic.

    The third thing I would like to track is : Domain1 traffic contribution to Domain2 in terms of traffic.

    How do i achieve above filter. I thought of setting filter “Include for Domain2″ and “Include ISP domain traffic from Domain1″, will this work as expected?

    Do I able to track two different UA-ID’s from website?

    Thanks in advance, please help me :)

    • seohimanshu

      you are probably getting this information in your referral traffic report.

  • Amit

    Hi Himanshu,

    I have one question regarding _link(), in our website we are not using absolute links we are using javascript functions for calling links. So in this case how we use this _link() to pass the cookies.

    Thanks in advance

    Amit

    • seohimanshu

      You then need to write a javascript code which can execute the onClick event as described in the post above: object.onclick=function(){SomeJavaScriptCode};

  • Dominic Johnson

    Excellent article, very clear version of a complicated subject.

    • seohimanshu

      Thanks

  • Dominic Johnson

    I have a question – WordPress does not allow you to change the onClick call on a link. Is there a non-javascript version of:

    onclick=”_gaq.push([‘_link’

    Maybe http://www.abc.com?utm_link=123.com ??

    Many thanks, can’t find the answer anywhere – well unless it’s a NO.

  • Aditya Todawal

    Hi Himanshu,

    Your post has proved really helpful for me to implement cross domain tracking. But I am witnessing unusual increase in number of direct visits. These are on pages of second domain. Can you provide some insights on this problem?

    • seohimanshu

      This could be due to referrer getting dropped when people visit your site from the second domain.

  • http://www.acneeinstein.com/ Seppo Puusa

    I was wondering if you could help me with a quick question. I’m trying to track an opt-in form that opens in a lightbox, when a visitor clicks a link it opens a lightbox with the opt-in form. This form is pulled from another domain (leadpages.com) via JavaScript.

    I’m not quite sure how I should set up the tracking in this case. Just adding the GA tracking code to the opt-in form doesn’t work. Should I treat this as cross-domain tracking problem? That is, set domain name to ‘none’ and use the link method to send cookie to the other domain? Not quite sure how this works since the lightbox opens on top of a page within my primary domain.

    I’d appreciate any insights you can offer. Thanks!

    • seohimanshu

      It is a cross domain tracking case. So you need to follow the cross domain tracking rules.

      • http://www.acneeinstein.com/ Seppo Puusa

        OK, thank you! Though an easier solution is just to fire a virtual page view when a visitor opens the lightbox. Doesn’t quite give you the same info, but it’s good enough for my purpose.

        • seohimanshu

          Virtual pageviews will artificiality inflate the ‘pageview’ metric in all those GA reports which use this metric and thus skew your data. Avoid using them.

  • Shahzad Hassan Butt

    Hi Himanshu,

    It is indeed an excellent and informative post. I would like to as you one of my question. I want to track tracffic from my subdomain (blog.myyello.com) to my main domain (www.myyello.com). Appreciate in advance.

    • seotakeaways

      Sorry for the late reply. You need to use the code for tracking across primary domain and its sub-domains.

  • anna-lisa

    Hi Himanshu, thanks for your helpful post.
    I have a question: I have a cross domain & subdomain issue, site A with subdomain A1/A2/A3 and so on, and the conversion/transaction occurs in Site B. I have set a duplicated complete profile for all visits to site A with a filter which includes all visists coming from subdomains. Then I have set single profiles for each subdomain with a filtert including visits from each subdomain (filter Host name). On these single profiles I can’t see any conversion/transaction. I presume it’s because the filter includes only visits from subdomain, so visits (and conversions) from Site B are excluded.
    Am I right? Is there a way to solve this problem?

  • aaron

    can you update your post here to the new analytics?