Quick Tip: The Power of Google Analytics Custom Variables

Quick Tip: The Power of Google Analytics Custom Variables

Tutorial Details
  • Program: Google Analytics and a Text Editor
  • Difficulty: Easy
  • Estimated Completion Time: 45 Minutes

Familiarity with your users and their needs is the most important aspect of any successful site or web campaign. Google Analytics is the perfect tool to gather this sort of information. But there’s much more you can find out about your users when you begin using custom variables to make better decisions.


What are Custom Variables?

Using custom variables carries huge potential, as it allows you to acquire information about the behavior of your visitors.

You can treat custom variables as your own extension of metrics and dimensions. These provide you with the ability to gather non-standard and detailed data, which are not available via the Google Analytics panel.

Using custom variables carries huge potential, as it allows you to acquire information about the behavior of your visitors, which, in total, can significantly contribute to an increase of the ROI (return on investment) of the website or e-shop.

For example, using custom variables makes it possible to differentiate the activities of the logged in users versus those who never signed in. This provides an opportunity to observe the behavior of the website designed for a particular target group. For instance, we can learn which page of our website is the one that males – aged between 20 and 30 – visit most frequently. And this is just a tiny piece of the information that can be stored with the help of custom variables.


So How do Custom Variables Work?

The functionality of custom variables is wonderful in its simplicity. In fact, it’s based on the simple fact that, while performing a pre-defined activity, the user is labeled and the information about this label is subsequently stored in a cookie. Then, on the basis of this particular label, we can create a new section in the statistics of the Google Analytics panel.

Custom variables can be used in one of three ways:

  • page-level – this is the lowest level used to monitor particular ways in which the visitor interacts with the web page (e.g. AJAX, watching a video etc.)
  • session-level – a label attached to this level is added to the actions of the visitor throughout the session, and is deleted when the session cookie expires.
  • visitor-level – this is the highest level; the label is permanent and remains detached during subsequent visits until the visitor deletes the cookie or overwrites the value.

How Do I Configure Custom Variables?

Custom variables are quite easy to configure; you only need to add one line of code before the _trackPageview call.

_gaq.push(['._setCustomVar,INDEX, NAME, VALUE, OPT_SCOPE']);
  • INDEX (required) – determines a slot for a custom variable in Google Analytics. There are 5 slots available, numbered 1 to 5. You should remember that, if the variables are to function correctly, a single variable must be placed in a single slot.
  • NAME (required) – is the name of the variable that is going to appear in the report of the Google Analytics panel – Custom Variables
  • VALUE (optional) – is the actual value of the variable which is paired with NAME. Any number of such value pairs is possible, for instance if NAME=country, VALUE can, one by one, equal the values of US, GB, PL etc.
  • OPT_SCOPE (optional) – is a level within which a custom variable functions. As I described above, there are three levels: 1 (visitor-level), 2 (session-level), 3 (page-level). When this parameter is not provided, it is replaced with the default page-level.
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount','UA-xxxxxxxx-x']);
        _gaq.push(['._setCustomVar,INDEX, NAME, VALUE, OPT_SCOPE']);
       _gaq.push(['_trackPageview']);

Some Practice

Now, let’s review how custom variables function in practice. Let’s suppose that we want to keep track of the visitors of our website’s – distinguishing between those who did and did not log in. In order to do so, before the _trackPageview call, we insert code describing the user.

_gaq.push(['._setCustomVar, 
			1, 			    // first slot
			'user-type',    // custom variable name
			'visitor', 	    // custom variable value
			2			    // custom variable scope - session-level
			]);


Once the visitor logs into your website, we change this code, accordingly:

_gaq.push(['._setCustomVar, 
			1, 			    // first slot
			'user-type',    // custom variable name
			'regular-user', // custom variable value
			2			    // custom variable scope - session-level
			]);


But What Follows?

It’s time to present the results of the described script. After the script had been executing for a week, an advanced segment in Google Analytics panel was created. Its aim is to divide the data in the panel into: total, report for logged-in users, and report for users who didn’t log in for particular metrics.

The segment itself is created through Advanced Segments => Create a new advanced segment. Then you should set the dimensions according to the screenshot below:

The variable which we defined using JavaScript was in the first slot, so we have to select Key 1 and Value 1. Then, we set the key which we are interested in (user-type), and the value for the defined key (visitor) together using concatenation. Next, we name and test the advanced segment. The number of visits during a particular period of time for particular segments will be calculated within the test.

We define the second segment which takes into account the logged-in users in the same way. We can create it by analogy with the pattern presented above – with the difference in that the custom variable is set as regular-user.

After establishing the two segments, we can activate them. The result is presented below. Such a set of data is a great basis for an in-depth analysis of the activities on a webpage.


It’s Worth Remembering…

  • Don’t duplicate the names of custom variables between the slots.
  • Set custom variables before the pageview call.
  • You cannot use more than five custom variables in a single request.
  • Consider event tracking instead of custom variables for some demands, as then no additional (false) pageviews are generated in the panel.
  • You can determine whether custom variables work by observing requests within Firebug, or with the help of the Chrome extension, Google Analytics Tracking Code Debugger.

