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
else
{
trackOnClick = “_gaq.push([‘_trackEvent’, ‘PDF’, ‘Download’, ‘” + linkLabel + “‘]);”;
}
$(this).attr(“onClick”, trackOnClick);
});
</script>
[/sourcecode]
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)