user34537
user34537

Reputation:

How do i properly use CustomVars in google analytics?

How do i properly use custom vars in google analytics? I used the code below and here is what i notice

  1. i only seen 'one' being in the get request
  2. After 24 hours only 'one' showed up
  3. After commenting out one and changing the 2 vars to use slotIndex-1 i notice NO vars going through GA

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

Answers (1)

TomFuertes
TomFuertes

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

Related Questions