At my current company we needed a way to track links to downloads or external sites, so they would be logged in Google Analytics. I came up with this piece of code that will allow us to do so, the only prerequisite being at jQuery exists and is used on a particular website.

Here is the code:

<script type="text/javascript">
if (typeof(jQuery) != 'undefined') {
    jQuery(document).ready(function() {
        jQuery(".googleTrack").click(function() {
            var link_href = jQuery(this).attr("href");
			return true;

function trackGoogleClick(link_href)
    if(typeof(window.pageTracker) !== 'undefined') {
        pageTracker._trackPageview('tracked/-/' + link_href);
    } else if (typeof(window._gaq) !== 'undefined') {
		_gaq.push(['_trackPageview', link_href]);

To track a link (or any element for that matter) this method just give the anchor a class or “googleTrack”, and that’s it, it will be tracked.

« »