Thursday 18 February 2010 | 11:47
You may remember that back in November 2009 we announced multiple custom variables. While at first look this feature may appear a little daunting, in this post I hope to explain how you can use custom variables to enhance the insights you get from Google Analytics with just a couple of extra lines of code.
Custom variables give you the ability to store data about metrics and dimensions that are not a part of Google Analytics as standard. Google Analytics has an excellent set of default metrics, but what if you want to confirm certain user behaviour or trends on your website are taking place and can't confirm this with the default set of metrics? You can use and set custom variables when a user takes a certain desirable action and use the information to analyse that visitor's future activity on your site. For example, your company offers a free trial period to your online training course. You could set a custom variable when visitors sign up for the free trial, then see what percentage of those that signed up for the trial later returned to buy the full course.
You can use custom variables to track a multitude of additional data. What that additional data is can vary dramatically, and really depends on what your specific data needs are. It could range from tracking of staff visits to non-staff visits, to if users selected they want a call back on a form. Remember you will need to make code changes and do a little planning to get your custom variables set up so remember to focus on useful data. That is, data that goes beyond what's nice to have/know and actually results in you taking concrete actions to optimise your site or marketing efforts.
In this post I'm going to discuss some examples of how you might use the different custom variable types to gain extra data about your users behaviour. The first two are more relevant to e-commerce sites while the third is more applicable to a content oriented, advertising driven site. For the sake of this article let's pretend all three are being set for the same site. The three examples are:
- How many people repeat buy your products
- Which shipping method buyers choose from a drop down
- Group the data for the different sections of your site together to analyse the pages in the sections as a whole
Format for custom variables
Before we get started I wanted to quickly recap how the new format for custom variables works. You will set custom variables in your code prior to the _trackPageview() or trackEvent() methods using the following notation:
_setCustomVar(slot, name, value, scope)
- Slot: is the slot number of the variable. There are five available slots (1-5) for each site, so be careful not to overwrite existing slots when you set a new custom variable. As the buyer variable is the first for our site we are going to allocate it the first slot. Note: In reality you can reuse slots that have a pageview or visit level scope but that can quickly become confusing. My advice is to stay away from this until you really have to. You can get more info on reusing slots here
- Name: is the name of the custom variable. So to capture buyers we may want to to name the variable 'Buyer'
- Value: for a given name we will probably have at least one value. For our buyer example we will set their value as 'yes' when they buy something.
- Scope: is the scope of the variable. There are three scopes; visitor (1), visit or session (2) and page-level (3). A visitor (1) variable persists in every pageview and visit that the visitor generates. You use it when you want to track how people interact with the site site over multiple visits, its not a value that will change often. A visit (2) level variable persists for every pageview in a single visit or session but does not persist for repeat visits. You use a visit level scope when you want to track something that happens once in a visit but you don't want it be associated with repeat visits. Finally a page-level (3) variable persists for that page only so it can change from page to page. What scope you use depends on how long you will want the data to remain for. For our buyer example we will want to track visitors across multiple visits so we will use a visitor level scope.
Understanding buyer behaviour
So here is our completed code example for the buyer custom variable:
_setCustomVar(1, "Buyer", "yes", 1)
We will place this code on our receipt page (before any calls to trackPageview()). Once someone buys something they will be labelled as being a buyer for all subsequent visits (as we used the visitor scope). We can then track how many buyers return to the site and if they buy again. We can see this information in the Visitors > Custom Variables report. Using advanced segments or a custom report we can also see what products they buy.
If repeat customers on the whole tend to buy different products than the first time buyer, we can use this information to up-sell those products in our email campaigns to existing customers. If we discover that not many customers repeat buy, we can work on our retention program. For example we could offer a money-off coupon on related items to existing customers.
What shipping method do visitors choose?
The second piece of data we want to capture is what shipping method users choose. As we want to associate a shipping method with a visit and users may pick different shipping methods on different visits we will use the visit level scope. Users have a choice of three shipping methods in our form:
- Express £15
- 3 Day £7
- 7 Days £2
The three shipping methods become the different values and we set this in second slot so we don't overwrite the buyer variable. We will need to work with our developer team to alter the values based on the users form selection, this is a pretty small job for a competent web developer. This is what our code will look like:
_setCustomVar(2, "Shipping Method", "Express", 2)
We can now track which shipping method is the most popular and determine what our default shipping method should be. We can also ask questions like do certain shipping methods correspond with certain products? Maybe products from the gift category get a lot of express orders. If that's the case we could highlight our express shipping option on the gift product pages.
Which content on my site is most popular?
Finally we want to aggregate individual pages into distinct sections. Maybe we are a news site and want to see how the sports section compares to the news section or the business section. Assuming there is no easy way to do this via advanced segments our next best option is to use a custom variable. As the page section we set can vary from page to page within a given visit we will want to set this variable as having a page-level scope. We will need to set this code on every page within a section. Here is the code we will put on our sport pages:
Posted by Brian O'Sullivan, Google Analytics Team.
_setCustomVar(3, "Section", "Sport", 3)
And here it is for the news section:
_setCustomVar(3, "Section", "News", 3)
You may be wondering if a visitor first visits the News section of the site and then the Sports section, won't the Sports section value overwrite the News section? This would happen if we had used a visitor or visit level scope but with a page level scope the slot is only associated with one page and is free to use on subsequent pages so this isn't an issue. We can now see, at an aggregated level, how our different sections perform in isolation. If the Sports section is more popular than the News section maybe our readers are more interested in sport; should we put more sport stories on the front page? Or if the engagement metrics like Time on Site and Avg Pageviews for News are particularly low maybe there is an issue with the content of this section?
Extra data won't always give you straightforward answers but it will allow you to ask interesting questions which often lead to interesting tests and ultimately to better conversion rates.
Posted by Brian O'Sullivan, Google Analytics Team.