Quick Fix: Google Analytics event tracking not working? Check your types!

Google Analytics’s Event Tracking is a powerful way to gain a deeper understanding of what is happening on your website. With event tracking, you can record each click, keypress or mouse move; it becomes easy to find out which fields are being filled or skipped on a form, or which thumbnails are being hovered.

I came across one sneaky gotcha this morning, though. I wanted to track the number that was displayed in a field at the point the user clicked on an “Add” button:

_gaq.push([
  '_trackEvent', 
  'Page Updates', 
  'Plus button clicked', 
  parseInt($('#count').text())
]);

On reviewing the data in GA, however, none of the clicks were being captured.

It turns out that Google will not record the event if the fourth parameter is of type Number. The simple fix was to make it a String type:

_gaq.push([
  '_trackEvent', 
  'Page Updates', 
  'Plus button clicked', 
  parseInt($('#count').text()).toString()
]);

Normally this wouldn’t come up as JavaScript has a tendency to treat most variables as Strings unless you’re careful about how they are created. In this case, relying on parseInt() to find a number turned out to cause more problems than it solved.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>