Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics custom variable not getting set

On my website I have four custom variables. My issue is that Google Analytics for some reason is only registering three of them. The script on the page that is not working properly looks like this:

<script type="text/javascript">
    var _gaq = _gaq || [];

    _gaq.push(['_setCustomVar',3,'Category 3','some value']);
    _gaq.push(['_setCustomVar',4,'Category 4','some value']);

    _gaq.push(['_setAccount', 'UA-XXXXXXXX']);
    _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); 
    })();
</script>

This page is supposed to track two of the custom variables in index 3 and 4. Another page is tracking custom variables in index 1 and 2.

In Google Analytics I can see that it has registered the categories in the first three slots (Index 1-3) but the category in the fourth slot never gets registered. According to the documentation you can have up to five slots.

Can anyone shed any light on why the fourth variable never gets registered?

Update

Inspecting the utme variable in the analytics request provided some interesting results.

Page 1, which is working, uses the following tracking script:

_gaq.push(['_setCustomVar',1,'Category 1','value1']);
_gaq.push(['_setCustomVar',2,'Category 2','value2']);

This results in the following utme parameter:

8(Category 1*Category 2)9(value1*value2)

Page 2, which is NOT working, uses the following tracking script:

_gaq.push(['_setCustomVar',3,'Category 3','value3']);
_gaq.push(['_setCustomVar',4,'Category 4','value4']);

This results in the following utme parameter:

8(3!Category 3)9(3!value3)

It's clearly ignoring the last custom value that I'm trying to track!

like image 292
Christian Hagelid Avatar asked Dec 15 '11 00:12

Christian Hagelid


2 Answers

I had trouble getting this to work too and ultimately it was a case of not reading the document carefully enough.

Not sure if you're posing the actual code you're using for setting your variables, if not, make sure you're in compliance with this warning:

The total combined length of any custom variable name and value may not exceed 128 bytes. Keep in mind that this is not equivalent to 128 characters. Because names and values are URI encoded when stored, some characters use more than one byte. For example, = is stored as %3D rather than = and uses 3 bytes rather than 1. To get a list of URI encoded values, search the web for URL encoding reference.

Have you checked the other recommended practices? These two might be a source of problems for you (if you're using session variables too):

Do not use duplicate key names across slots. You have up to 5 simultaneous custom variables for use in a single request (e.g. pageview or event call). The sum of all your custom varaiables cannot exceed 5 in any given request (i.e. you cannot have 5 visitor and 5 session custom variables set simultaneously).

like image 122
voutmaster Avatar answered Nov 10 '22 00:11

voutmaster


I have also had trouble with getting custom variables to work, and we've become aware of the (numerous) gotchas which you should be aware of.

Common Gotchas

(The official documentation does state most of these, but it's very easy to go wrong, and difficult to understand why.)

  • Use a valid slot number. By default this is an integer between 1 and 5, but Premium Analytics users will have more slots available.
  • The value for the custom variable must be a string. It does say this in the documentation—but don't assume that an integer (e.g. 1) will get converted to a string (i.e. "1"). You need to convert it with the toString() method (or by doing value + ""). What will happen if you don't (this happened to us, you see) is that the name of the custom variable will get set, but no value!— In your reports you'll see the variable, but when you click on it, you'll get the ominous message: "There is no data for this view."
  • The name for the custom variable must not evaluate to false. Therefore you cannot use the integer 0.
  • The value for the custom variable must not evaluate to false. The values 0, false, null, undefined, and the empty string "" all evaluate to false, so they won't get used in your Analytics reports. (Anyway, you shouldn't be setting any values which aren't strings.)
  • The combined length of the custom variable name and value must not exceed 128 characters. Other people have warned that you've got to take into account the URI decoding (i.e. treat the character '=' as 3 characters, because it gets encoded to '%3D'—however we looked at the code ourselves, and it doesn't seem to be the case. (At least, not as of today.)
  • Do not use duplicate key names across slots.
  • Call the _setCustomVar() function when it can be set prior to a pageview or event GIF request. 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.
  • Watch out for collisions between page- and session-level variables. If you need to track large numbers of custom variables, consider using a slot matrix to prevent collisions happening.

Debugging

If you set a custom variable where the value is false, or the combined length goes over the 128-character limit, the _setCustomVar() function will return false. You can output the return value to your browser to see what's going on:

setTimeout(function() {
    var tracker = _gaq._getAsyncTracker();
    console.log(tracker._setCustomVar(slot, name, value, scope));
    // should normally print 'true'
}, 2000);

(The setTimeout() function is needed, assuming you're using the asynchronous snippet, because otherwise the _gaq variable won't be set. This isn't pucker code, obviously, but it's good enough for debugging.)

Looking at the __utm.gif request

Finally, if you still have problems, open up the developer tools, and look at the requests the browser is making. You should see a request for http://www.google-analytics.com/__utm.gif?....

Then look in the full request for the utme parameter. It should look like this:

&utme=8(var1*var2*var3)9(val1*val2*val3)11(scope1*scope2*scope3)

If you see an exclamation mark anywhere (preceded by an integer), that means that no value was set for that custom variable. (The integer is just an incremented integer, it doesn't mean anything special.)

(You might notice that some values are escaped with apostrophes. The escaping method is as follows: '0 means '; '1 means ); '2 means *; '3 means !.) (Of course the entire request string will also be URI-encoded.)

like image 36
andre Avatar answered Nov 09 '22 23:11

andre