Upcoming LunaMetrics Seminars
Washington DC, Sep 22-26 Boston, Oct 6-10 Chicago, Oct 20-24 Seattle, Nov 3-7

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:

  • index is a “slot” that the variable gets put in, because you can have multiple custom variables. It can be a number from 1 to 5. How to use a slot becomes a little complicated, so we’re going to examine it in more depth later.
  • name and value work 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.
  • scope says whether the variable applies at the visitor, session, or pageview level. (See the discussion of scope in part I.) A scope of 1 is visitor, 2 is session, and 3 is pageview. The default is 3 if 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 _setVar and _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 _trackPageview (or _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.

Jonathan Weber

About Jonathan Weber

Jonathan Weber is the Data Evangelist at LunaMetrics. He spreads the principles of analytics through our training seminars all over the East coast. The next seminar he'll be leading will be a Google Analytics training in Boston. Before he caught the analytics bug, he worked in information architecture. He holds a Master’s degree from the University of Pittsburgh School of Information Sciences. Jonathan’s breadth of knowledge – from statistics to analysis to library science – is somewhat overwhelming.

http://www.lunametrics.com/blog/2010/04/23/custom-variables-part-ii-code/

4 Responses to “Custom Variables, Part II: The Code”

Ryan C. says:

This has been really helpful putting custom variables into layman’s terms. I’m having trouble wrapping my head around slots, so I’m really looking forward to the next post!

Nate Sidmore says:

This series is the best write-up I’ve found on this subject.

Quick correction here is that the default level in scope is set to the pageview level (i.e. level 3)

Nate

Jonathan Weber Jonathan says:

Nate — thanks for the catch. This seems to be a recent change, but the documentation now clearly says pageview is the default. I’m changing the post to reflect that.

Jim Williams says:

We have custom variables set up and am trying to validate. Using fiddler the utme value looks like this: 8(2!Name1*Name2*Name3)9(2!Value1*Value2*Value3)11(2!1*1*1)

My question is where does the slot indexes get passed. How does GA know which slot to parse this data into.