How to Use Custom Variables in Google Analytics

Contributing Editor

It’s easy to track campaigns, ecommerce transactions and JavaScript events in Google Analytics. Custom variables offer a further level of control which permits you to segment all visitor data, e.g.

  • monitoring pages viewed by members and non-members
  • discovering which products are bought by new and existing customers
  • categorizing content by topic.

A single custom variable is set using the following code:


_gaq.push(["_setCustomVar", index, name, value, scope]);

where:

index (required)
You can set up to five custom variables on any page so the index is an integer between 1 and 5.

I recommend keeping things simple — define five or fewer custom variables per website and assign a consistent index. It’s possible to create more if they are spread over multiple pages, but it can lead to confusion.

name (required)
The custom variable name.

value (required)
The custom variable value. It’s possible to set numeric values, but data is passed and treated as a string.

scope (optional)
An integer between 1 and 3 where:

  • 1 is visitor level — the custom variable data persists for every visit and page viewed by an individual.
  • 2 is session level — the custom variable data persists during the single visit made by an individual.
  • 3 is pageview level — the custom variable data only persists during the current pageview.

For example, the following visitor-level variable could be set after the user registers and logs in for the first time. It would only need to be set once because it would remain associated with the user (even when they log out of our system):


_gaq.push(["_setCustomVar", 1, "Member", "yes", 1]);

Perhaps we now want to segment members by the number of months they’ve been registered (up to a maximum of 12 months). We could set a session-level variable whenever they log in:


_gaq.push(["_setCustomVar", 2, "RegisteredFor", Math.max(months,12)+" months", 2]);

If we want to track which topics are of interest to users, we could set a pageview-level variable:


_gaq.push(["_setCustomVar", 3, "Topic", "JavaScript", 3]);

Custom variables are not sent immediately and should normally be set prior to calling _trackPageView(). They are also sent when a custom event occurs with _trackEvent(), but that’s not a situation you can rely on. Our full code could therefore be:


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);

// set custom variables
_gaq.push(["_setCustomVar", 1, "Member", "yes", 1]);
_gaq.push(["_setCustomVar", 2, "RegisteredFor", Math.max(months,12)+" months", 2]);
_gaq.push(["_setCustomVar", 3, "Topic", "JavaScript", 3]);

// track page view
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

Custom Variable Reporting

The easiest way to view custom variables in Google Analytics is the Visitors > Custom Variables report.

However, Advanced Segments in My Customizations offers a greater level of analysis. Click Create new custom segment then expand the Visitors section of the Dimensions box. Define a Segment by dragging a custom variable name or value to the dimension box. The following screenshot defines a new segment named “Logged In” which looks for data where the “Member” variable is set to “yes”":

Google Analytics Advanced Segments

Once the segment has been saved, you can open any report and click the “All Visits” tab at the top-right. You can restrict it to show members only by ticking the “Logged In” checkbox:

Google Analytics Advanced Segments

Have you found any interesting uses for custom variables in Google Analytics?

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Joshua Rapp

    This is funny because I have been trying to setup custom variables in Google Analytics over the past couple of days, then I end up getting frustrated and moving on.

    Hopefully I can get some metrics up and running. I’ve been trying to track banner clicks and conversions. Fun times.

    • http://www.optimalworks.net/ Craig Buckler

      I know exactly what you mean. It took me a while to understand the basic concept. I’ve rarely seen them used elsewhere, do I guess other developers give up too.

      For me, the most confusing aspect initially was the index. It’s used to limit the number of custom variables, but why can you then define a variable name? In addition, you won’t see the scope on any reports. Perhaps that’s something they’re working on…

  • Chris Luongo

    Very interesting. I’ve always thought Analytics provides more than enough information but I could use this for some deeper statistics. Such as like you mentioned, comparing how members act against non-members.

    Or to see who purchased a product does after purchasing a product. Do they look for a new model of what they bought? Do they look for addons for it?

    Awesome article.

  • Henry

    Seems simple enough. The only thing I don’t understand and can’t seem to find anywhere is, if, for example, I want to track logged in user behavior versus non-logged-in, do I have to put the code on the page that comes up after logging in or do I replicate the code across the whole of the site?

    If it’s the former – wouldn’t it just be easier to filter the results by visits that have gone to the specific after-log-in page?

  • http://secretstowebsuccess.com Dali Burgado

    Hi Craig,

    This was a great summary of how to use Custom variables, set them up, and how to segment to get a better understanding of your web users. Thank you!

    Dali

  • Peter van Klinken

    Graig,

    Great detailed explanation]. I’ve been using them for a while now and works perfect. No i have a new situation in witch i can’t seem to get it working correctly.

    I’ve been monotoring customers at my site (A) and setting visitor level vars. Works perfectly. The problem is that i need them to work on another site (B) as well. So these vars should show up on site B when the user has been on site A before. There is no direct linking between the 2 sites and the interval can be a couple of days. Any thoughts on how i could set this up?

    Thank you in davance,

    Peter