Upcoming LunaMetrics Seminars
Seattle, Nov 3-7 New York City, Nov 17-21 Washington DC, Dec 1-5 Los Angeles - Anaheim, Dec 8-12

Google Analytics Custom Variables Not Working?

Custom variables... or not?

Everyone can agree: Google Analytics’ custom variables are a great feature. It lets you label your visitors with all sorts of fun stuff. Male or female? Member or non-member? Cat person or dog person? If you can identify your visitors’ answers to these sorts of questions, you can tag them with a custom variable.

But sometimes website owners implement the custom variable code and start gathering these valuable segments of traffic, completely unaware that the numbers aren’t very accurate. What could be causing such a heinous mistake?

Believe it or not, Google warns against the issue right in those instructions there, but they don’t call it out quite enough for my taste. It’s extremely important that you “call the _setCustomVar() function when it can be set prior to a pageview or event GIF request” (emphasis mine). Here are the details:

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.

What exactly does this mean? Well, if a visitor fills out a survey on your site, hits submit, and you run _setCustomVar() after the _trackPageview() of the submission success page, that _setCustomVar() call just sits there in deep space, waiting for another _trackPageview() or _trackEvent() function to carry it along to Google’s servers. If that is the visitor’s final pageview in the session, then the call floats away forever, never to be heard from again.

The worst part, though, is if the visitor goes to another page, then the _setCustomVar() will tag along with any subsequent GIF requests. This means that it may appear as if the custom variable is working just fine, according to the reports in Google Analytics, but the numbers are just inaccurate.

So, to ensure that you are tracking your custom variables as precisely as possible, make sure that you always run the _setCustomVar() function before the calls to _trackPageview() or _trackEvent(). If this isn’t possible–you have an include at the top of every page, for instance, and can’t modify it–then be sure to include a second pageview or event after the _setCustomVar() function, like this:

_gaq.push(['_setCustomVar', 1, 'membership', 'gold', 1]);
_gaq.push(['_trackEvent', 'Tow Truck', 'go', '-', 0, true]);

This will ensure that every single time that function runs, it gets counted. Guaranteed!

Update: Tyson Kirksey, of Vertical Nerve, reminded me to set the event to non-interaction mode so that it won’t influence bounce rate, pages/visit, time on page, etc. I’ve also borrowed his clever naming convention for the event. Thanks, Tyson!

http://www.lunametrics.com/blog/2011/12/30/google-analytics-custom-variables-working/

9 Responses to “Google Analytics Custom Variables Not Working?”

Jeanette says:

Thanks Michael for this outstanding post!

What about first touch tracking? Currently:
1. Check if __utma is there. If yes, do nothing. If not:
2. Execution of asynchronus GATC
3. Read __utmz and write content to __utmv

Given this logic, _trackPageview() has to be executed before _setCustomVar().

When I get your post right, I should add another _trackPageview() at the end of this code to enhance my tracking.

What about the impact on other statistics? Would be good, if you could extend your remarkable article and I’m pretty keen that it’s not only me being interested in that.

Good post Michael, and great reminder. At Vertical Nerve, we call these events “tow trucks”.

_gaq.push(['_setCustomVar', 1, 'membership', 'gold', 1]);
_gaq.push(['_trackEvent', 'Tow Truck', 'go']);

In many cases, it probably makes more sense for the Tow Truck event to be a non-interaction event, so the code would look like this:

_gaq.push(['_trackEvent', 'Tow Truck', 'go', '-', 0, true]);

This way, metrics like pages/visit, time on page, time on site, bounce rate, etc will not be affected.

@Tyson: Great point! I keep forgetting about non-interaction in events. Thanks for the reminder!

Thomas says:

This post was very very helpful. Thanks.

Zoffix Znet says:

Thanks so much! My page custom variables were not working at all, and it turns out I was setting them after the track view.

venky says:

Hi Michael,
I want to use custom variables for storing page hits.
I am using the below code. I am creating all 5 variables.
I call Trackpage method from each page passing page name and value.
Most of the time I don’t see custom variables in the account.Even if it gets created(very rarely) it does not increase the count for the page and no other pages are added to that variable.
Can you let me know whats wrong in the below code?

function Trackpage(variableName, variableValue) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-XX']);
_gaq.push(['_setCustomVar', index, variableName, variableValue, 3]);

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

Nathan says:

This was driving me crazy, thanks for helping me figure out why it wasn’t working.

Keith H says:

Useful – cheers. Was scratching my head for a while there….

Wulf Sanoja says:

I’m finding that my visit counts are way off when using custom variables, which I need. My custom reports visit counts are not matching the other analytics package which we are running in parallel for testing. I have the code alltogether and setting the variable and calling the trackPageView. Pageviews are very close, not so much the visits.