Tracking Sub-Domains and Multiple Domains with Google Analytics

- October 2, 2009

There really isn’t anything better in the world than a properly configured Google Analytics tagging implementation. Well, actually, there are lots of things that are better, like Double Chocolate Chip ice cream and a live rock concert! But at this exact moment, no, there isn’t anything better than the comfort of knowing that your online presence – regardless of how many sub-domains or multiple domains it spans – is tagged perfectly and the cleanest possible data is being funneled into your Google Analytics account.

This is an important topic because there are lots of online storefronts, blogs, microsites, and other web site sections that are hosted on a sub-domain or a completely separate domain that are often forgotten about when it comes time for installing Google Analytics. When tagging isn’t present on a portion of your web site, not only does your bounce rate increase, you also start collecting “dirty data”, including self-referrals in your Traffic Sources section, along with a high than normal amount of direct traffic.

So, how do you implement Google Analytics across sub-domains or across multiple domains? It’s actually quite simple to do, as long as you follow the exact instructions outlined below. Let’s start first with the easy one, sub-domains. We’ll then work our way into the more complex one, multiple domains.

Tracking Google Analytics Across Sub-Domains
Let’s say that your web site’s URL is www.site.com. Let’s also say that your site’s blog is hosted on blog.site.com. Because of the way that the Google Analytics Tracking Code works, installing the code as is from your Google Analytics account will produce a lot of referrals from www.site.com or blog.site.com in your All Traffic Sources report. This means that the visitor’s original referring information (Google, Yahoo, Bing.com, etc…) will be lost if the visitor goes from your main site to your sub-domain site, or vice-versa.

To avoid this headache, simply update your Google Analytics Tracking Code to include the call to _setDomainName, as shown in bold below:

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-XXXXXX-X”);
pageTracker._setDomainName(“.site.com”);
pageTracker._trackPageview();
} catch(err) {}</script>

This modified version of the Google Analytics Tracking Code will need to be installed on both your main site and the sub-domain site. If you have multiple sub-domain sites, then all of those will also need to have this exact modified version as well. Finally, be sure to change “site.com” within the Tracking Code to your site’s domain.

This is relatively easy for you or your webmaster to do.

Tracking Google Analytics Across Multiple Domains
Setting up Google Analytics Tracking Code on multiple domains is a bit tougher than setting GA up on sub-domains, but it’s definitely doable. The following instructions are what you’ll need to follow if you have multiple domains, for example, www.site.com and www.somethingelse.com.

There are a couple of different ways that you can pull this off in documentation online. However, this is our preferred way. First, you’ll need to use calls to _setAllowLinker and _setAllowHash in your Google Analytics Tracking Code on each multiple domain site, like the bolded calls below:

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-XXXXXX-X”);
pageTracker._setAllowLinker(“true”);
pageTracker._setAllowHash(false);

pageTracker._trackPageview();
} catch(err) {}</script>

This is normally done without too much of a problem. The problems come about with this next item, which absolutely must be installed in order for multiple domain tracking to function. On every link to and from each domain, _link must be used, and on every form that takes a user to and from each domain, _linkByPost must be used.

For text links or image links, use _link as a JavaScript onClick event:

onclick=”pageTracker._link(‘http://www.site.com/page.html’); return false;”

You’ll need to update “http://www.site.com/page.html” in the above _link function with the full path that a visitor will be taken to on your own web site.

For submit buttons or “add to cart” buttons, use _linkByPost as a JavaScript onSubmit event:

onsubmit=”pageTracker._linkByPost(this)”

It bears repeating that either _link or _linkByPost MUST be used on links to and from each domain. A user may click on a text link that uses _link going from one domain to the next and not affect their tracking, but if they click on a link on the next domain that takes the same user back to the original domain, _link must be present on that link as well, or referring information gets lost and self-referrals start to occur in your Traffic Sources report.

Work closely with your webmaster or IT guru to make sure that _link and _linkByPost are used on every link or form on every domain. It may be tedious, but it’s critical for proper cross-domain tracking with Google Analytics.

We hope that helps you tag your sub-domains and multiple domains properly with Google Analytics. It’s a bit of extra work, but data that is as squeaky clean as possible is a fantastic reward for your efforts!

Comments are closed at this time.

© 2017 MoreVisibility. All rights reserved