Using Google Analytics to Track PDF Views

I currently build and manage a pro-bono site for Greenwood Christian Academy where they have a lot of PDF documents.  The users click on documents for enrollment, calendars, educational forms, health documents, etc.  Just recently they also announced an event that offers sponsorships.  The question that has come up lately is how to use Google Analytics to track how many people view the pdf documents and how many people click on the sponsor links.  This may sound simple but then I realized that Google does not track counts for links that go away from the site or to pdf views.  Not accepting this as failure, I started looking around and came across a “cool” feature that Google offers called ‘_trackEvent’.  You can use this for a multitude of things but I believe this is what I need.  The key is to realize that you need to attach something to the <a href> before the actual redirect.  Since I always use the pdf and external links with a target=’_blank’ I decided to attach an event handler to the <a href>.  The problem is this could take a while, especially if you have a lot of links and pdf.  Thank goodness for jquery.  I attached the code below as close to the end of the page as possible (preferably right before the </body>).  This script attaches a click event to every <a href> if there is a target=’_blank’.  I then determine if it is a pdf or just a “plain” link.

[sourcecode language=”javascript”]
<script type=”text/javascript”>
//Returns all elements that have a target=’_blank’
$(“a[target=’_blank’]”).each(function (index) {
linkLabel = $(this).attr(‘href’);
//if the word does not contain .pdf is in the link then
if ($(this).attr(“href”).indexOf(“.pdf”) == -1) {
trackOnClick = “_gaq.push([‘_trackEvent’, ‘Outgoing Link’, ‘View’, ‘” + linkLabel + “‘]);”;
//otherwise it must be a PDF
trackOnClick = “_gaq.push([‘_trackEvent’, ‘PDF’, ‘Download’, ‘” + linkLabel + “‘]);”;
$(this).attr(“onClick”, trackOnClick);

Understanding the _trackEvent on Google Developer web site can help you better understand the additional attributes.  Once you have this in place then when you log into Google Analytics you can click on the left panel under Events and then Top Events. (figure 1)

Figure 1
Looking for quality web hosting? Look no further than Arvixe Web Hosting!
Tags: , , , , , , , , | Posted under ASP .NET 2, ASP .NET 3.5, ASP .NET 4.0, DotNet/Windows Hosting, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *