Using Different Cookies When Tracking To Multiple Accounts In Google Analytics

March 6, 2009

The problems discussed in the previous post have been due to both sets of GA using the same cookies for holding information. Another option is to intentionally tell GA to use different cookies.  We can’t change the name of the cookies that GA uses, but we can change the domain and the path of the cookies to make them distinct.

cookies

If you have a simple site such as www.domain.com and domain.com redirects to www.domain.com this is no problem. You can use:

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.&#8221; : “http://www.&#8221;);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

<script type=”text/javascript”>
var pageTracker = _gat._getTracker(“UA-11111-1”);
pageTracker._setDomainName(‘www.domain.com&#8217;);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(“UA-22222-1”);
otherTracker._setDomainName(‘domain.com’);
otherTracker._trackPageview();
</script>

This causes each tracker to use completely different cookies.  At this point you no longer have the problems listed above.  You can track some pages to both accounts, and other pages to just one account without any conflict. You can use different User Defined Segments on each. And you can use cross domain tracking for one set of Tracking Code but not the other.

In addition to every cookie having a Domain that you can set, every cookie has a Path that you can set as well.  By default the Path would just be “/”.  But with GA we can use the _setCookiePath(…); method to set it to the subdirectory that we are operating in.

Just like _setDomainName can’t set the domain of the cookie to something other than the domain you’re actually on, _setCookiePath must be used to set the Path to a subdirectory that the page you are tracking is actually in.

For example, if you wanted to additionally track just one subdirectory to its own account you could use the following change on the tracking code for the pages that appear inside that subdirectory on your website. (Where “subdirectory” below is replaced with the actual subdirectory you are tracking.)

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.&#8221; : “http://www.&#8221;);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

<script type=”text/javascript”>
var pageTracker = _gat._getTracker(“UA-11111-1”);

pageTracker._setDomainName(‘domain.com’);

pageTracker._setCookiePath(‘/subdirectory/’);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(“UA-22222-1”);
otherTracker._setDomainName(‘domain.com’);
otherTracker._trackPageview();
</script>

Again, these two trackers will use different cookies and will not interfere with each other.

Finally, I’d like to point out a comment from my previous post made by André Scholten:

Also don’t forget to add an extra trackPageview call on the links you tagged manually. For example: if you added some tracking code to a print button it wil look like this: onclick=”pageTracker._trackPageview(’print’); otherTracker._trackPageview(’print’);”

If you want the data to appear in all accounts, then anytime you call _trackPageview for one tracker, you have to call it again for every other tracker you created (In this case, just 2).