Google Analytics Outbound Link Tracking with jQuery

Homepage > Diary > Coding > Google Analytics Outbound Link Tracking with jQuery

Tue 12/30/2008

in Coding, JavaScript

A usefull feature of Google Analytics is the ability to track outbound links with a simple call to a JS function, which tracks a click on a link to a specifed URL path. You can read more about this subject here.

The matter is that the code presented by the help page requires an onClick attribute, which is actually deprecated in XHTML.

To overcome this problem here is a simple jQuery plugin which converts any rev (or other given) attribute to a Google Analytics friendly tracking path (for both old and new versions of the app). Here is a sample code:

<a href="http://www.externalwebsite.com" rev="outbounds www.externalwebsite.com">visit this site</a>

Is tracked as /outbounds/www.externalwebsite.com

To apply this plugin call it inside a document loaded function, for example:

$("a").colt();

This calls the plugin on all A tags with an attribute rev.
Moreover you can use a custom element attribute by just passing it to the function:

$("a").colt("title");

And finally here is the source code:
jQuery.colt.js (908 bytes)

Share this:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • blogmarks
  • DZone
  • LinkedIn
  • MySpace
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Ping.fm
  • Yahoo! Buzz
No Related Posts

Tags: , ,

One Response to this post:

Leave a Reply ()

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

* required.