Custom Variables, Part II: The Code
In Part I of this series on custom variables, we looked at why you might be interested in using them. Now, let’s take a look at how you actually implement them with code on your site.
Set a custom variable
The code for custom variables is really easy, it’s just a single function. (If you ever used the old user-defined segment function
_setVar, the implementation is pretty similar, except there are a few extra parameters.) Here’s what it looks like:
pageTracker._setCustomVar(index, name, value, scope)
Here’s what the four parameters mean:
indexis a “slot” that the variable gets put in, because you can have multiple custom variables. It can be a number from
5. How to use a slot becomes a little complicated, so we’re going to examine it in more depth later.
valuework together to identify the custom variable. You might have “eyes” and “blue”, or “member” and “yes”, or “section” and “yes”. The name identifies what the variable is about, and the value is the label that applies. There’s a 64-character limit on the combined length of the name and value.
scopesays whether the variable applies at the visitor, session, or pageview level. (See the discussion of scope in part I.) A scope of
2is session, and
3is pageview. The default is
3if you don’t supply a value.
So, here’s a complete example:
pageTracker._setCustomVar(1, "eyes", "blue", 1)
This says, “Set the custom variable in slot 1 to eyes=blue, and apply that at the visitor level.” Makes sense, right?
Where do I get the values?
One of the most common questions with
_setCustomVar is, how do I fill in those values? In some cases, the values are the same for everyone to whom they apply (like “member”=”yes”). But for eye color, for example, how do I dynamically separately the blue-eyed people from the brown-eyed people?
Well, first off, you have to already know the information in some way, of course. You had people fill out a survey that asked what color their eyes are, or they filled it out on the form when they registered for your site or made a purchase. Whenever it happened, you captured the value and stored it somewhere — maybe a cookie, maybe a database, wherever that is.
Then, you want to dynamically fill that into the custom variable code. The exact details of how to do this depend on where the value is coming from and how your pages are built (ASP, PHP, etc.). But the basic approach is like this:
pageTracker._setCustomVar(1, "eyes", <<dynamically fill in eye color>>, 1)
Where does the code go?
OK, so once we’ve figured out the code, where does it go?
Just two guidelines here. First, put it on the page where it makes sense to capture it. If it’s the “member” custom variable, put it on the page that members get after they log in. If it’s the “section” custom variable (to record that they viewed the “widgets” or “about us” or “find a store” sections of the website), put the code on the pages in each of those respective sections. If it’s eye color, probably the easiest place is the page where you’ve collected that information.
Second, it’s important that you put this code before a
_trackEvent) on the page, because the custom variable information gets sent along with the pageview.
We’re halfway there
So, this is all the information you need to record a custom variable. But we still haven’t talked about this “slot” thing. We’re going to leave that to Part III, because it gets a little complicated.
Want to learn more about custom variables? Attend a day of our GA Training in NYC, June 8-10. $499/day.