Track all outbound links in Google Analytics

前端 未结 7 1828
小蘑菇
小蘑菇 2021-01-31 20:06

I\'ve been using a script to track outbound links for a couple of months now. The script WORKS, but in the report generated by Google Analytics many UR

7条回答
  •  天涯浪人
    2021-01-31 21:01

    The problem with window.open is that the referrer will be lost. A better solution is to use onmousedown instead of onclick. Having done some tests, I know this work better that using window.open or using setTimeout. You got some false positive from people clicking the right mouse button and not choosing "Open in new tab" or "Open in new window", but onclick doesn't always fire for middle and right click on all browser. It's a choice between the lesser of two evils here.

    jQuery(function($){
      $('a:not([href*="' + document.domain + '"])').mousedown(function(event){
        // Just in case, be safe and don't do anything
        if (typeof _gat == 'undefined') {
          return;
        }
    
        var link = $(this);
        var href = link.attr('href');
        var noProtocol = href.replace(/http[s]?:\/\//, '');
    
        // Track the event
        _gat._getTrackerByName()._trackEvent('Outbound Links', noProtocol);
       });
    });
    

提交回复
热议问题