Mastering Custom Segmentation in Google Analytics

Jason Ford // Dec. 11, 2009

Google has made some major strides in adding new features to Google Analytics over the past year. I used to recommend Google Analytics for basic tracking needs and Omniture (which is expensive) for when custom user tracking or goal conversion was required. After spending some time playing with the new custom variables and segmentation features in Google Analytics, I think I can say that for the vast majority of websites I've worked on - even those run by large enterprise companies with complex requirements - Google Analytics is all you need. And the best part is, it's free.


Why custom data is useful

The most common event tracked in web analytics is the page view. A page view occurs when a user loads a new web page in their browser. Google Analytics - and most analytics packages for that matter - track these page views along with information about the visitor's operating system, web browser, and a few other details by default. For a simple website, this is all you need, but for more complex sites that feature shopping carts, user registration, or advanced application functionality, page views usually do not capture the entire story.

Events like logging in/out, registering as a new user, or editing user settings don't always create a trail of page views that explain what happened. On FeedMagnet.com, users enter their username and password at the top of the home page and are taken directly to their management page. All we get are two page views: home page » management page. We could assume they logged in as an intermediary step but, in fact, most of our users have a cookie set to remember them so they don't have to log in every time they come back to the site. I also have no way of knowing who is logged in and who is not unless a visitor goes to a page that I know they can only view if logged in.

Additionally, page views don't allow you to do any segmentation beyond things like "all visitors who have looked at the Features page" or "everyone running the Firefox browser" - segments which are not entirely useful most of the time. For users who have logged in to our site, I want to segment by type of account and even drill down to specific users to see when the last time was that they visited the site or completed a particular action.

Custom variables and segmentation in Google Analytics let you accomplish all of this advanced tracking - providing functionality that used to only be available in expensive tools like Omniture. You have to have technical skill to get it set up and working, but I've found it to actually be easier and quicker to implement than more "advanced" analytics tools, which often require a team of specialists employed by the analytics provider to get configured properly.

Step 1: deciding what to track

