How to track multiple domains with Google Analytics

The Google Analytics dashboard allows you to see how your audience is interacting with your website. It does this by providing you with a set of pre-defined and user-customisable functions to create reports that are both data and visually intensive. In order to allow Google Analytics to work with your site, we need to install a specific code onto the site that Google provides us with.

This will allow you to view and analyse your website’s data including sales and conversions. It will also show where your sites traffic is coming from. However, when you need to collect the visitor’s data between two or more websites and they proceed from your main website onto another one, the default Google Analytics tracking code needs to be altered in order to allow this data to flow through to the second website, otherwise we will lose the data. This is where cross-domain tracking comes into play. Therefore, in this article, I will cover how to implement cross-domain tracking onto your website, as well as covering sub-domain tracking.

Cross-domain Tracking

To start off, the code below is the standard asynchronous tracking code that Google provides us on account creation. If you have already created an account and want to retrieve your current Google Analytics code, go to Analytics, and click the ‘Admin’ button located in the far top-right of the dashboard. Then click the ‘Tracking Code’ tab.

As part of the procedure to implement cross-domain tracking we will need to make certain modifications to it.

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_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>

To install cross-domain tracking onto the website, we can now proceed to add the following two bold lines to the standard asynchronous tracking code on all pages of the primary domain (www.domainone.com.au) and secondary domain (www.domaintwo.com.au). However, please note that the domain.com.au value needs to be different between the two websites. The complete code should therefore look similar to the code below. As per normal and as Google recommends, this code will need to be placed immediately before the closing head </head> tag.

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', 'domain.com.au']);
_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>

If you’re wondering what the two lines we’ve added actually do, I will now explain them for you. The set Domain Name (_setDomainName) method defines the domain of the cookie within the browser. Whilst using this particular method, we can control the domain name that is used by the cookie and therefore it only has to be used when linking between two top-level domains as is the case with www.domainone.com.au and www.domaintwo.com.au.

The set Allow Linker (_setAllowLinker) method is used to pass the cookie information between the pages that are sitting on different domain names.

As of April 17th this year, the set Allow Hash (_setAllowHash) method has been depreciated and therefore is no longer required to be included for cross-domain tracking to work. Previously, this method allowed us to remove the domain hashing by setting the value to false like so: ‘_setAllowLinker’, false. For example this particular method would create a number generated for your domain name and add it to the Google Analytics cookie already set in your browser. For cross-domain tracking to then work correctly, we previously had to set this to ‘false’ as the domain name will be different on the second domain. Therefore, when the cookie was set within your browser and it discovers the hashing does not match the current domain, it will destroy the hash.

Congratulations! You’ve successfully installed the script onto the websites and now the next step is to add a special method that goes by the name of _link. This method passes a cookie from Domain One to Domain Two via the normal URL procedure. This needs to be added to any links on Domain One that sends the current user to Domain Two. This can be easily done by adding it to the onClick event within the anchor tag.

Please see the before and after modifications as an example of where to place the _link() method.

Before:

<a href="http://www.domaintwo.com.au/">Shopping Cart</a>

After:

<a href="http://www. domaintwo.com.au/" onClick="_gaq.push(['_link', 'http://www.domaintwo.com.au/']); return false;">;Shopping Cart</a>

The previous code was for links going between the two websites. However, if your website sends the user from Domain One to Domain Two using a form, we then need to add another special method called _linkByPost. This method passes a cookie from the form located on Domain One to Domain Two using a string that we will now append to the action value of the form. Just like last time, this can easily be added to the onSubmit event within the form declaration to achieve it. Please see the before and after modifications as an example of where to place the _linkByPost() method.

Before:

<form name="subscribe" action="http://www.domaintwo.com.au/" method="post">

After:

<form name="subscribe" action="http://www.domaintwo.com.au/" onSubmit="_gaq.push(['_linkByPost', this]);" method="post">

Once the above steps have been implemented onto both of your websites, you have now successfully setup Google Analytics Cross-domain Tracking! A special note: Google recommends creating a new tracking profile based on your existing tracking code as the reports will be modified immediately after implementation. Furthermore, Google also recommends the tracking code be the latest asynchronous version. When creating a new tracking profile within the Google Analytics dashboard, this will automatically use the newer asynchronous version. We now also need to modify the cross-domain profile, which is done inside the dashboard by adding an advanced filter to show the full domain in the Google Analytics reporting system. This can be done by signing into your Google Analytics account and pressing the ‘Admin’ button we clicked on earlier. From here, you should be on the ‘Profiles’ tab and underneath this is another set of sub-tabs. Next, go to the ‘Filters’ tab and click on the ‘New Filter’ button. Follow the below steps to create an ‘Advanced filter’ for your profile. Filter Name: Name this what you please. I’ve called it ‘Show full domain’ as an example. Filter Type: Select the ‘Custom filter’ radio button and click ‘Advanced’.

In the next six text areas apply the settings below so they appear in the following screenshot:

Field A: Hostname Extract A: (.*)
Field B: Request URI Extract B: (.*)
Output To: Request URI Constructor: $A1$B1

Sub-domain Tracking

Next we move onto sub-domain tracking which is a similar and easier process then cross-domain tracking as we are not editing any anchor tags or form tags like before. To implement sub-domain tracking, we only need to edit the Google Analytics code. An example of sub-domain tracking is where the user proceeds from Domain One (www.domainone.com.au) to the Store sub-domain (store.domainone.com.au) of the website.

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', '.domain.com.au']);
_gaq.push(['_addIgnoredRef', 'domain.com.au']);
_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>

Once again I will explain what the new lines we’ve added to the standard Google Analytics tracking code do. What we’ve done here is add the set Domain Name (_setDomainName) and the Ignored Ref (_addIgnoredRef) methods. The set Domain Name method, which we also used for cross-domain tracking exactly like before, defines the domain of the cookie within the browser. However, please note this time the leading period before the domain. This is especially important for subdomain tracking as this period causes a cookie reset as the domain hashing will once again be different.

The add Ignored Ref method helps to alleviate the problem with self-referrals appearing in Google Analytics. This works when the browser cookie includes your domain name within it and therefore, as it matches, the referral is ignored and stops the printing of self-referrals in the reports.

Hopefully this article has taken some of the guesswork and trial and error out of implementing both Cross-domain and Sub-domain tracking onto your website, as I know it can be a very fiddly process. However, when done correctly, you can have the peace of mind that your reporting within Google Analytics is spot on. Like always, please leave any comments or suggestions in the section below.

Brett Hoffman

Website Detective

Brett Hoffman is Reload Media's very own Website Detective and his role is to assist all teams within the company in implementing SEO and PPC strategies. Brett is also responsible for the smooth operation including regular updates of our websites to ensure they all meet the latest web standards and provides all customers with an enjoyable web experience.