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:

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:

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:

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.

Jonathan Weber is our Data Evangelist, focusing on bringing the strategic value of data analysis to our customers. He spreads the principles of analytics through our training seminars and even wrote a book on Google Analytics & Tag Manager. Before he caught the analytics bug, he worked in information architecture. Away from the computer, you can find him as a flower farmer and plant geek.

  • Ryan C.

    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!

  • 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)


  • Jonathan

    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

    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.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187