开发者

Google Analytics; Track outbound clicks; How?

开发者 https://www.devze.com 2023-01-22 14:46 出处:网络
I read this article about tracking outbound links on banners and such: http://seogadget.co.uk/how-to-count-your-outbound-click-stats-with-onclick-in-google-analytics/

I read this article about tracking outbound links on banners and such:

http://seogadget.co.uk/how-to-count-your-outbound-click-stats-with-onclick-in-google-analytics/

So开发者_StackOverflow社区 I added this code to the onClick event of my href:

 javascript: pageTracker._trackPageview('/outbound/top_banners/banner_name');

Is this enough?

Because I have read some places I need a "link delay" function or something in the HEAD of my document as well, before any javascript is executed!

Also, where exactly in GA (in the interface) will I be able to view the clicks?

Thanks


Here's the problem: every item of data recorded & reported by Google Analytics is sent to the GA Servers in the Request URL component of a Request by the client for the __utm.gif. The function _.trackPageview() provokes the collection/concatenation of the data into a Request URL, as well as the Request itself. In other words, if _trackPageview() isn't called, no data is sent to the GA Servers.

So the question is whether the GA Request (above) is processed before the Request associated with the outbound link. If it is not, then the click on the outbound link is not recorded by GA, which is not what you want.

So what you want to is delay, just slightly--long enough for the GA Request to occur but short enough so that the user doesn't notice the delay--the outbound link Request.

There are a few differences between the code posted in your Q, and the code below--all diffs are directed to this 'race condition'.

First, notice that the return value from the onClick handler is set to "false"--preventing the client browser from (immediately) navigating to http://www.outbound-link.com

The second diff is the call to setTimeout. The third argument passed in, '100' is the number of milliseconds delay.

Third, the onclick handler (fnx) creates its own tracking object, and therefore doesn't rely on a pageTracker object to have been initialized elsewhere.

Your second question is, where in the GA Browser do you view these clicks? Using GA, you can track events in two distinct ways--using _trackEvent() or _trackPageView().

By tracking outbound links the second way (as you did and therefore as i did below) the 'click' shows up not as an event, but as a pageview ('virtual pageview' is the term most often used by the GA Consultants, et al. to indicate something tracked as a page view but which is actually not). So you will see these clicks reported along with other page views--i.e., in Content (along with Traffic and Visitors, the three main headings in the left-hand panel). How can you tell which lines in that report refer to the these outbound clicks? The value of the Page field (usually the left-most column heading) will be the url of the outbound link. Once you know this, of course, you can create an Advanced Segment or Custom Report or even a new Profile, to report these separately.

<script type="text/javascript">
    function fnx(that) {
        try {
            var pageTracker=_gat._getTracker("UA-YOURACCOUNTHERE-PROFILE");
            pageTracker._trackPageview("http://www.outbound_link.com");
            setTimeout('document.location = "' + that.href + '"', 100)
     }catch(err){}
    }
</script>


<a href="www.outbound_link.com" onclick='fnx(this);return false;'>"Take Me Here"</a>


This is my 100%-working solution for tracking all outbound links (I love jQuery, so you need to add jquery.js script to page).

function isLinkExternal(link) {
    var r = new RegExp('^https?://(?:www.)?' + location.host.replace(/^www./, ''));
        return !r.test(link);
}

$(document).ready(function() {
    $(document).bind('click', function(e) {
        var target = (window.event) ? e.srcElement : e.target;
        while (target) {
            if (target.href) break;
            target = target.parentNode;
        }
        if (!target || !isLinkExternal(target.href)) return true;
        var link = target.href;
        link = '/outgoing/' + link.replace(/:\/\//, '/');
        _gaq.push(['_trackPageview', link]);
    });
});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-YOURCODE-1']);
_gaq.push(['_trackPageview']);
(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

This script give all outbound links to GA like: /outgoing/http/www.example.com. For better experience you need to create advanced segment for this links. Create it with parameters: "Page" "Starts with" "/outgoing/http". That's all! Now you have full statistics for your outbound links.


This solution will allow you to click links with target='_blank' and additionally, will allow you to set data attributes on any link and have those automatically send up to GA.

$('a').bind('click', function( event ) {
  var target = $(this).attr('href');
  if ($(this).attr("data-event-category") !== undefined && $(this).attr("data-event-label") !== undefined) {
    gtag('event', 'click', {
      'event_category': $(this).attr("data-event-category"),
      'event_label': $(this).attr("data-event-label"),
      'event_callback': function(){handle_redirect($(this), event);}
    });
  } else {
    handle_redirect($(this), event);
  }
});

function handle_redirect(element, event) {
  var target = element.attr('href');
  var regExp = new RegExp("//" + location.host + "($|/)");
  var isLocal = (target.substring(0,4) === "http") ? regExp.test(target) : true;
  var url = target;
  if(isLocal===true) {
    return;
  } else {
    event.preventDefault();
    gtag('event', 'click', {
      'event_category': 'outbound',
      'event_label': window.location.href + ' --> ' + url,
      'transport_type': 'beacon',
      'event_callback': function(){element.unbind( "click"); element[0].click();}
    });
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消