Add to Google Add to Bloglines Add to Newsgator Add to Yahoo! Contact ROI Revolution Sign Up for the ROI Newsletter Unofficial Google Analytics Blog Feed Unofficial Google Analytics Blog
Unofficial Google Analytics Blog by ROI Revolution

Categories Search This Blog Blogroll Archive

| |

March 27, 2008

Tracking Subdomains

By Jeremy Aube, Google Analytics Support Tech

Dive Subdomain, Dive!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.


Find The Secret To Solid, Sustainable Improvements To Your Website
Learn the insider secrets to making more money from your website than ever before – Guaranteed! Get started right away with the Google Analytics Training Course.

Posted by Jeremy Aube at 1:30 PM









Filed under:

TrackBack

TrackBack URL for this entry:
/mt/mt-tb.cgi/278.

Comments

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.

Posted by: Reid at March 27, 2008 10:09 PM

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

Posted by: Jeremy Aube, Google Analytics Support Tech Author Profile Page at 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.

Posted by: Benjamin at 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.

Posted by: Jeremy Aube, Google Analytics Support Tech Author Profile Page at 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

Posted by: Emiel Kamzol at 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.

Posted by: Jeremy Aube, Google Analytics Support Tech Author Profile Page at 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!

Posted by: Jesse DaCosta at 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.

Posted by: Jeremy Aube, Google Analytics Support Tech Author Profile Page at 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?

Posted by: Jesse DaCosta at 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.

Posted by: Jeremy Aube, Google Analytics Support Tech Author Profile Page at April 4, 2008 1:29 PM

Post Your Comments

Post a comment

Contacting ROI Revolution

Telephone Number:
(919) 954.5955 ext. 306
ask for Meredith Smith

Office Fax:
(919) 954.4767

Address:
3109 Poplarwood Ct.
Suite 219
Raleigh, NC 27604


Authorized Google Analytics Consultant

Google Analytics Certified Service and Support Consultant


Free Webinar!

Join a Live FREE Google Analytics Training Webinar
Register now to learn how to
turn Google Analytics into
pure profit! You'll learn how to use many of the key reports, a ten-step system for properly configuring Google Analytics on your website, and how to apply what you learn to the attainment of your online goals. Register for our free Google Analytics training webinar today.


Subscribe to the
ROI Revolution Google
Analytics Newsletter today

Free monthly email publication that contains valuable, practical tips, secrets, and much more! Subscribe to our free Google Analytics newsletter today.


Looking to Purchase
Urchin 5 Software or Fee
Based Support?

We are a Urchin software certified service and support partner! Buy Urchin 5 software, profile packs, load balancing modules, campaign tracking, or fee-based support today.