This evening I was experimenting with adding custom variables to my Google Analytics tracking code, but when I checked Google’s custom variable documentation, it didn’t match the analytics code that I had.
Google Analytics changes their code for you to copy and paste quite often, seemingly every time I setup a new site to track.
The latest version of the analytics code that I was given used a new format, notably:
The docs say to put the
_setCustomVar code in before
pageTracker._trackPageview() … but I no longer have that older style setup.
After a bit of digging around, I came up with a solution from documentation on the _gaq Global Object. The
_setCustomVar code can be added as follows:
_gaq.push(['_setCustomVar', index, name, value, opt_scope]);
Just put this after the last push line up top and you’re good to go.
Hopefully Google Analytics will update their docs to reflect the newer code style using
_gaq, but in the mean time, this is working for me.