wp_nav_menu google analytics event tracking

I’ve got a WP3 custom menu, with a custom link in the menu, How can I add google analytics event tracking to that custom link.

Thanks
Alex

1 Answer
1

Is it an outbound link? Otherwise it should automatically get tracked (as a page view). If it is outbound, I’d set a class on the custom menu item in WordPress (using the CSS Classes (optional) field). Then you could trigger that link with JavaScript. For example, with jQuery you could do something like this (assuming you are using the Async version of Google Analytics):

$('.custom-classname').delegate('a', 'click', function(e) {
   e.preventDefault();
   _gat._getTrackerByName()._trackEvent('outbound', 'custom link');
   var lnk = $(this).attr('href');
   setTimeout('document.location = "' + lnk + '"', 100);
});

The setTimeout is there to make sure that Google Analytics has time to read the event before the browser sends the user to the outbound link.

I hope that helps.

Link:
http://www.google.com/support/analytics/bin/answer.py?answer=55527

Leave a Comment