Track PDFs in Google Analytics

October 31, 2012

With just 3 lines of code

After a quick Google search I couldn’t find a really clean way to track PDF clicks. So I wrote my own solution instead.

1
2
3
$("a[href$='pdf']").live('click', function() {
  _gaq.push(['_trackEvent', 'PDF', 'Download', $(this).attr('href')])
})

This solution depends on jQuery. Since I had to implement this on a somwhat old jQuery version I had to use live instead of on. Live is deprecated as of version 1.7 of jQuery so if you’re using a newer version use on.