Snippet: capturing outbound links with Prototype

In fact, this can be don with plain JavaScript, but Prototype helpers are quite helpful

<script type="text/javascript">
//<![CDATA[
  function logclick() {
    new Ajax.Request('/stats/outlink/' + this.href);
    return false;
  }

  window.onload = function () {
    var linktags = document.getElementsByTagName('a');
    var links = $A(linktags);

    links.each(function(node){
      if (node.addEventListener){
        node.addEventListener('click', logclick, false);
      } else if (node.attachEvent){
        node.attachEvent('onclick', logclick);
      }
    });
  }
//]]>
</script>

This simple JS code modifies each link in the page so when it’s clicked the browser sends an AJAX request to /stats/outlink/the_url, sou you can track the exit points of your website.

It’s not perfect yet. It doesn’t work if you right-click -> Open in new tab, and it has only been tested in Firefox, but if it’s of some help to anybody, I’ll be happy :)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s