jQuery Google Analytics Super Tracker

Published On: 2011-11-26

Category: Jargon Jabberwocky

I'm not really sure where I found this, and it's undergone a few changes since then but here is what I use on most sites to automatically track outgoing links, mailto links and downloads. It requires jQuery and Google Analytics to already be installed on your site. This piece of JavaScript should go below your analytics code.
if(jQuery) {
jQuery(document).ready(function() {
  jQuery('a').click(function() {
    var $a = $(this);
    var href = ($a.attr('href')) ? $a.attr('href') : '';

    if ( (href.match(/^http/i)) && (! href.match(document.domain)) ) {
      var category = 'outgoing';
      var event = 'click';
      _gaq.push(['_trackEvent', category, event, href]);
     } else {
      if ( href.match(/.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/i) ) {
        var category = 'download';
        var event = 'click';
        _gaq.push(['_trackEvent', category, event, href]);
      } else {
        if ( href.match(/^mailto:/i) ) {
            var category = 'mailto';
          var event = 'click';
          _gaq.push(['_trackEvent', category, event, href]);   
        }
      }
    }       
  });   
});
}
Bonus: I've built a simple wordpress plugin out of this code.
<?php

/*
Plugin Name: Add Analytics
Plugin URI: http://fallsgeek.com
Description: A simple plugin to add analytics code so we can easily update the theme.
Author: Anthony Taylor
Version: 0.1
Author URI: http://fallsgeek.com
*/

function load_analytics() {
  if (!is_admin()) {
    //nowdoc would be perfect for this.
    $analytics_out = '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
  var _gaq = _gaq || [];
  _gaq.push([\'_setAccount\', \'UA-#############\']);
  _gaq.push(["_trackPageview"]);
if(jQuery) {
jQuery(document).ready(function() {
  jQuery(\'a\').click(function() {
    var $a = jQuery(this);
    var href = ($a.attr(\'href\')) ? $a.attr(\'href\') : \'\';
    if ( (href.match(/^http/i)) && (! href.match(document.domain)) ) {
      var category = \'outgoing\';
      var event = \'click\';
      _gaq.push([\'_trackEvent\', category, event, href]);
     } else {
      if ( href.match(/.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/i) ) {
        var category = \'download\';
        var event = \'click\';
        _gaq.push([\'_trackEvent\', category, event, href]);
      } else {
        if ( href.match(/^mailto:/i) ) {
            var category = \'mailto\';
          var event = \'click\';
          _gaq.push([\'_trackEvent\', category, event, href]);   
        }
      }
    }       
  });   
});
}

  (function() {
    var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
if (\'http:\' == document.location.protocol) {
  ga.src = \'http://www.google-analytics.com/ga.js\';
} else {
  ga.src = \'https://ssl.google-analytics.com/ga.js\';
}
    var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
  })();
/* ]]> */
</script>';

  echo $analytics_out;
  }
}

add_action('wp_head', 'load_analytics');

?>
Here's a pure Javascript version that seems to do the same thing.