Analytics Market Chart

Tracking Subdomains in Google Analytics


Subdomains on a website can confuse Google Analytics. You need to make decisions about how you want to track your site and then tag accordingly.

You may have heard that Google Analytics uses cookies. In fact, it pays a lot of attention to those cookies.

By default, the cookies are set to the full domain shown in the address bar. When the visitor moves to a different subdomain, the tracking code often will create another set of cookies and ignore the original set. This also usually results in Google Analytics assuming that a new visit from a new visitor has begun, and it may show the previous subdomain as the referring site for this new visit.

Our code generator tool will customize code to take care of any subdomain tracking issues you are having.

Tracking Regardless of the Subdomain

If you want to track visits seamlessly as they move from one subdomain to another, you will need to edit the tracking code on each page using the _setDomainName() method. This method affects what domain the cookies are set to, and it should be called in the main block of tracking code before _trackPageview().

Calling _setDomainName('.mysite.com') will force the tracking code cookies to be set to mysite.com, regardless of the subdomain.

This method needs to be called identically on each page of the site. Also, if the parameter does not match the domain in the address bar, the code will not set any cookies at all.

Tracking for Specific Subdomains

If you want to track each subdomain separately for different profiles, pass _setDomainName() the exact domain.

I know what you're thinking. "You said that Google Analytics set the cookies to the full domain by default."

You're right. I did. But if you need it done consistently, you should use _setDomainName() and pass it the full domain. Then you know for sure that it's working correctly.

This is especially important for sites like blogs that exist only on a specific subdomain and where different subdomains are owned by different people.

Automatically Generate the Right Code

It's worth mentioning that we have a free tracking code generator that will add these customizations and more to your code.