Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 09:23 AM   PM User | #1
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
Question How do I track PDFs in google analytics?

Please help, I am trying to track PDF downloads

see my code below:
Code:
<!doctype html>
<html>
	<head>
		<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(){
			      $(this).addClass("last");
			});
		</script>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
		<script type="text/javascript">
  			jQuery.noConflict();
		</script>
		<script type="text/javascript">
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-11840529-1']);
			_gaq.push(['_trackPageview']);
			(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>
	</head>
	<body>
		<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);
   		 });
		</script>
	</body>
</html>
This is not working?!


I got the code from here: Tracking PDF downloads in Analytics

Last edited by jarv; 10-11-2012 at 09:29 AM..
jarv is offline   Reply With Quote
Old 10-11-2012, 09:57 PM   PM User | #2
slickuser
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 0
Thanked 4 Times in 4 Posts
slickuser is an unknown quantity at this point
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:

Code:
<script type="text/javascript">
$$("a[href$='pdf']").each(function(pdfLink) {
    pdfLink.addEvent('click', function(e) { 
        _gaq.push(['_trackEvent', 'PDF', 'Download', pdfLink.href]);                           
    });
});
</script>
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
slickuser is offline   Reply With Quote
Reply

Bookmarks

Tags
analytics, google, pdf, tracking

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:01 AM.


Advertisement
Log in to turn off these ads.