Google Analytics does not track link click events.

I am currently using analytics.js (a newer version of GA), and I'm trying to track all types of events from my site, including when a user clicks on an anchor tag that points to an external URL. I am currently using this setting:

 (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-KEY-HERE', { 'alwaysSendReferrer': true, 'allowAnchor': true });

and I send events when I hit the link:

$(document).on("click", ".anchor-class", function (event) {
    label = //get data from anchor element here ...  
    ga('send', 'event', 'Link Clicked', 'Click Details', label);;
    return;
    }
});

and it does not send anything to GA (although the event handler calls the ga (send ...) method ). However, if I use this exact technique, but with event.preventDefault (); at the beginning of the function, the event is dispatched and appears on the GA toolbar.

Are there any settings that I skipped to do this correctly?

+4
2

hitCallback:

  $(document).on('click','a', function(event){
    event.preventDefault();
    var label = $(this).attr('href');

    ga('send', 'event', 'Link Clicked', 'Click Details', label, {
      'hitCallback': function(){
        window.location.href = label;
      }
    });
  });
+2

Blexy, - . , , Google Analytics , Ghostery, . , :

http://veithen.imtqy.com/2015/01/24/outbound-link-tracking.html

+1

Source: https://habr.com/ru/post/1523298/


All Articles