Search code examples
javascriptgoogle-analyticsanalyticsdom-events

Adding event tracking to site for PDF/Microsoft Word downloads


I am having some difficulty setting up event tracking. I have a website where people can download PDFs and Word docs of various content. I inserted the event tracking like so:

<a href=files/8399039122.pdf onClick='_gaq.push(['_trackEvent', 'downloads', 
'all', 'nofilter']);' >File #1</a>

<a href=files/8329384939.doc onClick='_gaq.push(['_trackEvent', 'downloads',
 'all', 'nofilter']);' >File #2</a>

However, after four days data is still not showing up on my analytics profile. Did I install this wrong? Also, do I need to add the _gaq.push(['_trackEvent', 'downloads', 'all', nofilter]);' to the analytics script in the header of my page?


Solution

  • Looks like it's your use of single quotes (not nested properly). Try this:

    <a href="files/8399039122.pdf" onClick="_gaq.push(['_trackEvent', 'downloads', 'all', 'nofilter']);" >File #1</a>
    
    <a href="files/8329384939.doc" onClick="_gaq.push(['_trackEvent', 'downloads', 'all', 'nofilter']);" >File #2</a>
    

    Wrap the entire onClick in double quotes. And, the path to your links (href) should be quoted as well.

    To delay the onclick without using target="blank"

    <a href="pdfs/my-file.pdf" onclick="var that=this;_gaq.push(['_trackEvent,'Download','PDF',this.href]);setTimeout(function(){location.href=that.href;},200);return false;">Download my file</a>