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
	};

Comment