Tracking Sub Domains with Google Analytics

November 19, 2007 by Justin Cutroni

Tracking websites that have multiple sub domains may not seem like a complicated thing. In fact, the GA support docs clearly outlines two ways to track sub domains. You can track all sub domains in a single profile or you can track sub domains in separate profiles.

But what if you want more flexibility? What if you want to track one group of sub domains in one profile and a different group of sub domains in another profile? Or what if you want to create new profiles for a different groups of sub domains in the future?

I like to use an approach that let’s you decide how you want to group sub domain traffic. This setup will give you the most flexibility now and in the future. Some of the benefits include:

1. It let’s you collect all data in a master profile. This facilitates ‘roll up’ reporting and gives you an easy way to measure traffic to the primary domain and all of the sub domains.

2. This technique lets you track each individual sub domain in it’s own profile which is useful for deeper analysis.

3. You can easily create new profiles in the future based on groups of sub domains.

Here’s the exact process to get this working.

1. Create Profile for The Primary Domain

The first step is to create a master profile. All other profiles for the website, including profiles for the various sub domains, will be based on this profile. When you enter the domain for the website use the primary website domain. In the example below, I’m using www.sitedomain.com as the website domain.

Google Analytics Master Profile Settings.

After creating the profile GA will display the tracking code for the site. Before you add it to your pages it must be modified. You need to force GA to use the primary website domain for the cookie domain. This modification will let you track each unique visitor across all sub domains. This in turn let’s us collect all site data in a single profile and do ‘roll up’ reporting.

Old urchin.js tracking code:


<script type="text/javascript">
_uacct = "UA-XXXXXXX-1";
_udn = "sitedomain.com";
urchinTracker();
</script>

New ga.js tracking code:


<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-1");
pageTracker._setDomainName("sitedomain.com");
pageTracker._initData();
pageTracker._trackPageview();
</script>

Use this profile as a way to measure overall traffic across all of your sub domains. To facilitate page analysis in this profile add a filter that concatenates the hostname to the Request URI, which is the piece of data in the Top Content report. This will help you differentiate traffic to pages with the same URI that may reside on different sub domains.

20071118-hostname-request.png

A handy report that you’ll want to use in this profile is the Visitors > Network Properties > Hostnames report. This report will show traffic to the primary domain and all of the sub domains. It’s an easy way to compare traffic to the various sub domains.

2. Create Profiles for All Sub Domains

Once you have created the master profile you can start creating profiles for each of the sub domains. Remember, each sub domain profile is based on the master profile, that we created in step 1. Here’s how to set up the new profile:

20071118-sub-profile.png

Now that you’ve created the profile, make sure you add the tracking code to the pages on the sub domain. The tracking code for the sub domain is the same as the tracking code that goes on the primary domain (it’s the code from step 1).

3. Add Filters to Sub Domain Profiles

The next step is to add a filter to the sub domain profile so it only contains data for the appropriate sub domain. This is done using an include filter based on the hostname. Once this filter is in place you’ll have the master profile which will contain data for all sub domains, and this profile that will contain data for a specific sub domain.

Filter to create a GA sub domain profile

To create additional profiles for other domains just repeat steps 2 and 3. Obviously the settings for the filter used in step 3 will depend on the sub domain that you want to include in the profile.

Creating Additional Profiles

Let’s say you need to create a profile for two sub domains: 1.website.com and 2.website.com. This can easily be done because the same tracking code is on both sites. All you need to do is add an include filter with the following settings:

GA Filter settings for 2 sub domains.

While Google’s documentation describes a good way to track sub domains, I think this approach is better due to it’s flexibility. Plus, because the same tracking code is used all all sub domains you don’t need to worry about placing the wrong tracking code on a sub domain.

Good luck with your setup!

Subscribe:

Tracking Clicks with GA Pt. 3: Advanced Implementation

April 9, 2007 by Justin Cutroni

Welcome to Part 3 of Tracking Clicks with Google Analytics! Last time I discussed the process and theory behind tracking visitor click data in GA. This technique is primarily used to track non-HTML files and outbound links. In this post we’ll discuss a quicker, easier way to implement the click tracking. Please make sure you’ve read Part 2 before reading this post.

Overview

Whenever we need to track clicks we use the following JavaScript code. This code automatically adds the urchinTracker function to appropriate part of your HTML at the appropriate time. With this script there is no need to change the HTML in your pages. You simple add this script to all the pages on your site and it does all the work for you.

You’re probably wondering how this all woks. I’m not going to get into the nuts and bolts, but this uses the DOM or Document Object Model. Using the DOM in the code gives us the ability to modify the HTML in real time.

About the Script

This script will only track clicks on anchor tags. It will not track clicks on images or other HTML elements.

If the anchor tag points to a different domain then urchinTracker() is executed before the visitor leaves the site and a pageview named ‘/outbound/<url>’ is created in Google Analytics. The <url> part is replaced with the value from the HREF attribute in the anchor tag.

If the anchor tag points to a non-HTML file then the scrip creates a pageview named ‘<path>’ where <path> is the value in the HREF attribute of the link.

