When tracking outbound links with Google Analytics, why delay an outbound click and not push a function into the queue?

The official suggestion for tracking outbound links with the (asynchronous version) of Google Analytics is to insert a tracking event into the queue, for example:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']); setTimeout('document.location = "http://foo.bar"', 100); 

It would not be better to push an anonymous function to the GA queue , for example:

 gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']); gaq.push(function() { document.location = 'http://foo.bar/'; }); 

There is no guarantee in the setTimeout version that the event will be processed before being redirected, while in the second version it will be redirected only after the event is processed - right?

+6
source share
2 answers

The problem with your proposal is that it will not have time to execute the request until the page changes.

The browser will not wait for the completion of these two events before starting user navigation. If you are familiar with jQuery, it will be like adding a click event handler to a link, adding an ajax request to that handler, but not putting event.preventDefault() . In other words, the ajax request will not be processed as the user has already moved to the next page.

change , as you mentioned in the comments, it doesn't matter if you apply return false to the links.

If you can really click on a function like you illustrated in your example, I really donโ€™t understand why it will not work better than with the exception that the first request is timed for some reason, forcing the user to wait far beyond limits of 100 ms, as usual.

What about users blocked by Google? There are many add-ons / programs, etc., which can completely block Google Analytics, adsense, etc. Will these users have a normal user interface?

+1
source

The best way is to work with the hitCallback function supported by GA. hitCallback is a function that is called immediately after a successful deletion.

In your case, you can do something like this:

  // if after 300 ms, we still didn't get any action from hitCallback, // redirect manually setTimeout(function() { document.location = 'http://foo.bar/'; }, 300); _gaq.push(['_set', 'hitCallback', function() { document.location = 'http://foo.bar/'; }]); _gaq.push(['_trackEvent', 'outbound link ','click', 'http://foo.bar/']); 

I talked about this here: https://gist.github.com/jonasva/aa20811003e7077360fcb1b297f0311d

0
source

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


All Articles