Posts Tagged ‘google’

Google Analytics tracking PDF and Flash files on your Site

Tuesday, September 29th, 2009


Setting up Google Analytics is pretty easy work thanks to google :)

If you dont have a Analytics account yet  you can get one here: Google Analytics

To start tracking just add the code below to the page(mostly before the end of <body> tag) that you wish to track using Google Analytics


&lt;script type="text/javascript"&gt;

var gaJsHost = (("https:" == document.location.protocol) ? "<a href="https://ssl.&quot;">https://ssl."</a> : "<a href="http://www.&quot;);">http://www.");</a>
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
try {
var pageTracker = _gat._getTracker("XX-XXXXXX-XX");
pageTracker._trackPageview();
} catch(err) {}&lt;/script&gt;

Note: Make sure that you replace the ‘X” with your tracking id.

A bit tricky part is tracking downloads for PDF and Flash files.

But since google rocks they have made this simple as well.

To track a file download add the following code to the anchor tag’s onclick event


pageTracker._trackPageview('/downloads/myfile.pdf');

The complete anchor tag would look like something below:


&lt;a href="/myfile.pdf" onclick="pageTracker._trackPageview('/downloads/myfile.pdf')"&gt;My Pdf&lt;/a&gt;

When the user clicks on the above link , you will see the ‘/downloads/myfile.pdf’ url in your analytics report.

This technique can be used to track other file types as well (Flash, WMV, AVI, MOV, etc..)

Post to Twitter