Skip to content

Instantly share code, notes, and snippets.

@NathanQ
Created March 4, 2014 21:36
Show Gist options
  • Save NathanQ/9356297 to your computer and use it in GitHub Desktop.
Save NathanQ/9356297 to your computer and use it in GitHub Desktop.
Javascript to track file downloads on Google analytics. This example takes a pdf link click and pushes the event to analytics with it's url. The setTimeout is there to give it a bit of time for the event tracking to occur before beginning the file download.
$(document).ready(function() {
$("a[href$='pdf']").click(function(event){
var href = $(this).attr('href');
_gaq.push(['_trackEvent', 'Downloads', 'File Downloaded', href]);
setTimeout(function() { window.location.href = href; }, 500 );
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment