Adding Google event tracking with jQuery 'click' function

I recently set up AB tests on some Drupal sites using the Multivariate Module. This works for setting up the different AB test pages (varients), and tracking their conversions through to a certain page.

A couple of the tests weren't typical AB tests though, and instead of tracking a singular conversion goal each varient linked to it's own conversion page. For example one banner varient linked to a 'Buy Now' page and the other varient to a 'Free Demo' page. Not ideal for AB testing purposes, but that's a different topic.

The AB test module tracks the amount of visits to each varient, but in order to track the banner click conversions I used Google Analytics Event Tracking. This requires add something like the below to your links:

onClick="_gaq.push(['_trackEvent', 'AB Testing', 'Homepage - banner conversion test', 'Banner A']);"

In this instance I found the simplest way to apply the event tracking code to the link was using this jQuery click function:

$('div.yourLinkClass a').bind('click', function() {
	_gaq.push(['_trackEvent', 'AB Testing', 'Homepage - banner conversion test', 'Banner A']);
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.


Submitted by Tom on

This was useful...

Submitted by Anthony on

Thanks for the help.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.