Straight to content [Alt + SHIFT + 2] Straight to navigation [Alt + SHIFT + 1]

Javascript

February 16, 2010

Event Tracking Blues

You code for hours but it doesn't work. Then you read the documentation which turns out to be poor. The question you posted in the forum is left unanswered for many weeks. Situations like these can happen to any Web Developer like me and is very frustating. I had such one in the previous weeks regarding Google Analytics' Event Tracking.

Event tracking is very useful, you can add your own statistics, combine these with custom reports and analyse any data for your E-Business. When Google Analytics is properly set up on your website you can use this Javascript call anywhere:

_trackEvent(category, action, optional_label, optional_value)

For example if you want to append the page performance of the contact site for custom reports:

_trackEvent('Page performance', '/contact/', 'ms', 534)

Whereas in this example the optional value 534 is generated on the server side. In PHP you can measure that by calling microtime() at the beginning and end of script execution.

The mistakes I made were:

  • Surrounding this fourth optional parameter with semicolons like '534'
  • Trying with a float number like 1.23 (for seconds)

Google Analytics never threw an error. I tried to debug, to check in Firebug console and with many different parameters until a mate told me to use integer instead. That's true, the documentation says that this fourth parameter must be an integer.

"Hooray!" - Finally I knew why it didn't work and smiled. All I had to do is to remove the semicolons and to change that floating number to an integer by using milliseconds instead of seconds. If Google Analytics' script had thrown an error or just anything, it would have saved me from wasting many hours ...

Write your opinion

This question has been specially drafted to filter out spam and to prove you're human.

Keeping you informed when someone has answered.