Reputation:
How do i properly use custom vars in google analytics? I used the code below and here is what i notice
What am i doing wrong?
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-1']);
if (cond) {
_gaq.push(['_setCustomVar',1, 'one', d1,1]);
_gaq.push(['_setCustomVar',2, 'name two', "sz",1]);
_gaq.push(['_setCustomVar',3, 'name3', boolVal,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);
})();
Upvotes: 2
Views: 235
Reputation: 7270
Code looks fine, although you might want to make sure your variables exist and convert to strings.
if (cond) {
if (typeof d1 !== "undefined") {
_gaq.push(['_setCustomVar', 1, 'one', d1.toString(), 1]);
}
_gaq.push(['_setCustomVar', 2, 'name two', "sz", 1]);
if (typeof boolVal !== "undefined") {
_gaq.push(['_setCustomVar', 3, 'name3', boolVal.toString(), 1]);
}
}
Also, Custom Variables can lag behind _trackPageview's showing up in the GA UI. (Source: Mastering Google Analytics Custom Variables)
Upvotes: 1