Cross-domain tracking and _setVar


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”);

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. . .)

John is a former LunaMetrician and contributor to our blog.

  • Paul


    I have noticed an issue with cookies not working properly in firefox (possibly just firefox 3) and as a result, all transactions are being filed under direct traffic. Can you comment on this at all?

  • John

    First guess is that your ecommerce portion of the site is on a different domain than the rest of your site and there are issues with your cross-domain tracking.

    But that’s just a guess, since it would be the most common cause of direct transaction traffic.

  • matt


    I have a non-comm and an ecomm site on different domains and I want the keyword that was used to get them to the non-comm site to stay with the visit when the visitor crosses domains. Would I use the approach you outlined above, (i.e. leveraging the setvar to store the keyword) or is there better approach to accomplish this?

  • John

    The post above is about an incorrect placement of the _setVar method.

    For what you want, you (probably) just need to implement the standard cross-domain tracking GA code with

    and whenever you link between domains use the pageTracker._link(. . .) method.

    Google’s documentation for cross domain tracking is located at:

    And is a good place to start.

  • Mike

    Hello John,

    I use a subdomain from which visitors go to the main domain to to a purchase, so the thankyou page is on the main domain. In GA I lose the referral when visitors go from the sub to the main domain (link to main domain is in iframe on subdomain).

    My GA code:

    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(unescape(“%3Cscript src='” + gaJsHost + “’ type=’text/javascript’%3E%3C/script%3E”));

    try {
    var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
    } catch(err) {}

    Can i add some code to keep the cookie after the crossing of sub to main domain?

    Hope you can help!


  • John

    I think you have the GA code correct, so the problem could involve the iframe implementation.

    I’m afraid that at this point, it is not very obvious and i’d have to dig into your specific situation to attempt to troubleshoot.

    Sorry I couldn’t just give you an easy answer and fix.


Contact Us.


24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us



We'll get back to you
in ONE business day.