Here’s an example of the script in action. The link below will take you to a test page where you can click on links to other domains and files Rather than send the data to GA, the script has been modified to display a message (note: a new window or tab will open in you browser).

GA Click Tracker Test Page

To view the code just look at the source of the above page. Right click on the page and choose ‘View Source’.

Installing the Script

Installing this script is simple. Copy and paste the code from the test page and place it in your web page. I place it before the closing BODY tag. After the script has been installed you can configure it to work with your site. Also, make sure that the Google Analytics tracking code is installed on the page.

Customizing the Script

There are a few customizations you can make to the code.

First, there is a variable named debug. When it is set to 1 the script will display a pop up window when a link is clicked. Set debug to 0 to disable the pop up. By default, debug is set to 1 (on).

Next, you can define the types of non-HTML files that this script will track. Find the variable named fileTypes. The value should look like this:

(".doc",".xls",".exe",".zip",".pdf")

To track additional file types just add the extension to the list. Make sure you match the text-formatting of the existing list.

Finally, you can change the way external links appear in the GA reports. By default, all external links will be preceded by ‘/outbound/’. If you would like them to appear differently change the extIdentifier variable.

Summary

There are lots of scripts like this. This is just our version. I hope it gives you some idea of what is possible and inspires you to create one of your own or modify ours. Just remember that this script will create additional data in your GA profiles. You many need to add a filter to deal with the extra data.

Please feel free to use and modify this script. The only thing we ask is you share the changes with others.

Subscribe:

Tracking Clicks with GA Pt. 2: Basic Implementation

March 23, 2007 by Justin Cutroni

In Part 1 of this series I discussed urchinTracker: how it works and what it does. Today we’re going to put that knowledge to use and learn about tracking visitor clicks.

The concept behind tracking clicks with Google Analytics is simple. We capture the visitor’s click by modifying the page HTML and then send the click to Google Analytics in the form of a pageview. Because the click is stored as a pageview we can find the data in many Google Analytics reports.

Implementing click tracking is a two step process:

1. Capture the visitor’s click

2. Tell Google Analytics how to record the click

Here’s more detail about the process.

Basic Installation Process

1. Capture the Visitor’s Click

clickerhand.jpgWhen a visitor clicks on a web page, the web browser captures the click. When I say ‘captures’ I mean that the browser knows that the user performed a click and what they clicked on. We can tell the browser to take some type of action at the exact moment when the click occurs. Here’s an example. When you click on this link a pop up message will appear.

How does it work? I made a small change to the HTML that ‘told’ the browser to pay attention when you clicked on the link. The browser does not know to pay attention to the click unless you tell it to. Normally we don’t care about every single click, so we don’t tell the browser to pay attention.

Let’s look closely at another example, an image tag. Here’s a standard HTML image tag:

<img alt="My Image" src="/foo.jpg" />

Here is the same image tag that has been modified to capture a visitor’s click:

<img onclick="" alt="My Image" src="/foo.jpg" />

See the ‘onclick’ part of the image tag? It’s called an attribute and it’s the part that tells the browser to listen for the click. The onclick attribute can appear in almost any HTML tag. That means that we can capture the click when a visitor clicks on almost any part of your web page.

The onclick attribute is also the place where we specify what action we want the browser to take when the click happens.

2. Telling Google Analytics What Happened

Now we know how to to tell the browser to capture a click AND how to respond. What do we want to happen when the click occurs? We want to create a pageview in Google Analytics that identifies the visitor’s click. How do we create pageviews in Google Analytics? With urchinTracker :) See, it’s all coming together.

Let’s cut right to the chase. Here’s how to track a click on an image:

<img alt="My Image" onclick="javascript:urchinTracker('/clicks/images/foo.jpg');" src="/foo.jpg" />

We’ve added a ‘reaction’ (urchintTracker) to the onclick attribute. Here’s the entire process that follows a click on the above image tag:

  1. The browser captures the click
  2. The browser responds to the click by running (or executing) the urchinTracker function
  3. urchinTracker creates a pageview in Google Analytics named ‘/clicks/images/foo.jpg’

Don’t worry about the ‘javascript:’, that just tells the browser that the function is a JavaScript function.

The beauty of this system is that the onclick attribute exists for almost all HTML elements. This means you can apply the above example to almost any piece of HTML.

Implementation Examples

20030323-pdf-image.jpgThe most popular application is tracking clicks on links, also known as anchor tags. Why? Because anchor tags link to many objects that can not be tracked by Google Analytics. Two of the most common are non-HTML files (like PDF, MP3, etc) and links to other websites site (I call them outbound links). Here’s an example of tracking a link to another site:


When someone clicks on the above link, a pageview will be created in Google Analytics. The pageview will be named ‘/outbound/lunametrics.blogspot.com’.

And here’s an example of tracking a link to a non-HTML file:

<a href="/files/pdf/trail-map.pdf" onclick="javascript:urchinTracker('/files/pdf/trail-map.pdf');">trail map</a>

This PDF will be tracked as a pageview named ‘/files/pdf/trail-map.pdf’.

