Google Analytics Custom Variables Not Working?

December 30, 2011

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!