contact us

Feel free to reach out to us using the form to the right, or contact us us directly.

 

info@ewbanaltyics.com | 5o8-650-0271

 

PO Box 433
Natick, MA 01760
USA

508.650.0271

EWB Analytics Blog

Mystery PDF Tracking

Elizabeth Brady

Tracking clicks to pdf files requires custom code. Without the code, you should not see any views to pdf files in web analytics reports, but this is not always the case.  One additional simple code customization explains the mystery.

Since it is not possible to include the basic javascript page tag on a pdf file similar to other html pages on a website, pdf tracking requires embedding an onclick (or onmousedown) event in the link to the pdf, to pass either a virtual pageview or an event to track the click to the pdf file.  These can be added manually, with a fully customizable hierarchy of event lables, as described for Google Analytics such as:

<a onclick="_gaq.push(['_trackEvent', 'Document', 'PDF', 
'/directory/file.pdf']);"href="/directory/file.pdf">
Click here to view PDF</a>

For sites with a large number of pdf files, it’s worth the investment to set up custom code in a jquery file to automatically track all clicks to pdf’s (as well as other documents, as specified).  Many varieties of the code exist – such as this solution provided by Adam Buchanon.

While walking one client through the various requirements and options for setting up tracking, and in an attempt to demonstrate the lack of any pdf tracking at all on their site, I performed a filter on ‘pdf’ in the standard content reports.  The results returned – much to my surprise -- a long list of pdf documents.  I went to the site to view a random selection of pdf documents.  I could see that there were no tags firing to track the click to the pdf.  How was it possible to see page views in the reports to pdf’s without any code on the site, and without data passing to Google Analytics? 

Finally, after some brainstorming with a very talented colleague, we noticed that all of the visits to the pdf documents were from search and referrals (suspicious).  We finally decided to go to the site to view the specific pdf files listed in the content report. We discovered that all of the pdf addresses listed produced 404 errors.  Without code adjustments to the 404 error template, web analytics reports record pageviews to the URL requested, instead of tracking the 404 error.  Sometimes the error status may show up in the page title, but sometimes it does not (the page title may be blank).

 For Google Analytics, modify the trackPageview line of code to record the 404 error along with the page requested:

 _gaq.push(['_trackPageview','/error 404/'+document.location.pathname]);

 Therefore, the Google Analytics Tracking code on a 404 error page should be:

<script>

                var _gaq = _gaq || [];

                _gaq.push(['_setAccount', 'UA-XXXXXX-X']);

                 _gaq.push(['_trackPageview','/error 404/'+document.location.pathname]);

                (function() {

                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

                })();

                </script>

In addition to explaining ‘mystery’ pdf tracking, error template customization produces extremely actionable reports for managing broken links, revealing where 301 redirects need to be set up, and improving customer experience.