Joe Riggs
Joe Riggs

Reputation: 1324

Google Analytics Event Not Working - file download click event added dynamically

I have added a jquery snippet that attaches an event to all hrefs that open as pdf. I can see that it is firing but the event is never tracked in analytics

$( document ).ready(function() {
//attach event dispatcher to all links that are pdf files
//register event in analtyics

   $('a[href*=".pdf"]').click(function(e) {
       ga('send', 'event', 'PDF', 'download', 'digital content', $(this).attr('href'));
       console.log($(this).attr('href'));
    //console log is working each time link clicked
   });
});

Analytics code is added in head like such

  <script type="text/javascript">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXX-YY');
    ga('send', 'pageview');
</script>

PDF docs linked:

<a href="http://website.com/50025H966.pdf" target="_blank" class="body_link_11">
http://website.com/50025H966.pdf<span class="icon_11 ui-icon-extlink"></span></a>

Tag manager not an option as I dont control that part of the site, I can only add js

Upvotes: 0

Views: 712

Answers (2)

ak85
ak85

Reputation: 4264

As far as I am aware you should only use 5 parameters (including send).

eg

ga('send', eventCategory, eventAction, eventLabel, eventValue);

based of your example I used this on my site and it works in the real time events with no problems

ga('send', 'event', 'PDF', 'digital content', $(this).attr('href'));

Upvotes: 0

nyuen
nyuen

Reputation: 8907

One thing I notice is that you are sending in a non-integer value for the event value (the 6th parameter in your ga function). It should be an integer

ga('send', 'event', 'PDF', 'download', 'digital content', 1);

(cf. https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation)

Upvotes: 2

Related Questions