I’ve got a stack of posts that I want to write, and realized that the all deal with Custom Variables. So, to make sure that we’re all on the same page when it comes to custom vars, here’s my guide to Mastering Google Analytics Custom Variables.
For those of you that have not used custom variables, CVs are a way for you to insert custom data into Google Analytics. What kid of custom data? Almost any kind of data. As long as the data is available to you in the source code of a web page you can pass it to Google Analytics via a custom variable.
There are 4 parts to a custom variable:
1. The name of the variable
2. The values for each variable
3. The index or slot of the variable and the scope of the variable
4. The scope of the variable
Name & Value
Custom variables are name-value pairs of data. The name is also called the “key”. To create a custom variable you define the name of a custom variable, like Favorite Baseball Team, and then assign it different values, Red Sox, Braves, Giants, etc.
Google Analytics will show you a list of all the custom variable names in a list and then let you drill down into the list and see all of the values.
Here’s an example. I use a custom var named “Year” to track the publication year for all my posts on this blog. I can view which year had the most popular content by finding “Year” in the Visitors > Demographic > Custom Variables report.
Then I can click on “Year” to a get a list of all the values:
Custom variables can also be used in custom reports and advanced segments. The name of the custom variable becomes the “Key” dimension and the value of the custom variable becomes the “Custom Variable Value” dimension.
Index or Slot
The index is a way to organize your custom variables. Index is also referred to as “slot”. At a basic level, you get 5 custom variables. But this can actually be misleading. Think of the index as a parking lot with 5 parking space. You can put or “park” a custom variable in each space.
You can technically have more than 5 custom variables, but we need to discuss the next concept, scope, and how it impacts the index.
The key to mastering custom variables is understanding the scope. The scope of a custom variables determine how long the custom variable will persist.
If we use the parking lot analogy, the scope determines how long a car will stay parked in the custom variable parking lot.
There are three different scopes for Google Analytics Custom Variables:
Page Level: A page level custom variable will only persist for as the visitor is viewing the page where the custom variable has been set. Once the visitor moves to a new page the custom variable will expire.
Visit Level: A visit level custom variable will persist for the visitor’s entire visit. Once the visit ends the custom variable will expire.
Visitor Level: A visitor level custom variable will persist for 24 months as a cookie on the visitor’s machine until the visitor deleted her cookies.
Where scope really gets complicated is when we use it with the index. Remember, you have 5 slots that can hold a custom variable. Once you place a custom var in a slot that SLOT BECOMES FILLED and you can not place another custom var into the slot.
Scope and index work in tandem to determine how many custom variables you have available at any given time.
Let’s start with a visitor level custom variable.
When you place a visitor level custom var in a slot that slot remains filled until you delete the custom var. Remember a visitor level var is stored as a cookie so you have to actually delete the cookie. So, if you set a visitor scoped custom variable in slot 1 you can NEVER use slot one again. Unless you want to erase the custom var you set in slot 1.
Every time the visitor comes back to your website slot 1 will be filled because there is a cookie (named __utmv) on the visitor machine.
PARKING ANALOGY: There is a car in parking space 1. The car has no tires… and no engine… it’s not going anywhere soon… unless it gets towed.
When you create a visit level custom variable and place it in a slot, that slot remains full for the entire visit. Once the visit ends that slot opens up and you can place another variable in that slot.
Unlike the visitor scope custom variable there is no cookie. So when the visitor comes back to your site that slot is open.
PARKING ANALOGY: There is a car in a parking space 1, but just for the day. The spot will be vacated at the end of the day and someone else can park there.
When you create a page level custom variable and place it in a slot that slot remains filled until the visitor moves to another page. Once the visitor moves to another page the slot opens up and you can place another variable in the slot.
PARKING ANALOGY: There is a car in a parking space 1, but someone is just running into a store. The engine is idling and the spot will open up in 5 minutes.
Let’s look at a couple of examples:
Example 1: A visitor on a news site logs in. I set 5 visitor scoped custom variables. This means that whenever the visitor comes back to the website ALL five slots are filled. I can never use any other custom variables unless I erase or over-write one of the existing variables.
Example 2: A visitor is on a blog. Each blog post is categorized 3 ways using a page level custom variable. on every page I set the same 3 custom variables. When a visitor logs in I set a visitor custom variable to identify them as a member. So now I have 3 page level variables and one visitor level variable. The slot that contains the visitor level variable will always remain full, so I need to make sure that I always place my page level variables in the same slots.
Example 3: A visitor logs into a news site. I set 5 page level custom variables when they log in to record various pieces of information about their membership. One the next page I fire of 5 different custom variables to record information about their past browsing history. In this senario I have actually set 10 custom variables. How? Because they are all page level custom variables. The slots associated with a page level custom var open up when the visitor moves to a new page.
Hopefully this explains how you can technically have more than 5 custom variables.
Here’s the standard Async code snippet:
The index is a number, from 1 through 5.
The scope is also a number. A value of 1 indicates a visitor scoped custom variable, a value of 2 indicates a visit level custom variable, and a value of 3 indicates a page level custom variable.
The name and the value are just plain text. Whatever you enter into the code above will be pulled into Google Analytics.
Where do you put this code? That’s the hard part. You place this code when you want to create a custom variable. You may want to categorize all of the pages on your site using a page level custom variable. In this case you might place the custom var code in the standard GA code snippet, like this:
Or, if you’re going to identify someone as a ‘member’ of your site, you might place the code on a page after the visitor logs in. Like this:
But you can also attach the code to visitor action, like
You can also create custom variables for mobile devices using the iOS SDK and the Android SDK. How cool is that!
And some of the super nerds I work with at Cardinal Path have been able to hack custom variables and set them using the Google Analytics Mobile Tracking code for low-end mobile devices.
Regardless of the platform that you’re tracking, you can probably use a custom variable.
Super Nerd Stuff
Custom variables are not without their limitations and oddities. Here are a few things you need to be aware of when implementing:
1. What happens if you place a custom variable in a slot that is already filled? The last value placed in the slot is applied to the visit.
2. Custom variables are sent to Google using the same invisible image request. In the old days (ie two months ago) the size of this request was limited to 2048 characters by the browser. Because this limitation can really restrict data collection, Google decided to limit the size of your custom variable. This means that the name – value combination must be less than 64 characters. I know, this sucks.
BUT, Google has since changed how it collects data. The size of the image request is now 8000 + characters. So, in theory, Google could kill the 64 character CV limit. But they have not…
3. Custom Variable processing lags the standard data processing by about 24 hours. However, if you add a custom variable to a custom report it will be visible. I know, completely weird. This is really only for very, very big sites, where data processing can lag in general.
4. Page level custom variables are more-or-less the same as an event. When you create an Advanced Segment using a page-level custom variable, GA will return the number of visits that included that custom var. So, if you don’t have enough slots available you may want to consider using an event.
5. When you set a custom variable the data is sent immediately back to Google Analytics via an image request. This means that if you set a custom variable, on the last page of a visit and AFTER the pageview is recorded, that custom variable will NOT be collected by Google Analytics. If you can, put your custom variable code BEFORE
That’s ALL I have to say about custom variables. Hopefully this gives you a good baseline understanding of how they work and how to implement them.
If you’re using custom variables leave a comment. I’d love to hear how you’re using them.