One thing to note is how I am structuring the name of the pageview. For the outbound link I added ‘outbound’ to the pageview name. It’s a good idea to have some type of naming convention for your non-HTML files and outbound clicks. It will help if you need to create a filter or want to easily sort the data in GA.

You’ve probably realized there is a downside to this tracking method. What happens if you have a huge number of clicks that you want to track? For example, what if your website has 1000 PDF files and you don’t want to modify all of those links? I have a solution for this and will cover it in my next post.

A Note About Your Data

You’re probably wondering where all this click data is stored. Remember, we’re creating pageviews. So any GA report that shows pageviews will show the visitor clicks. Here’s an example, the top content report:

20070323-outbound-example.jpg

One thing to consider is how your profile data will change if you start tracking clicks with urchinTracker. urchinTracker creates pageviews, so the number of pageviews in your profile will increase. If you need an accurate number of true pageviews then make sure you use an exclude filter to massage the appropriate data.

Here’s how an exclude outbound clicks filter would look:

20070323-exclude-outbound-filter.jpg

So now you can see the importance of structuring the name of your pageview.

Final Thoughts
One thing I did not cover in this post is how to use urchinTracker to track non-HTML events. Certain technologies, like Flash, JavaScript and Ajax also create clicks but they can not be trackd in the manner described above. One does use urchinTracker to track these technologies, but the implementation is different.

Next up, I’ll talk about an advanced way to track visitor clicks.

Subscribe:

Tracking Clicks with GA Pt. 1: About urchinTracker()

March 19, 2007 by Justin Cutroni

Welcome to Part 1 of How to Track User Clicks using Google Analytics. Before we learn how to track clicks we need to review some of the technology behind Google Analytics. In particular, we need to understand how GA creates pageviews. Why? Because we will use the same technology to track clicks. This post may seem off topic, but it lays the foundation for the next post in the series.

urchinTracker: A JavaScript Function

It all starts with the Google Analytics tracking code:

2007-03-16-tracking-code.jpg

I’d like to point out the second to last line. It contains a JavaScript function called urchinTracker. For those of you that don’t know what a function is, it’s a piece of code that actually ‘does stuff’.

There are two important features of a JavaScript function:

  1. It can appear almost anywhere in your web page. There are certain limitations, but in general you can move it around.
  2. It can appear more than once.

Applying these characteristics to urchinTracker means we can place urchinTracker almost anywhere in a web page AND we can include it multiple times, two important concepts.

urchinTracker: What it Does

We know that urchinTracker is a function and it ‘does stuff’, but what exactly does it do? While visitors engage with your website, urchinTracker is collecting information in the background. It identifies things like:

  • Where the visitor came from
  • How many times they’ve been to your site
  • What content they are viewing
  • Characteristics of their browsing environment (operating system, browser, etc.)
  • The title of the current website page
  • etc.

Please note that urchinTracker is not collecting any personally identifiable information. It is collecting generic information about the visitor.

After it collects the data, it sends it to Google Analytics in the form of a pageview. So every time urchinTracker runs, or as programmers say ‘executes’, it creates a pageview in Google Analytics. And, because urchinTracker is a function, we can add it to a web page multiple times. This means we can create multiple pageviews in Google Analytics every time a single page loads in the visitor’s browser.

‘Naming’ Pageviews with urchinTracker

Another important concept to understand is how urchinTracker ‘names’ pageviews. When I say ‘names’ a pageview, I mean the way that the pageview is represented in the GA reports. Let’s look at some pageviews in the Top Content report:

2007-03-18-top-content.jpg

Everything in the ‘Content’ column was created by urchinTracker. Here’s how:

  1. urchinTracker extracts the information from the location bar of your browser.
  2. It modifies the value to show only the name of the directories, file and query string variables.

Here’s an example. This URL:

20070315-location-bar.jpg

would appear in the top content report as this:

20070315-top-content-uri.jpg

That’s the default behavior of urchinTracker. Recapping what we’ve learned; whenever a page loads in your browser, urchinTracker collects all the necessary information, creates a pageview name from the location bar and sends the data off to Google Analytics in the form of a pageview.

Important Stuff Below, Pay Attention :)

What’s really cool is that we can over-ride the default behavior and tell urchinTracker how to name a pageview. For example, let’s say we want to change the way the pageview for ‘/index.php’ is named in GA. We would modify urchinTracker as follows:

urchinTracker('ski-bum');

This modification forces urchinTracker to name the pageview ’ski-bum’ rather than ‘/index.php’. This means that when you view the Top Content report you’ll see ’ski-bum’ and not ‘/index.php’. Whatever you place in the parenthesis of urchinTracker becomes the name of the pageview in Google Analytics.

Summary

There are a few important concepts to take away from this post:

  1. Google Analytics pageviews are created by a JavaScript function named urchinTracker
  2. urchinTracker can appear almost anywhere in a web page
  3. urchinTracker can appear multiple times in a web page
  4. By slightly modifying urchinTracker we can change the ‘name’ of pageviews in Google Analytics

In Part two of this series we’ll put our knowledge of urchinTracker to work and talk about tracking visitor clicks.

Subscribe: