Tracking File Downloads Automatically in Google Analytics with Prototype

October 3rd, 2007

I wrote this Javascript after seeing this Google Analytics tip: http://www.google.com/support/googleanalytics/bin/answer.py?answer=55529&topic=11006.

Having onclicks on the anchors in the markup is messy and tedious to maintain. My code dynamically does this. It’s pretty simple. As long as this code is executed after the ‘contentloaded’ event, it will work. Also, the Javascript is dependent on Prototype.

$w(‘mp3 pdf doc txt rtf’).each(function(ext){

  $$(‘a[href$=.’ + ext + ‘]’).each(function(a){

    var pageview = ‘/downloads/’ + a.href.substr(a.href.lastIndexOf(‘/’), a.href.length); /* /downloads/wtf.mp3 */

    a.observe(‘click’, function(){ urchinTracker(pageview); });

  });

});

Spumco Comic Books

September 9th, 2007

“We don’t want no moon cheese babies!”

February 8th, 2007

Yo La Tengo

February 2nd, 2007

ABMVC: Entry 5

January 31st, 2007