Google Analytics Cross Domain Tracking (ga.js)

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

Get the E-Book (37 Pages)

Learn to read e-commerce reports book banner

Get the E-Book (104 Pages)

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

<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’ 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

<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’  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

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

<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’  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=”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 _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 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 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: 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 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 

Other Articles on Specialised Tracking in Google Analytics

  1. Google Analytics Recurring Revenue and Subscriptions Tracking Guide
  2. How to track the impact of cookie consent on website traffic in Google Analytics
  3. Phone Call Tracking in Google Analytics and Beyond
  4. Scroll Tracking via Scroll Depth Trigger in Google Tag Manager
  5. Video Tracking via YouTube Video Trigger In Google Tag Manager
  6. Calculated Metrics in Google Analytics – Complete Guide
  7. The one thing that you don’t know about PayPal.com and the referral exclusion list
  8. Introduction to Postbacks in Google Analytics
  9. Creating Content Group in Google Analytics via tracking code using gtag.js
  10. Tracking Site Search without Query Parameter in Google Tag Manager
  11. Setting Up Site Search Tracking for POST based search engines in Google Analytics
  12. Tracking Site Search for GET-Based Search Engines in Google Analytics
  13. Understanding site search tracking in Google Analytics
  14. Tracking Virtual Pageviews in Google Tag Manager – Complete Guide
  15. Creating and using Site Search Funnel in Google Analytics
  16. Learn to Setup Facebook Pixel Tracking via Google Tag Manager
  17. How to use Keyword Hero to reveal Not Provided keywords in Google Analytics
  18. Guide to Event Tracking via Google Tag Manager
  19. Learn to track Qualified and Won leads in Google Analytics
  20. Creating your own Google Analytics Tag Auditing System
  21. Using multiple Google Analytics tracking codes on web pages
  22. Guide to Offline Conversion Tracking in Google Analytics
  23. Google Analytics and YouTube Integration Guide
  24. Complete Guide to Google Analytics for Facebook
  25. Tracking true referrals in Google Analytics when using PayPal and other payment gateways
  26. Virtual pageviews in Google Analytics – Complete Guide
  27. YouTube Video tracking via Google Tag Manager
  28. Implementing E-Commerce Tracking via Google Tag Manager
  29. Event Tracking in Google Analytics – Complete Guide
  30. E-Commerce Tracking in Google Analytics – Complete Guide
  31. Google Analytics Cross Domain Tracking Explained Like Never Before
  32. Google Analytics Social Tracking – Twitter, Facebook, Google Plus and LinkedIn
  33. Setting up & Tracking AMP Pages in Google Analytics
  34. Setting up Sales Funnel across websites in Google Analytics
  35. Using Data Visualization to analyze the SEO Performance
  36. Tracking offline conversions in Google Adwords
  37. How to Correctly Measure Conversion Date & Time in Google Analytics
  38. Tracking Twitter and Linkedin Social Interactions in Google Analytics
  39. Implementing Scroll Tracking via Google Tag Manager
  40. Phone Call Tracking and ROI calculations – Champion’s Guide
  41. Google Analytics Cross Domain Tracking (ga.js)
  42. Open Graph Protocol for Facebook Explained with Examples

Learn about the Google Analytics Usage Trends Tool

The Google Analytics usage trend is a new tool which is used to visualise trends in your Google Analytics data and to perform trend analysis.


Do you want to Learn Web Analytics in 4 Weeks?

  • Learn and Master Web Analytics, Conversion Optimization & Google Analytics from Industry Expert in 4 weeks.
  • Lifetime access to the course + Lifelong FREE course updates.
  • New study material added every few months (lifelong learning).
  • Up to date training material.
  • Most exhaustive course on Google Analytics on the internet.
  • Hundreds of Assessments to test your learning.
  • Your 24/7, 365 days a year reference source.
  • Learn at your own pace and from any place.
  • Risk Free with 30 days 100% Money Back Guarantee.

Take your Analytics knowledge to the next level. Checkout my Best Selling Books on Amazon

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.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Alert: Content is protected !!