简体   繁体   中英

Google Analytics: Is _gaq.push() a blocking function?

What I am trying to do is to redirect the user to the next page right after sending an event to Google Analytics:

_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';

The above code manages to register events because I can see them in the GA report. However I suspect that some events were lost because the browser redirects the user to the new page before the track pixel loads.

My questions are:

  1. Does _gaq.push() block until the event has successfully reached Google's server?
  2. If not, what is the best way to make achieve what I need?

Thanks!

Google has a support page that says you might want to add a delay after calling _gac.push , however they claim that this is to give the Google Analytics JavaScript a chance to load . They don't say that it's to ensure the request is sent.

First, delay the outbound click by a fraction of a second.

This delay will hardly be noticeable by the user, but it will provide the browser more time load the tracking code. Without this method, it's possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded.

I haven't checked @pixelfreak's claim of handling onbeforeunload , but it seems that's what they do.

This is my observation based on some quick research. That function is part of Google Analytic's asynchronous API, so it should not block anything, otherwise, it's not async. :)

Digging through the obfuscated ga.js, you can kinda see that the tracking is probably called onbeforeunload, which fires when you leave a page.

It really doesn't matter if the tracking pixel loads completely, it's a fire & forget. As long as the request is initiated and reaches Google's server, it'll get tracked. The response (in this case, the pixel image) is a by-product.

Maybe you can try this method, but I have not tried

_gaq.push(['_trackEvent', 'xxx' ...]);
_gaq.push(function(){location.reload()});

Just use 1 sec delay. Like this:

_gaq.push(['_trackEvent', 'xxx' ...]);
setTimeout(function(){window.location = 'some_url'}, 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM