click events inserted in inline links (google analytics pdf tracking)

Does anyone use the Google Analytic? If so are you using their event tracking to capture PDF downloads?
OR
Does anyone have some suggestions how I can get this to work?

Basically to track PDF downloads with Google Analytic I need to add an onClick event to the URL. Most of my users however are using the RHythmyx inline link to link to their PDFs and I can’t see how to edit the bnFile template to include this onclick event, nor do I want to force all our users to use inline templates for their files.

<a href=“http://www.example.com/files/map.pdf” onClick="javascript: pageTracker._trackPageview(’/downloads/map’); ">

Shane

Inline links can be appended with the appropriate code but here is an example of what we do

I have a PDF content type associated with a Snippet called osSnFileSizeLink:

#set( $type = $sys.item.primaryNodeType.name )##
#if( $tools.math.toInteger("#isBinaryType($type)") )## 
	#set( $file_size = "#getFileSize()" )##
#end##
#if( $file_size != "0" && $type ==  "rx:pPdf" )##
<a href="#getUrlValue()" #getTarget() #trackDownload() title="#displayfield('linktext')"> $!{file_size}</a>
#elseif( $file_size != "0" && $type !=  "rx:pPdf" )
<a href="#getUrlValue()" #trackDownload() title="#displayfield('linktext')"> $!{file_size}</a>
#else##
—
#end

The real workhorse is the macro #trackDownload()


#macro( trackDownload )#set($dq = '"')#set( $onclick = "onClick=${dq}pageTracker._trackPageview(this.href);${dq}" )$onclick#end

Although this isn’t an inline link, its template based and the code that really matters is

onClick="pageTracker._trackPageview(this.href);"

You may need to add a new field to use a custom url in

pageTracker._trackPageview(#field($custom_url));

but I assume you get the idea.

Let me know if you need some more help.

~rmark

We use Google Analytics to track ‘events’ such as a pdf download, or a click on an external link. We don’t use the trackPageview as we didn’t want it to affect our real pageviews. We use javascript to go find all of the links on page load and then once someone clicks on one we run an event to track them. We went with the javascript option so that it just worked for our end users and it required no modification on our end. It works pretty well, we haven’t had any real issues with it yet.

Thanks Ryan. Unfortunately a snippet template is what I’m trying to avoid as there are cases where we do need to use the inline link as opposed to template but thanks for the suggestion.

Shane

Thanks for the suggestion Alex. This javascript option, is it something you guys wrote yourself or was it something you found online and just had to modify it to work? Would you be willing to share the code?

Shane

Whichever solution you go with (ie templates or javascript) I would check the sys_context and only add the tracker when the item is being published…

It is simple enough and prevents “fake” page hits…

http://forum.percussion.com/showthread.php?t=740&highlight=sys_context

We use the MooTools framework. So it is relatively easy to find all of the links. I used a combination of some code I had found somewhere, and then the suggestions in the Google Analytics FAQs.


window.addEvent('load', function() {
	if(pageTracker) {
		$('container').getElements('a[href^=http]').each(function(el) {						  
			el.addEvent('click',function(e) {
			var dd = el.get('href').replace('http://','');
			pageTracker._trackEvent('Outbound Links', dd);
			}.bind(this));
		 });
	}
});


This code should only fire if it finds pageTracker on the page so you should have to worry about clicks during preview, as I assume you don’t have your Google Analytics on your pages in preview anyway. If you are not familiar with JQuery or Mootools then this code might look a little foreign.