The ROI Revolution Blog
« Google Analytics Keyword Sleuth vs Search Query Performance Report | Main | All About Historical Data »
Tracking Subdomains
March 27, 2008
One of the most important things to consider when trying to set up Google Analytics for your site is the integrity of your visitors' source/medium data. Keeping this data as accurate as possible will go a long way to helping you make useful decisions about your marketing efforts.
That being said, there is a silent enemy threatening to destroy the harmony of your Google Analytics data: the self-referral. You may first see it rear its ugly in head in the All Traffic Sources report. Not only is it rather disconcerting to see your own site as a visitor's referrer, but this entry in your reports represents irrevocably lost data. What's worse, you may even notice that the conversion rate for this segment of traffic is actually quite good. You may be putting lots of time and money in SEO, paid online adversing, e-mail campaigns and print ads, but when someone asks which of these was responsible for the conversion, you really don't know. Some of those sources may be getting overwritten by your self-referrals.
If your site has subdomains (domain.com and blog.domain.com, for instance), this might be causing the self-referrals to show up in your reports. The standard Google Analytics Tracking code is only good for sites with a single domain and no other structural complications. Anything beyond this and you'll need to make some kind of modification to the script. Subdomains are one such complication.
Whenever a visitor comes to your site, the Google Analytics Tracking Code on your pages asks the visitor's browser a question:
1. Do you have any cookies for this domain?
If the answer is no, then a follow-up question is asked:
2. Where did you come from?
Cookies are then created with values based on the information provided in the second question. As long as a visitor stays on a single domain, there's no confusion. If a visitor moves from domain.com to blog.domain.com, the browser responds to the first question with "No, I don't have any cookies for blog.domain.com" and to the second question with "I was referred from domain.com." The cookies from domain.com are not recognized, so the source, medium, campaign name, keywords, etc. information is not available while the visitor is on the blog subdomain.
To make matters worse, if the visitor goes back to main domain from the subdomain, the original source/medium for the main domain may be lost as well. The Google Analytics Tracking Code will ask the browser the same question as before:
1. Do you have any cookies for this domain?
Even though the browser can answer "Yes, I have cookies for domain.com," there's a different follow-up question that the Google Analytics Tracking Code asks after a "Yes" response to the first question:
2. Is this a new visit?
If the answer is "No," then everything is fine. But if it's been over 30 minutes or the browser has been closed since the last domain.com pageview, then the answer will be "Yes," and again, the question will be asked:
3. Where did you come from?
Since the visitor is coming from blog.domain.com, the original source and medium will be overwritten with a referral from blog.domain.com.
Fortunately, this issue is simple to resolve. For the ga.js version of Google Analytics, just add the line pageTracker._setDomainName("domain.com"); to every page on your site, both the main domain and any subdomains of that domain. Your final code should look something like the following:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._setDomainName("domain.com");
pageTracker._initData();
pageTracker._trackPageview();
</script>
If you're still using urchin.js, this line is _udn="domain.com";. Again, you should add this line to every page on your site, both the main domain and any subdomains of that domain. Your final code in this case should look something like the following:
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXX-X"
_udn="domain.com";
urchinTracker();
</script>
Be sure to replace "domain.com" with your own domain. That's it! Now the Google Analytics Tracking Code will always look for cookies for domain.com, even if the visitor is on a page for blog.domain.com. So when a visitor moves from domain.com to blog.domain.com, the following conversation ensues:
GATC: Do you have any cookies for domain.com,
Browser: No, I don't have any cookies for blog.domain...wait a second...did you say domain.com?
GATC: Sure did.
Browser: Yeah, I got those.
And everything is right in the world.
There are a couple of different filters that are useful to have when your site has subdomains. One will include only traffic from a subdomain and the other lets you differentiate between pages with the same name but from different subdomains.
Please feel free to leave any comments or questions.
Interested in learning more about Google Analytics?
Attend our LIVE Google Analytics Seminars for Success training in Atlanta, GA Wednesday, April 14th, 2010 and Thursday, April 15th, 2010 or get the latest tips and tricks sent to you via our free, twice-monthly Google Analytics newsletter.
Posted by Jeremy Aube, Analytics and Optimization Specialist at 1:30 PM
Permalink | Comments ( 42 ) | TrackBacks ( 0 )
Filed under: Analytics
Tagged as: Analytics Basics
Comments
@Reid: Not tracking for subdomains is not the only reason someone might see self-referral in their Google Analytics reports. Other reasons include not tagging all pages of the site with the Google Analytics Tracking Code, not properly tracking for multiple domains in addition to subdomains, content, applications, or forms that require a visitor to stay on a single page for an extended period of time, etc.
In addition to this, even if you fix one of these issues, any visitors who had their source/medium overwritten with a self-referral will again show up as a self-referral when they return to your site. It's important to fix these issues in order to maintain the cookie integrity of your future visitors, but there is no hope of getting the previous source/medium for those visitors who have had theirs overwritten.
March 28, 2008 9:07 AM
Hi,
We use unique URL's to try and bring off-line advertising online (where we can more easily track it!). So we have started putting unique URL's in most of our print ads. All of the unique URL's point to our main site and now we are noticing that they are not being captured by Analytics. Do you have a strategy to fix this? Is sub-domains the answer (if "yes", we're screwed b/c our unique names have already been printed!)
Any comments/posts on the topic would be appreciated.
March 28, 2008 9:08 AM
@Benjamin: You don't need subdomains for what you're doing. What you will need to do is tag the 301 redirects to your main site with the appropriate utm parameters. You can get help with tagging your links by going to our URL builder found here.
March 28, 2008 9:50 AM
I have the following situation: main domain is www.firstdomain.com whitin the site we have pointed a'second domainname www.seconddomain.com to: www.firstdomain.com/academy
Now I want messeaure this second domainname separate from firstdomain.com
April 3, 2008 10:55 AM
@Emiel: This would involve setting up an additional profile with a filter that only includes visits to the /academy subdirectory. There is a built in filter that does this, so it's pretty easy to set up. You do not have to modify your tracking code in any way to track a site that just has subdirectories. Since your second domain is pointing to your first domain, you don't have to worry about any multiple domain issues either.
April 3, 2008 11:10 AM
Something I have always wondered is what if you have sub-domains AND multiple domains on your site? For example, main site is www.site.com and it also has a subdomain, example.site.com. When customers make a purchase they go to a secure shopping cart on another domain, www.securesite.com.
I know for tracking sub-domains you want to add pageTracker._setDomainName("domain.com")
in your code but for multiple domains you are supposed to use pageTracker._setDomainName("none").
So which one do you use if you have both?
Thanks, have always wondered!
April 3, 2008 9:16 PM
@Jesse: The short answer is that it's complicated. The not-quite-as-short answer is that you need to use pageTracker._setDomainName("domain.com"), but you need it to match the root domain of the page you're on. You still have to call pageTracker._setAllowLinker(true); and use appropriate linking functions between domains (though not between subdomains with the same root), and you also have to call pageTracker._setAllowHash(false);.
This will all be explained more fully in a future article. For now, let me point you to an excellent article that explains this for the urchin.js version of Google Analytics.
April 4, 2008 8:33 AM
"The not-quite-as-short answer is that you need to use pageTracker._setDomainName("domain.com"), but you need it to match the root domain of the page you're on."
So will the other domain, such as a secure shopping cart on another domain like www.securesite.com, be be counted along with the main site and subdomains this way? Liek would I see in the top content report pages from the main site and the scure site all togther instead of seeing th secure site as a referral if I follow all of these above instructions?
April 4, 2008 1:05 PM
@Jesse: If you are able to put the Google Analytics Tracking Code on your shopping cart pages and everything else is set up correctly, then yes, you would see everything together in your top content report, pages from both the main site and the secure site. It would prevent future referrals from the shopping cart as well.
If you can't put tracking code on your shopping cart, however, you can still prevent referrals from your shopping cart by tagging your return links with utm_nooverride=1.
April 4, 2008 1:29 PM
How can i integrate Google Analytics without having a web domain?
Means how can i tract google analytics if i don't have web site address.
I want to integrate GA for my localhost website.
Is it possible?
August 28, 2008 7:23 AM
@Nirav: It is possible, but only if the domain is fully qualified. For example, localhost.com will work, but localhost will not.
August 28, 2008 9:10 AM
@Jeremy on March 27: If anyone is still reading this, I came across your comment while researching subdomains. We've had GA code on our site for a year, and just figured out the subdomain issue, so we think we've got those set up properly. Is there anything we can do at all to "reset" our traffic. I can't think of a better word, but I am concerned that we will have all those legacy referrals from our subdomains forever. What is the best practice for this?
September 17, 2008 12:29 PM
@Amanda: Now that you have correctly set up your Google Analytics Tracking Code to account for subdomains, you should see your referral data steadily improve going forward. There's no good way to "reset" your traffic. Even if you could reset your traffic, the best you could do is rename it as direct traffic, which doesn't give you any more information than the referral does. Overtime your self-referral traffic should decrease until it's no longer significant.
September 17, 2008 2:59 PM
Jeremy,
I've been having some trouble with subdomains tracking the last couple of months.... and now I came across your post and the only difference that I see with my approach is that my tracking code doesn't include the pageTracker._initData(); line, my code looks like this:
var pageTracker = _gat._getTracker("XXXX");
pageTracker._setDomainName("domain.com");
pageTracker._trackPageview();
should I add the pageTracker._initData();? sorry, but I goy confused because it's not in bold.
October 22, 2008 4:20 PM
@piko: pageTracker._initData() has been deprecated. It won't hurt your code if it's already in there, but adding to your code won't help it to work any better either. Generally, subdomain tracking should be pretty straightforward. If you're having issues with it, your best bet is to check and make sure that you're consistently using the same tracking code across the entire domain. Also, if you previously weren't tracking for subdomains and only started to recently, you may have a fair amount of self-referrals left over for a while. This should improve over time.
October 22, 2008 4:36 PM
Thanks for the detailed info. I didn't think there was a solution for my dilemma.
The good news is that I just started G.A. on the subdomains, so thanks for starting me in the right direction.
I do have another semi-off-topic question. I currently use a free StatCounter along with GA. Is this overkill? The only reason I have SC is because of its simplicity. Thinking of getting rid of it just for lesser codes on my footer.
Thanks in advance.
December 24, 2008 11:22 AM
@Marc: It probably doesn't hurt anything to have SC on there as well, but like you said, it will simplify things to have less tracking codes on your pages.
December 24, 2008 12:01 PM
Hi,
We've been using GA for about an year. And it's been workig great. Recently we added the setDomainName to merge all our sub-domains.
The traffic from the sub-domains is now being referred as direct traffic.
The problem is that since the change, all the users that had thier cookie before the change lost thier source/medium value and are now included as direct traffic.
We use exactly the code you recommended on.
What can i add to stop the old users from losing thier source/medium?
Thanks in advanced!
January 9, 2009 4:11 AM
@Lior: There's not really any good way to prevent old users from losing their referral data. This is an unfortanate, but temporary set back. As you get more and more new visitors to the site, you should see a drop-off in direct traffic.
On the other hand, if you had never addressed the subdomain issue, you would continue to see referral data overwritten for traffic going between subdomains. This would increase as your subdomains became more integrated over time. So long term, it's to your benefit to track for subdomains.
If you still want to attempt to recover the source/medium from old users, you could write some code that would look for the old cookies and rewrite them so they match the format of the new cookies before the Google Analytics Tracking Code runs. This certainly isn't trivial, but it should be doable.
January 9, 2009 8:33 AM
Thanks Jeremy!
Do you have an example of such a code that i could use?
January 19, 2009 4:22 AM
Hi Jeremy,
You answered me about my users losing their referral data after i set the setDomainName property.
It's not just that the referrals are lost is it?
All the GA cookies change their values.
Is the user now considered as an entirely new user?
Thanks for your help.
January 19, 2009 8:09 AM
@Lior: I don't have any code like that ready and it would take some time to develop. As I've stated before, longterm your referral data will start to look better and will benefit from the subdomain tracking. If you absolutely need to keep the old visitor data and convert those cookies, I would advise purchasing support so that we can develop the code for you. Otherwise, it would be best to just hang in there and wait for the data quality to improve over time.
January 19, 2009 8:21 AM
Yes, they are considered a new user now. It's not so much that the Google Analytics cookies have changed their values as that the old cookies have been abandoned and new ones are being used instead. The old referral cookie will remain out there for 6 months. The old cookie that lets Google Analytics know that it's a returning visitor will remain out there for two years.
January 19, 2009 8:24 AM
Great Job on your blog post! , though a quick question came up. You are using pageTracker._setDomainName("example.com"); where google uses pageTracker._setDomainName(".example.com"); with an extra . (DOT) before the domain name. Which is the right way to do it and does it actually matter? You can see google’s way on http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55524
Before reading your post I tried doing it the google way and I am trying to collect some data to see if it work. I see it took all my 2 subdomains and made it into 1 cookie instead of 2.
Just wondering if this effects reporting.
January 27, 2009 10:12 PM
Our main site is http://www.ibo.com and there are two sub domains like -
dir.ibo.com and trade.ibo.com
My question is should I add the code -
pageTracker._setDomainName("domain.com"); to all the above three sites and on each page.
The All traffic report for http://www.ibo.com shows referals from two sub domains. If I implement the above code, what does it show ?
January 28, 2009 3:12 AM
@Atul: Mostly likely, you should add pageTracker._setDomainName("ibo.com"); to the Google Analytics Tracking Code on all three subdomains. Doing this will allow you to see the original referrer that brought a visitor to your site rather than knowing that that they went from one subdomain to the other.
If for some reason it's more important for you to know that one subdomain is sending traffic to another domain than to know what's sending that to your site in the first place, then you may want to think about not enabling subdomain tracking. For most cases, the original referral will be the most important.
January 28, 2009 8:47 AM
@Paul: Both ways (with or without the dot) are acceptable, but the key is to be consistent. If you decide to use the dot, you must always use the dot. Likewise, if you decide not to use the dot, then you should never use the dot.
Not using the dot tends to be a little bit more forgiving. For example, if you forget to add the subdomain tracking line for a page or two on your main domain, your tracking may still work.
On the other hand, if you use the dot and forget to add the subdomain tracking line for a page or two, it will almost certainly break your tracking for those pages and cause referral issues in your reports.
Other than that, as long as you consistently use one way or the other you should be fine. Personally, I never use dot for any tracking purposes, but you may find that you're already locked into using it. That's perfectly OK.
January 28, 2009 8:54 AM
Hi Jeremy,
I'm having an issue understanding the referring logic with this setDomainName "thing"...
If i have few sites that are under subdomains (e.g a.site.com | b.site.com) and i set the setDomainName to "site.com" what should i expect to see in my reports for the following scenario - user visits a.site > b.site
A. 1 Unique visitor, 1 visit, in my profile reports ?
B. The movement between a.site.com to b.site.com would count as a direct visit? or would it be considered as referral and i'll be able to see the source of traffic as one of these sub domains?
Thanks...
April 7, 2009 11:40 AM
When you use pageTracker._setDomainName("example.com"); you are telling Google Analytics to set cookies for example.com rather than a.example.com or b.example.com. This means that the same cookies will be used as the visitor goes from a.example.com to b.example.com. So A would be true: you would have 1 unique visitor and 1 visit in your profile reports.
April 7, 2009 12:36 PM
This is a great post and the comments are extremely useful.
I am working with a company that is facing a self-referral issue. Their site is set up with partitions so that their website URLs are:
www.domain.com
www.us.domain.com
www.euro.domain.com
We installed the GA code as specified for subdomains, but in google analytics, the reports are showing the following line items as referrers:
domain.com
us.domain.com
euro.domain.com
Have you seen this problem before? Any ideas for how to solve it?
July 3, 2009 8:17 PM
@Matt: If you had Google Analytics Tracking Code up for a while without the proper modifications for subdomains, you will still see "left-over" traffic from your subdomains for a while after implementing subdomain tracking. This should diminish over time.
July 6, 2009 8:28 AM
Just to close the loop. We solved the issue this week. Here is the relevant Google Analytics Help Page:
http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55532
July 10, 2009 1:44 PM
Oops.. got cut off.
We enabled the following line in the code and it solved the problem:
pageTracker._setAllowLinker(true);
July 10, 2009 1:45 PM
@Matt: Do you have multiple domains in addition to subdomains or do you just have subdomains?
July 10, 2009 1:57 PM
Hi, Jeremy! Nice post!
I have a doubt about what you've wrote. Should I use
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._setDomainName("domain.com");
pageTracker._initData();
pageTracker._trackPageview();
</script>
or
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._setDomainName(".exemplo.com.br");
pageTracker._initData();
pageTracker._trackPageview();
} catch(err) {}
</script>
August 20, 2009 3:08 PM
@Felipe: The Google Analytics Tracking Code has been updated several times since this post. It would be quite the nightmare to go back and update all of our posts every time an update came out, so we tend to leave them as is.
The correct implementation of subdomain tracking, using the latest version of the Google Analytics Tracking Code, is the following:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._setDomainName("domain.com");
pageTracker._trackPageview();
} catch(err) {}</script>
The main changes from the post are that the deprecated _initData is no longer used and that the main body of the script is now wrapped in a try-catch block.
August 21, 2009 8:52 AM
On the google site, they indicate you should set the ._setDomainName to .domain.com
See this article: http://bit.ly/11Edsu
What is the difference?
August 31, 2009 5:39 PM
There's no real difference between using the leading period or leaving it off. The important thing is to be consistent. For example, I always leave the leading period off since that's what I'm used to, but if I'm working with someone who is using the leading period, then I will as well.
Also, if you don't use the leading period, you don't necessarily have to use _setDomainName for the main domain (www or non-www). I personally don't advantage of this, rather I use _setDomainName across all subdomains, including the main sudomain. If you use the leading period, you must use _setDomainName across all subdomains, including www and non-www.
August 31, 2009 6:05 PM
With this setup, I understand the subdomains use the exact same GATC as the "master" domain, and are tracked as a unit by the common setDomainName value.
So I guess it is safe to assume that if a subdomain has already been established as a "new domain", with its own unique UA number, all previously captured data will be lost after changing to this arrangement?
February 3, 2010 11:50 AM
@miked: It's not exactly lost unless you delete the old profile, but there won't be an easy way to connect the data after switching to subdomain tracking to data before switching. The other thing to consider is that the cookie data for that subdomain will basically be reset. The Google Analytics Tracking Code will create new cookies at the root level domain and basically ignore the cookies set at the subdomain level.
You could, of course, write some custom code to try and save your old referral data by copying the old cookie data to the new cookies, but this is usually more trouble than it's worth, especially since you are moving to a new system of tracking where you no longer expect to see referrals from the same subdomain.
Usually it's worth the temporary loss of data, however, in order to be able track visitors across subdomains.
Receive new blog posts immediately direct to your email inbox!











I haven't really found that to be the case. I finally got GA working with that setDomainName variable so that it reports both domain and subdomain to the same profile, but I still get the subdomain as the top referrer. Not sure what I'm doing wrong, but I've found the GA code to be a little temperamental.
March 27, 2008 10:09 PM