Dot Or Not

May 27, 2009

We often hear variations on the question “Do we need the leading dot when using _setDomainName”.

With the dot:
pageTracker._trackPageview(‘.www.lunametrics.com’);

Without the dot:
pageTracker._trackPageview(‘www.lunametrics.com’);

The _setDomainName method is used to tell GA what to set the “domain” to, for the cookies that it uses to keep track of visitor information.  This “domain” field for the cookies are used as part of the security in browsers to determine what websites can access what cookies. (You don’t want porn.com getting access to your amazon.com cookies, for example.)

The Google Analytics documentation recommends using a preceding dot.  For example: pageTracker._setDomainName(‘.www.lunametrics.com’)

The example above allows any www.lunametrics.com subdomain to access the same cookies.  (blog.www.lunametrics.com and help.www.lunametrics.com are both still “www.lunametrics.com” so the browser says this is okay)

In most cases, it doesn’t really matter whether you use ‘.www.lunametrics.com’ or ‘www.lunametrics.com’.  As long as you’re consistent across the entire website that you’re tracking. That’s part of the confusion.  Both ways work fine.

However, Google explains, in cases with multiple levels of subdomains, you can sometimes run into problems if you don’t use the preceding dot with _setDomainName.  If we had robbin.posts.blog.www.lunametrics.com the browser may not let the GA code on that page have access to cookies written with _setDomainName(‘www.lunametrics.com’), but it would allow access to cookies written with _setDomainName(‘.www.lunametrics.com’).