This step requires some critical thinking to determine what actions you are not already tracking via pageviews. You also need to think about what data would actually be useful to help you make decisions regarding the website and your business in general (just because you can track something doesn't mean it will be useful to you).

Google provides tracking for three types of data: information about a user or visitor, information about a specific visit that a user made to your site, and information about a single action a user made during a visit. In the case of FeedMagnet, I decided on the following items to track, in those categories:

  1. Visitor information
    • User type: FeedMagnet employee or customer
    • Account type: free, premium, etc.
  2. Session information
    • Was the user logged in at any point during their visit to the site?
  3. Specific actions
    • Log in
    • Registration (including account type)
    • Newsletter signup
    • New author/topic creation, editing, and deletion in the manage area

Step 2: planning how to use your 5 slots

Google gives you five slots to store custom variables in. This limitation is due to the way the variables are stored in cookies on the user's computer. It is important to plan out how you are going to use these five slots. Each slot can only hold one piece of data at a time, and if you try to put data in a slot that already has something in it, Google will just throw away the old data and replace it with the new. So, most of the time, you only want to put data in slots that are already empty - which means you need to understand how long each type of data sticks around.

Each of the three variable types from above has a different lifespan:

  1. Visitor information: stays in the slot forever - even if they leave and return days later
  2. Session information: removed when the browser is closed, or after 30min of inactivity
  3. Actions: removed after tracking each page view

This diagram shows how that works visually:

Custom variable types in Google Analytics

So, if you want to track visitor information, you really need to cordon off a slot for it and be sure not to use that slot for anything else. That is pretty much true for session information as well, but you have some flexibility in that you can store different things in the session slot each time the visitor comes to your site. Learn more about how this works »

For FeedMagnet, I came up with the following plan:

Custom variable slots in Google Analytics

We're using slots 1 and 2 to track information about the user and whether they were logged in each session. Slot 3 is our primary action slot for events that aren't necessarily tied to a page view. We don't really need more than these 3 slots right now, but we are keeping slots 4 and 5 open in case we need to pass supporting data to go along with a slot 3 event.

Step 3: Implementing the tracking code

Because custom variables are tracking actions that happen programmatically on your site, implementation is going to be different for everyone, depending on the architecture of your site and the programming language being used. I'll save the details of how we did our implementation in Django and Python for another post and just talk about the theory of how it works here.

The way we set up analytics on FeedMagnet.com was to create a queue of analytics data that we want to track. The queue is specific to each visitor and is stored as a session variable in Django related to their session. Each time a page is loaded by the visitor, we grab all the data that has built up in the queue, add it to the Google Analytics tracking tag, and put the tag in the HTML before delivering it to the user.

In this way, we can set custom variables at any time (not necessarily while loading a page), but all the variables that we set end up being sent to Google as part of a page view. And each time we do this, we clear the queue so we can start with a fresh slate for the next page view.

Variables are added to the queue with this line of Python:

track_analytics(request.session, scope="action", name="registration", value="free")

The example above adds a custom variable to the queue in slot 3 named "registration" and set to "free", which is the new user's account type. The logic controlling the slots each variable goes in to is stored in the our track_analytics() method so that we don't have to specify them when calling the method.

Our standard Google Analytics tracking snippet looks like this:

<!-- Google Analytics --> 
<script type="text/javascript"> 
    var _gaq = _gaq || [];
    _gaq.push(["_setAccount", "UA-9040781-1"], ["_trackPageview"]);
    (function() {
        var ga = document.createElement('script');
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        ga.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(ga);
    })();
</script>

But once we've added this new variable to the queue, the next page view will include a snippet that looks like this:

<!-- Google Analytics --> 
<script type="text/javascript"> 
    var _gaq = _gaq || [];
    _gaq.push(["_setAccount", "UA-9040781-1"], ["_setCustomVar", 3, "registration", "free", 3], ["_trackPageview"]);
    (function() {
        var ga = document.createElement('script');
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        ga.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(ga);
    })();
</script>

Note that we are using the newer asynchronous tracking snippet from Google Analytics since it is slightly more efficient and helps our pages to load quicker without having to wait for the analytics exchange to finish successfully.

Step 4: Setting up custom segmentation

Once you start tracking data, you need to wait a day or so to collect enough data that you'll be able to see if your segmentation is working properly. It would be nice if Google provided real-time access to tracking data, but unfortunately they do not. Once you've waited long enough, log in to Google Analytics.

The first thing we want to do is make sure the data is being tracked correctly. We can do this by selecting Visitors in the side menu and then choosing Custom Variables from the sub-menu. If everything is working correctly, you should see a list of the variables that you've set with a drill-down interface that lets you click on a variable and see all of the values that have been set to it.

Google Analytics - Custom Variable List

Once you are sure your data is coming in correctly, its time to set up some custom segments to really see the power of the custom variables. Open up the advanced segments drop-down and select "Create a new advanced segment" to get started.

Google Analytics - Create Custom Segment

You should get a screen that looks like this - its Google's custom segment configuration tool, and it is pretty dang awesome:

Google Analytics - Custom Segment Settings

Google Analytics - Custom Variables Now, there is a lot you can do here - and Google provides a lot of documentation on how to use this powerful segmentation engine - but what we are most concerned about right now are our custom variables. Open up the Visitors drop down in the Dimensions sidebar section. Toward the bottom, you'll see Custom Variable (Key 1...5) and Custom Variable (Value 1...5) listed. These corresponds to the variable names and values that you set and are numbered based on the slots you used when setting them.

Because we are setting a session-level variable for "authenticated" we can now set up a segment to show us all visits to FeedMagnet.com where the user was logged in. Our "authenticated" variable is set in slot 2, and the value is always set to "true", if it is set at all - so I'm less interested in the value and more interested in the presence of the variable in general. I'll grab the Custom Variable (Key 2) and drag it over to the "dimension or metric" box. I want it to match exactly "authenticated" because that's what I named the variable.

Google Analytics - Custom Segment Settings

Next, I can name the segment "Authenticated Users" down below and save it.

Google Analytics - Custom Segment List

Let's do one more. Prior to setting up custom variables and advanced segmentation, we used to filter out all traffic to FeedMagnet.com from the IP addresses where our staff work. This completely removes data collected from those IPs from Google Analytics, but it does not work when we are at coffee shops or airports since those IP addresses aren't registered to be blocked by Google (and we wouldn't want them to be!).

Since we are storing a visitor-level variable in slot one that includes the user type ("employee" or "user"), we can just set up a custom segment to show us all activity by non-employees. Our variable in slot one is formatted like this: "employee" or "user-free" - where the first part of the value is always "employee" or "user" and the second part, only used for non-employees, contains the account type (free, silver, gold, etc). For this segment, we'll drag Custom Variable (Value 1) over to the "dimension or metric" box.

Google Analytics - Custom Segment Settings

I want to make sure this value does not start with "employee" so I just fill that in, name the new segment "Non-employee" and we're good to go.

Google Analytics - Custom Segment List

Step 5: Viewing segmentation in reports

Google Analytics - Custom Segments List Once you have all your segments set up the way you like them, go back to the dashboard. This time, when you open up the Advanced Segments drop down, you should see your new segments listed in the Custom Segments box. Select a couple of them and hit the Apply button. Now, all of your data in Google Analytics will be filtered through those segments. Even better, since you selected more than one, you can compare your segments side-by-side.

Google Analytics - Segmentation Data

The flexibility of this system is what makes it so powerful. With a little strategic thinking about what should be tracked, some custom programming to get the tracking snippets all set up, and a little fiddling in the Google Analytics interface itself, you can have detailed segmentation for all aspects of the activity on your site available at your fingertips.

And did I mention all of this is free? Way to go Google.