Google Analytics: Cookies and Tracking across Multiple (sub)Domains
Cross-domain considerations with Google Analytics
Tracking users between domains with Google Analytics does not work 'out of the box' because GA uses only first party cookies. We need a way to share cookie data between domains.
Tracking Multiple Domains
If using HTML links to transfer visitors between domains, use the _link() Method as follows to track between domains:
1. Add pageTracker._setDomainName('none'); and pageTracker._setAllowLinker(true);
2. Add onclick attribute to all HTML links between the sites - onclick="pageTracker._link('http://destinationurlhere'); return false;'"
If using forms (with method="post") to transfer visitors between domains, for example if using a 3rd party shopping cart or payment process, use the _linkByPost() method in the form onsubmit="pageTracker._linkByPost(this)".
_linkByPost appends the contents of the Google Analytics cookies to the form's action URL in the parameters __utma, __utmb, etc. _link adds them to the href URL.
Tracking Multiple Subdomains
To track across multiple subdomains, use pageTracker._setDomainName(".parentdomainname.com"); A side-effect of this is that request URIs may be the same across multiple subdomains and will not be differentiated clearly -- to rectify this, create an advanced filter to prepend the hostname to the request URI as follows:
Filter Type: Customer filter > Advanced
Field A: Hostname
Extract A: (.*)
Field B: Request URI
Extract B: (.*)
Output To: Request URI
Multiple Domains and Multiple Subdomains
For each (sub)domain:
Call _link() or _linkByPost() in all links or form transitions between parent domains 1 & 2 (not necessary for subdomains within single parent domain).
The Google Analytics Cookies
Google Analytics uses 4 cookies (__utma, __utmb, __utmc, __utmz) to track visitors to your site for a standard implementation, and a 5th (__utmv) if you use the _setVar user defined variable or custom variables.
Persistent cookies have an expiration data and remain after closing your browser. Temporary cookies are stored only for the duration of your current browser session (and therefore have no expiration date).
__utma = Visitor ID = persistent (2 years)
__utmb = Session ID = persistent (30 mins)
__utmc = Session ID = temporary (ie. destroyed when browser is closed)
__utmv = Visitor Segmentation = persistent (2 years) (only used if setvar is used)
__utmz = Campaign Values = persistent (6 months)
Contents of __utma cookie
__utma = 168481234.2935412345401845900.1249123716.1249212345.1249212345.1
__utma = (domain hash).(random).(time initial).(time previous).(time current).(session count)
N.B. (random).(time initial) = "visitor id" -> used to calculate unique visitor numbers
If _setAllowHash(false) has been called (important for tracking across multiple domains), the domain hash in __utma and other cookies will be 1.
Contents of __utmb cookie
__utmb = 168481220.127.116.114561716
__utmb = (domain hash).4.10.(time)
Expires after 30 mins
Contents of __utmc cookie
__utmc = 168481234
__utmc = (domain hash)
Temporary. Required to signal a new session if a user closes the browser, then reopens it and visits the site again before the __utmb cookie times out.
If either __utmb or __utmc cookies are missing, the GA code will note this as a new session replace the missing cookie. The GA tracking code can be modified to customize this behaviour.
Contents of __utmz cookie
__utmz = 168481234.1234561716.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
__utmz = (domain hash).(time).(session count).(campaign count).utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
__utmz holds campaign data if URLs are tagged with parameters below...
utmcsr = source (utm_source)
utmccn = campaign (utm_campaign)
utmcmd = medium (utm_medium)
utmctr = keyword (utm_term)
utmcct = ad content (utm_content)
Contents of __utmv cookie
__utmv holds the setVar value if it has been called. If custom variables are used, visitor level custom variables are also stored here.
__utmv = 168481234.var
__utmv = (domain hash).(setvar value)
More on multiple domains, sub-domains and sub-directories on the Google Code Analytics cross-domain tracking doc.