Cross domain and sub-domain tracking with google analyticsMany of our clients have a need to track their web site visitors across domains and sub-domains.

However, Google Analytics does not automatically track a single visitor across different domains or sub-domains using the default set of tracking code, so some customization of the default code has to take place.

All of my past research suggested this should be easy, and like anything it is, if you have all of the information you need.

After many, many, many, many tests, I have come up with the following guidelines.

(Please note this is for the ga.js tracking code, not the older urchin.js code.)

Sub-Domain Tracking

Let’s start with the easy one.

Premise:

Your main site is www.abc.com.

There are links from your main site to your store, shop.abc.com, and your help section, help.abc.com.

You want to track a single visitor who views the pages on your sub-domains.

Your default tracking code is:

<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-1");
pageTracker._trackPageview();
} catch(err) {}</script>

All you have to do is add in the setDomainName() method for Google Analytics to track a single visitor across all of your sub-domains:

<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-213407-1");
pageTracker._setDomainName(”.abc.com”);
pageTracker._trackPageview();
} catch(err) {}</script>

I don’t care what you’ve read elsewhere, PLEASE INCLUDE THE ‘.’ before the domain name setDomainName(”.abc.com”);. I’ve seen this not work without it.

(Please remove pageTracker._initData(); if you are currently using it. It is deprecated. Yes, I still need to remove this method from our code!)

Cross Domain Tracking

Now on to the slightly more involved stuff.

Premise:

Your main site is www.abc.com.

There are links from your main site to a completely different site that you manage, www.def.com.

You want to track a single visitor who visits www.def.com.

There are a few tasks that need to be completed in this situation.

  1. You need to add three methods to the default Google Analytics tracking code.
  2. You need to add a method/function call to every link or form button that takes your site visitor to def.com.
  3. You should place the customized Google Analytics tracking code in the <head></head> section of your page rather than just before the closing </body> tag (normal recommendation).

Alter the default tracking code:

Default code is this:

<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-1");
pageTracker._trackPageview();
} catch(err) {}</script>

Add in three methods:

<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-213407-1");
pageTracker._setDomainName(”.abc.com”);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}</script>

You need to add in the above methods in red to every domain that you are tracking.
NOTE: pageTracker._setDomainName(”.abc.com”); should be set to the domain the tracking code is actually on.

So on abc.com:
pageTracker._setDomainName(".abc.com");

On def.com:
pageTracker._setDomainName(".def.com");

NOTE:
If you read Google’s recommendations, they tell you to use this:
pageTracker._setDomainName("none");

From all of my research and tests, which includes talking to many other experts, you should NOT use “none” as the setDomainName() value. It can cause duplicate cookies and therefore inaccurate data.

Add _link() and _linkByPost() methods to your code

In order for Google Analytics to track a single visitor across domains, the cookie information needs to be sent to the other domain(s). This is accomplished by calling the link() method on normal hyperlinks and the _linkByPost() method on form submission buttons.

Let me make something clear up front:

You only have to add these links to the domain that your visitor lands on first.

So if your visitor lands on abc.com first, then all links and form submissions to def.com need the _link() and _linkByPost() methods added to them respectively. You do NOT need to add these methods to links or posts FROM def.com to abc.com.

(If your visitors could potentially land on either domain first and then link off to the other domain, then you would want to add the methods to both sites.)

Here’s how to add the code.

Hyperlinks:

<a href="def.com/mypage.html" onclick=”pageTracker._link(this.href);return false;”>Link Here</a>

NOTE: Do not forget the return false; part of the code!

Form Submissions:

<form name=”post_form” method=”post” action=”mypage.php” onsubmit=”pageTracker._linkByPost(this);”>

Place the Google Analytics tracking code in the <head></head> section of your page

If you are using any _link() or _linkByPost() methods in your code, then the Google Analytics tracking code should be placed in the <head></head> section of your page.

This is because the ga.js file needs to be loaded before the _link() or _linkByPost() methods are called.

Chances are the page would load (and therefore the ga.js file) before a user triggered one of the methods, but better safe than sorry.

NOTE: If you want to track cross domain AND sub-domain traffic, just follow the Cross Domain Tracking guidelines above.

That’s it. Go forth and track.

 

Digg This | Stumble It! | Save to del.icio.us | Netscape

Subscribe | Email This | Trackback