Thursday, April 26, 2012

Google analytics and variable types

I recently went on a mission to change up how we use google analytics events and I wound up having some problems.  I use chrome as my default browser and there is a great extension called google analytics debugger (by google) that helps immensely when debugging analytics code.  I used it the first time around to set everything up.


function gAnalyticsPush(action, name, id, non_interact)
{
  non_interact = typeof non_interaction !== 'undefined' ? non_interact : false;
  push_id = parseInt(id);
  label = id; // this is a string
  _gaq.push(['_trackEvent', name, action, label, push_id, non_interact]);
}

This is my current code.  I tried setting it up so push_id (integer) would be the name (category in the api docs) and the google debugger said everything was working but, alas, when i uploaded it and found I was no longer getting events I realized it was not.

After a while I ditched sending Id's as my category and started using the name (as for me, the two are essentially the same) so I had this:
function gAnalyticsPush(action, name, id, non_interact)
{
  non_interact = typeof non_interaction !== 'undefined' ? non_interact : false;
  push_id = parseInt(id);
  _gaq.push(['_trackEvent', name, action, push_id, push_id, non_interact]);
}

But that wasn't working either.  The problem wound up being that I couldn't send an integer type in the label parameter.  I changed the above code from push_id to just id and it STILL  wasn't working.  I then guaranteed that all id parameters being sent to my gAnalyticsPush function had quotes around them and SHAZZAM, everything worked.

I'm sure that I could solve this in the function but right now there is no need for me to do it.

So I call the above function like so (inside a script tag):

gAnalyticsPush('something_viewed','<%= @model.name %>','<%= @model.id %>',true);

Note that those quotes around the model id are quite important and google analytics debugger did not report any errors even when things were not working.

No comments:

Post a Comment