Using Google Analytics to track pdf files

Many webmasters use Google Analytics, but if you are like me, you would like to know how many people download certain pdf files. This answer is applicable to the latest version of Analytics.

Your normal code will look like this,

< script type="text/javascript" > (spaces added)
 var _gaq = _gaq []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + ''; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); < /script >

This should always be but just above the < /head > (spaces added)

On the page with the link to the pdf put this in the main body text,

< a  href="yourlink/20-297.pdf" onclick="javascript: _gaq.push(['_trackPageview','yourlink/20-297.pdf'])"target="_blank">Download as a pdf file< /a > (spaces added)

When you access your Google account you will start to see the page info after 24 hours.