Google Analytics _setAllowHash bug

GATC (Google Analytics Tracking Code) has an annoying bug with _setAllowHash.

Suppose you have something like this:

  • Domain http://test.cereto.net/ that has a GATC for multiple sub-domains.
  • Domain http://test2.cereto.net/ that has both our tracker and a secondary default tracker that we don’t control.

If you use this configuration it should work as expected. Two sets of cookies are gonna be created. One set inside domain test2.cereto.net and the second set inside .cereto.net. GATC will know which cookie to look at on both cases.

But now suppose you also want to track domain www.my-other-domain.com in the same account. What you’d need to do is:

  • Use _setAllowLinker(true) on both accounts.
  • Use _getLinkerUrl() or _link() on the links that go from one site to another and vice versa.
  • Use _setAllowHash(false) on both domains.

_link() and _getLinkerUrl() will move the cookies from one domain to another. _setAllowLinker(true) is needed for GATC to look on URI for cookie parameters.

Q: Now why would you need _setAllowHash(false)? A: The GA cookies have a parameter that is a domain hash (in red below ). Of course the hashes from both our domains are gonna be diferent. In that case Google will trash the cookie when it sees that the hash doesn’t match the current domain. So we set _setAllowHash(false) and everything is  fine. Is it?

Cookie with a domain hash
__utma=253008534.504424944.1258547704.1258547704.1258547704.1

Cookie with Domain Hash disabled
__utma=1.504424944.1258547704.1258547704.1258547704.1

But now we don’t have the Hash anymore and it’s very important to GATC. When we’re reading cookies with javascript we have no information about the cookie besides value and name. The Hash is important so GATC knows which is the right cookie for that specific GATC.

In our setup we’ll have two sets of cookies available from test2.directperformance.com.br. If we disable the Domain Hash on both there’s no way for GATC to get the correct one. This will lead to ga.js firing pageviews with mixed data from both the cookies. This will mix origin, user hash, custom variables and more. Generating unexpected results on Analytics Interface.

This is an old bug. You must avoid it but sometimes there’s no way. It’s present no matter if you use default _gat or the new Async Tracker _gaq.

Example

I created a little test to illustrate the issue:

  • Access first domain using a url with campaign variables. This has a single tracker/single cookie
  • Now access second domain directly. It has two trackers each in a diferent domain, so 2 different cookies.
  • The cookies were created accordingly, one for each tracker. The first one still has the campaign origin, and the second should be a refferal from this blog now.
  • Since you have _setAllowHash(false) on both trackers, GATC don’t know which cookie to parse.
  • You can see using HttpFox or similar that both pageviews have the same origin.

As explained GATC didn’t know which was the correct cookie, and got the first one.

Solution

There’s no good solution at this time, besides avoiding this setup.

All could be solved if _setAllowLinker(true) simply ignored the domain hash and used the hash for the current domain instead, after all it makes no sense to check the domain hash on the cookies you’re importing.

There’s an undocumented feature on ga.js that seems to fix it. It’s the function _setNamespace(‘ns’). If you use this on one or both trackers (with different Namespace for each of course). This problem is gone. But it’s not safe to use undocumented features as it might change in the future or removed completely generating unexpected results. You won’t want to use that on your production code.

This post is intended to get this bug properly documented since there’s no public bug tracker for ga.js and I didn’t get proper response or position from Google on any related user groups out there;