This is Only the Beginning

Using custom variables translates into more justified and accurate site decisions.

The example presented in this article only illustrate the process of using a single custom variable, and determining the best way to manage a website, according to the type of visitor. Of course, this is only the beginning. Custom variables can become incredibly powerful when we combine several of them at the same time. As a quick example, with the applicable data from a website’s registration process, we can then track, not only the sex of the visitor (session-level), but also his or her age bracket (session-level). Further, we could divide the visitors into groups who have made purchases in our fictional eShop, or even track those who took a specific action, such as clicking on a Facebook button.

These techniques translate into more justified and accurate site decisions.

Tags: tips
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Abdulhafeez
  • http://carloshermoso.com Carlos Hermoso

    This is a really interesting article about using custom variables with Analytics. Definitely I have to come back here to check this in deep.

    Thanks for sharing.

  • http://www.samuellavoie.com Samuel Lavoie

    Really indepth article for anyone that have not yet jump on the custom variable :)

  • http://itcutives.com Jatin

    I have read about custom variables with Google Analytics, but this article unleashed the power of custom variables to people like me, taught us how we can configure, use and take business decisions using it.

    Thank You.

  • http://myviews.pl krzysu

    Congratulations for your first post on nettuts+. Greetings from Poland :) Keep going!

  • http://www.modernooze.co sam – dorset web design

    Very interestng thank you very much

  • http://www.paidsearchgeeks.com Matt Kelly

    Nice work, this is among the best resources I’ve found for explaining custom variables in very simple terms.

  • jo dean

    Wow this makes a lot ofsense dude.

    http://www.complete-privacy.au.tc

  • http://Andy-Russell.com Andy

    Great into in to the exciting world of more advanced Google Analytics. :-)

  • http://www.wholesalejeweler.ca Jeweler

    Great article!

    I have to read more about advanced Google Analytics. For the moment, I just do the basics. This article will help me.

  • http://www.annospot.com FM94

    Thanks a lot for this, very interesting !
    (Wondering if you guys have heard about the new Facebook send button which gives you the ability to directly share with groups!)

  • Steve B

    Quick question about the custom variables. Lets say I set one up for users logged in and users who are not logged in. Do I need to keep the custom variable code in my GA javascript block for every page loaded? Or can I set it once on the first page load and then it will follow the user automatically through subsequent page views?

    Thanks for the help.

    • Nathan

      Would also love an answer to this.

      We have a paid members login and the rest are visitors. It would be awesome to see the % diff between members and non members viewing all pages.

  • http://www.wpexplorer.com AJ Clarke

    First time hearing about this. Really awesome stuff. I will definitely give these custom variables a shot.

  • http://www.pureperformancemotors.com BMW Repair

    Thanks, this is a great article, most regular users have no idea of the potential of Google Analytics!

  • http://webfuel.org Justin

    I really appreciate this article but there are a few errors.

    For example, from the article:

    _gaq.push(['._setCustomVar,
    1,
    'user-type',
    'visitor',
    2
    ]);

    Should be:

    _gaq.push(['_setCustomVar',
    1,
    'user-type',
    'visitor',
    2
    ]);

  • Pallavi

    This a pretty detailed article, Thank you. I am pretty much trying to achieve something similar but trying to track event data for visitors vs. members.

    @Steve B – Did you get a resolution to your issue about identifying logged-in vs anonymous visitors? I am trying to achieve the same functionality but my reports are all messed up by public events appearing under private and vice versa.

    I have the basic GA tracking and custom variable code set in the master page and the master page itself dynamically identifies whether the user is logged in or not and set’s this custom variable to Public or Private. Also, for achieving this kind of functionality, I am using session level custom variable – Is that the right approach?

  • http://www.gargoyleconsulting.com/seo.html SEO Consultants

    Great article – custom variables and advanced segments are hugely powerful, and can offer some interesting information about demographics/user-engagement of paid traffic as well.

  • Alessio

    Could I use custom variables with trackevent method?

  • Daniel Wheeler

    Hey, great post. This is something that I am def. going to use

  • http://www.spaaza.com Sam Critchley

    I also found this article and the comments really useful, thanks everyone. I also ran into problems getting Google Analytics to recognise my custom variables though. It turns out that the setCustomVar _gaq.push commands you have in your analytics snippet need to be placed BEFORE your trackPageview command or else they won’t be picked up. You can debug this with the Google Analytics Debugger extension in Chrome (and possibly in other browsers).

  • lyman

    I set the custom var with visitor level and try to merge the none logged in users and logged in users in the visitor flow, but end up with a lots of drops, is this due to the none logged in pages and the logged in pages in different frames and different domain? even I set the ‘_setDomainName’ as parent domain for both none logged in and logged in, and all the code was set before ‘_trackPageview’.

  • http://www.product-shot.co.uk/ Product Photography

    Thanks for defining the custom variables of Google Analytics.

  • Jeniffer Maben

    Very nice explanation of custom variable. Customized Google Analytic code can be implemented with Live2Support’s live chat application. We can track how many visitors click on offline chat window and online chat window.