Cross‑domain Tracking And _setVar

April 7, 2009

This is from a question that I answered on the support forum that involved cross-domain tracking and the use of a _setVar().

The problem was that the session information was being lost.  That is, GA was losing track of the visitor and started a new visit for them.  This not only would alter the visit data, but in the ‘second’ visit, you would lose all of your referral knowledge and this ‘second’ visit would appear direct.  Since this is where the conversion was happening, it was a real problem.

The code being used was:

var pageTracker = _gat._getTracker(“UA-xxxxxxx-y”);
pageTracker._setVar(‘buyer’);
pageTracker._setAllowAnchor(true);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();

The problem here is the placement of pageTracker._setVar(‘buyer’);

This is a non-obvious problem that actually comes from _setAllowHash(false);

One of the things that _setAllowHash(false) does is to alter the format that GA uses to write cookies to the visitors computer.  There are 2 lines here that write cookies, the _setVar and the _trackPageview.  One happens before the _setAllowHash and one after.  So what was happening was this:

1. pageTracker object gets set up
2. _setVar writes cookies using “Format A”
3. _setAllowHash changes cookie format to B
4. _trackPageview writes cookies using “Format B”

So what we really have is GA getting confused because of some cookie mismatches, and this is what causes the problem.

The solution is simply to make sure you call any _setVar()’s After _setAllowHash.  (Or after pageTracker._setDomainName(‘none’); if you use that line.)

This was the first time I’d seen this problem.  It is probably pretty uncommon may not be very useful by itself.  But a slightly better understanding of what the JavaScript is doing may help you troubleshoot some other situation.  (And, more importantly, Robbin wanted me to write a blog post. . .)