Snippet: capturing outbound links with Prototype

Posted by Jorge Bernal November 23, 2006

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 :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Furl
  • Google Bookmarks
  • email
  • StumbleUpon

Related posts

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply