Custom Variables in Google Analytics: Real Examples
One of the great features of Google Analytics is the ability to segment users. You can segment by any possible metric that Google Analytics captures, from type of traffic (direct or referral) and if you have e-commerce enabled, visits with transactions. Also hidden within Google Analytics, is the ability for special segmentation that you have complete control over–the only limit is your imagination. It is known as the Custom Variable. You can find that report under Demographics in the Google Analytics interface. Don’t be alarmed if you see it’s blank. We will explain what Custom Variables are and how get some data and use it to get some awesome insights.
I know Custom Variables are not new (they replaced user defined variables two years ago). Some of you might already be using them effectively as well. Personally, I was a bit apprehensive about Custom Variables because it always seemed to be complex. It’s not that I did not understand the purpose of Custom Variables, or the theory behind it, but it was due to the lack of implementation documentation. I could never find examples that showed how to actually use on my site. After playing around with custom variables for the past year, I decided it was time to share my code and what I have learnt so you too can take advantage of custom variables.
Let’s start with the simple. What are custom variables in Google Analytics? The custom variable in Google Analytics is a code you can place on a page, to segment a user based on criteria you determine. For example, on your site, if you have a sign-up form and you are getting demographic data such as age or gender, you can import this data into your analytics. Another example might be if you have a a membership site, and you want to know how your logged in users experience your site, versus those that do not have a membership. This is where Custom Variables can help.
Before we get dirty with some code, let’s just review some of the important aspects of Google’s Custom Variable documentation. As explained by Google, there are three levels (or scopes) you can use a custom variable:
- Visitor—the client that visits the site, such as the browser or mobile phone operated by a person.
- Session—the period of time during which the visitor is active on the site.
- Page—activity on the user’s behalf which sends a GIF request to the Analytics servers. This is typically characterized by a pageview, but it can include:
- a pageview
- an event (e.g. click on a movie button)
Having laid out the scopes, take a moment to read Google’s recommendations, when to use (and not use) custom variables. Actually, it really makes sense to read the whole document, so I would review it quickly before you read on. It will only take a moment so, go ahead and read it. I would also pay special attention to the usage information, as I will assume you have a understanding of this going forward.
Okay, ready? Before we get into the code, let’s look at the construction method of the a custom variable: _setCustomVar(index, name, value, opt_scope).
There are four parameters:
- index—This is slot for the custom variable and is required. You get get 5 slots that can range from
5, inclusive. A custom variable should be placed in one slot only and not be re-used across different slots. If you not careful they can be overwritten.
- name—The name for the custom variable and is required. Take a moment to think about this. It is the top-level term which identifies the custom variable. Think gender or membership level.
- value—The value for the custom variable and is required. This is a value that is paired with a name, so for example if you measure Gender, the value would be male or female.
- opt_scope—The scope for the custom variable and is optional. The scope defines the level of user engagement with your site. It is a number whose possible values are
3(page-level). When left undefined, the custom variable scope defaults to page-level interaction.
I know up to now we have been talking in theory, and for most that is fine, but I am very visual. So here is an example of a complete custom variable:
_setCustomVar(1, gender, male, 1).So far so good? It all made sense to me too, until I tried to actually implement. For the life of me, without some code example, I was lost. I blame it on the fact I am not a “certified” programmer. So for those like me who are tinkerers let me explain the next steps or where we go from theory and examples to actual implementation. What you need to do, is actually bring in the information for name or value from your MYSQL, your database or via some PHP trickery. Below are some actual code examples that show it in action, so you can see what I mean.
First is my “gender & age” code example. Using a simple form a user enters the data about their age and gender. On submit, they are sent to a page which shows the data they have entered. PHP code calls the info which at the same time enters the data into Google Analytics (lines 5 & 6 of the code).
The second example, is much more flexible and something that can be modified to your hearts content. Basically, it works by reading the url structure of your site for a specific term, and then segmenting users into two groups. Here is the example code:
As you notice in my example code, we are trying to segment a site based on consumers who segmented themselves by company type, B2B or B2C. Those visitors who are visiting B2B section will be tagged as B2B customers by a cookie. If they are not on a B2B folder, they are termed to be B2C customers. You could also leave the “else” as blank as well.
As I mentioned the code above is very flexible, but I wanted to ensure I added a clear warning with this solution. With custom variables, the last variable to be called is the one applied. For example a customer who visits B2B and than B2C, will be considered a B2C customer. For this reason make sure you work though the use case for your site. So, for example, you could tie folder areas to registration, so the B2B users only see B2B content. Whatever you decide,
I hope this framework helps you build something fun.
Last, but not least, I wanted to offer some direction for those using WordPress and want to play with Google Custom Variables. You can use the conditional tags to allow you to segment users based on tags or categories users visits. Below is an exmaple where we are using a custom variable segmenting users subscribers to “category id 7” on our blog.
There you have it. After setting up your custom variables, you will be able to start seeing them within a couple of hours, and use them to view user behaviour on your site, include them in advanced segments and add a bit of color to your analytics. Truthfully, the applications are limited only by your imagination and the data your users are willing to share.
One last bit, I wanted to add a tip for those who may have issues in getting custom variables to record data. Usually where the custom variable does not get recorded is in the set-up. Basically, you need to remember, try to bundle custom variable with the trigger of
_trackPageview (as you saw in the folder example). If there is a form where they fill in info or login, you will need to set another
_trackPageview() request after the user sets a custom variable (like in the gender example above). One other issue may be the need to escape certain characters,such as an apostrophe (like in the WordPress example).
So what are you using custom variables for? What creative segmentations do you have? What are your favourites? Are there other aspects of Google Analytics you’d like me to explore? Let me know in the comments…Print This Post