Google Event Tracking

Keeping track of events on your website is very useful. Google Analytics comes with a useful feature called Event Tracking.

http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html

For example, if you want to track all clicks to on a link you can use

[cc lang="html"]Play[/cc]

The specification for the GA _trackEvent() function is

[cc lang="javascript"]_trackEvent(category, action, opt_label, opt_value)[/cc]
  • category (required)The name you supply for the group of objects you want to track.
  • action (required)A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
  • label (optional)An optional string to provide additional dimensions to the event data.
  • value (optional)An integer that you can use to provide numerical data about the user event.

So, if you want to group events and distinguish certain actions, you can do something like this
[cc lang=”javascript”] _gaq.push([‘_trackEvent’, ‘Videos’, ‘Play’, ‘Gone With the Wind’]);
_gaq.push([‘_trackEvent’, ‘Videos’, ‘Pause’, ‘Gone With the Wind’]);
_gaq.push([‘_trackEvent’, ‘Videos’, ‘Stop’, ‘Gone With the Wind’]);
[/cc] If you want to track successful form submissions for a particular form, you can add code like this on the confirmation page
[cc lang=”javascript”] jQuery(document).ready(function ($) {
_gaq.push([‘_trackEvent’, ‘Contact’, ‘Form submit’, ‘Sales Inquiry Form’]);
});[/cc] If you want to track the total number of PDF downloads and clicks on an email link, you can add code like this
[cc lang=”javascript”]

jQuery(document).ready(function ($) {
/* track views of PDF files in our /docs folder */
$(‘a[href^=/docs][href$=pdf]’).bind(‘click’, function() {
/* track as events in google analytics */
_gaq.push([‘_trackEvent’, ‘PDFs’, ‘View’, $(this).attr(‘href’)]);
/* optional way to track as virtual pageview */
/* _gaq.push([‘_trackPageview’, this.href]); */
});
/* track mailto links */
$(‘a[href^=mailto]’).bind(‘click’, function() {
/* track as events in google analytics */
_gaq.push([‘_trackEvent’, ‘Contact’, ‘Mail to’, $(this).attr(‘href’).replace(‘mailto:’, ”)]);
});
});
[/cc] From the code above, you can see that you can also track using virtual page views.