Track user clicks on certain links

categories: analytics, howto, jquery, plugins

We recently added a new feature to dailystrength.org, and there was much debate about whether users would use it as we intended. To find out, I wrote a clicktrack ditty that sends some data to the server when a link is clicked. It was somewhat inspired by a post I wrote a while ago about tracking outbound links with Google Analytics and jQuery, but is actually even simpler. I've retooled my clicktrack script to work as a generic, 2.5k plugin, which you can download here.

Usage

To use this plugin, add a distinct classname to all links you would like to track. For example:

 
<a href="foo.html" class="clicktrack">this is a link</a>

You can also add additional classes to a link with a distinct prefix — the default prefix is "ct_". These classes will be passed to the server along with other clicktrack data:

 
<a href="foo.html" class="clicktrack ct_type_1">this is a link</a>

Then, include the script and a line of code to initialize it in the foot of your page. Note that you must at least pass a URL for the remote (server-side) script that will handle the clicktrack data ("foo.php" in the example below):

 
<script src="/includes/js/jquery.clicktrack.js">
type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() { $("a.clicktrack").clicktrack("foo.php"); });
</script>

This will pass the following data to a script called "foo.php" on your server:

Optional parameters

The only required parameter is the URL of the remote script. However, you can pass a more elaborate configuration object. The options and their defaults are as follows:

 
	var defaults = {
		remote_script: null,
		prefix: 'ct_',
		extraData: null,
		callback: function(){},
		dataType: null,
		sendOnce: true,
		preventDefault: false
	};

3 Responses to “Track user clicks on certain links”

  1. Prashanth says:
    August 31st, 2009 at 1:16 am

    Hello Rebecca,
    Thanks for this nice slim click tracking script. I beleive there will be no changes required to implement the same in Asp.net

  2. Bill Bartmann says:
    September 5th, 2009 at 4:00 pm

    Hey good stuff…keep up the good work! :)

  3. Jeremy says:
    November 18th, 2009 at 1:03 pm

    Is this working for you in Safari? I was having issues with one I rolled myself and thought I’d try this… both work fine in Firefox and IE but Safari never fires the call to the external script and instead just goes to the href location.

Comment