Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2007
    Thanked 0 Times in 0 Posts

    Question How do I track PDFs in google analytics?

    Please help, I am trying to track PDF downloads

    see my code below:
    <!doctype html>
    		<meta charset="utf-8">
    		<meta name="Keywords" content="">
    		<meta name="Description" content="">
    		<title>my website</title>
    		<link rel="stylesheet" type="text/css" href="/css/reset.css" media="all">
    		<link rel="stylesheet" type="text/css" href="/css/global.css" media="all">
    		<script type="text/javascript" src="/js/mootools.js"></script>
    		<script type="text/javascript" src="/js/popup.js"></script>
    		<script type="text/javascript" src="/js/clients.js"></script>
    		<script type="text/javascript">
    			$("#main nav ul li:last-child").each(function(){
    		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    		<script type="text/javascript">
    		<script type="text/javascript">
    			var _gaq = _gaq || [];
    			_gaq.push(['_setAccount', 'UA-11840529-1']);
    			(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);
    		<a href="document.pdf">Click here to download PDF</a>
    		<script type="text/javascript">
     		   $("a[href$='pdf']").each(function(index) {
       		   pdfLabel = $(this).attr('href');
       		   pdfOnClick = "_gaq.push(['_trackEvent', 'PDF', 'Download', '" + pdfLabel + "']);";
        		  $(this).attr("onClick", pdfOnClick);
    This is not working?!

    I got the code from here: Tracking PDF downloads in Analytics
    Last edited by jarv; 10-11-2012 at 10:29 AM.

  2. #2
    New to the CF scene
    Join Date
    Oct 2012
    Thanked 4 Times in 4 Posts
    I see that you are originally loading MooTools and then loading jQuery. Is there a reason that you need both?

    I would guess that since $ is loaded first then it is referencing MooTools and not jQuery, but I could be mistaken. I have never needed both libraries on the same page.

    For a Mootools solution try something like:

    <script type="text/javascript">
    $$("a[href$='pdf']").each(function(pdfLink) {
        pdfLink.addEvent('click', function(e) { 
            _gaq.push(['_trackEvent', 'PDF', 'Download', pdfLink.href]);                           
    This is mootools syntax to select the 'a' tags linking to a pdf file. Loops through the results. Adds a click event for each link. That click event will add the new event to the google analytics array.

    Good Luck


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts