Custom Variable Not Showing Up in Google Analytics

Attempting to track authenticated users within the site, I added a custom variable to my Google Analytics tracking. It went something like this:

_gaq.push(['_setCustomVar', 1, 'User_Status', 'Authenticated', 2]);

I placed this right after my Google Analytics tracking code in the head section of the page. Like so:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_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 for logged in users 
_gaq.push(['_setCustomVar', 1, 'User_Status', 'Authenticated', 2]);

Seemed like the logical place based on other Google Analytics code I have added in the past.

This function calls the _setCustomVar method, sets it to slot 1, creates a category of ‘User_Status’, sets the value to ‘Authenticated’, and sets the scope of the custom variable to session level.

And then I waited and waited. After two days I saw absolutely no custom variables in my Google Analytics reports that corresponded to the one I had set.

Going back to the documentation Google provides on setting custom variables, I found this little nugget at the bottom of the page:

Call the _setCustomVar() function when it can be set prior to a pageview or event GIF request.
In certain cases this might not be possible, and you will need to set another _trackPageview() request after setting a custom variable. This is typically only necessary in those situations where the user triggers a session- or visit-level custom var, where it is not possible to bundle that method with a pageview, event, or ecommerce tracking call.

I moved the custom variable code, and, like magic, it all worked out.

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

//custom variable for logged in users 
_gaq.push(['_setCustomVar', 1, 'User_Status', 'Authenticated', 2]);

_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);
  })();

It pays to read ALL the documentation. It would have been nice, though, if Google had put that important piece of information in a more prominent location.

Posted in Google Analytics. Tags: . Permalink. Both comments and trackbacks are closed.

3 Comments

  1. yves
    January 18, 2011 at 5:51 am | Permalink

    Thanks for the post.

    Small tip: I used Firefox+Firebug to analyze the parameters attached to the GET .gif pseudo-image, and could determine this way that the custom variables were not sent in.

  2. November 1, 2010 at 6:30 am | Permalink

    Hi Jen, I need to learn more about Google Analytics and tracking my stats. So much to learn and so little time! There is some good info here.

  3. Mike
    October 25, 2010 at 6:07 pm | Permalink

    Jen, your sooo talented! I'm actually learning ColdFusion right now and I just have to say wow at what you have done! It's just that good!

    I am trying to use your code for Analytics to implement site data, keyword data, and sitemaps data from Google's webmaster tools as I learn ColdFusion (and Google API lol).

    Do you have any tips or tutorials to implementing webmaster tools stuff? I know I'd very much appreciate it and I'm sure others would as well!