Does Google Analytics interfere with history.back () in Android browser?

I have an html5 mobile application where everything exists on one page, and I use history.back / popstate / etc to change the contents of the page (via jQuery Mobile). I use Google Analytics to track various events, and on one page I track if a user exits through a specific button:

$('#my-back-button').bind('tap', function() { _gaq.push(['_trackEvent', 'mycategory', 'myaction']); history.back(); return false; }); 

In the android 2.2 browser, history.back () is called, but onpopstate does not start and the page does not change. If I put window.onpopstate = function() { alert("!"); }; window.onpopstate = function() { alert("!"); }; Just before history.back (), I do not see a warning.

If I format two lines first (history.back () and then _gaq.push), this seems to work, but I cannot rely on this order in all my code.

No exceptions are thrown. It works great on iOS and desktop Chrome.

Any ideas why history.back () doesn't work after calling Google Analytics?

+7
source share
3 answers

Try wrapping the call to _ gaq.push() in asynchronous timeout 0:

 $('#my-back-button').bind('tap', function() { setTimeout(function() { _gaq.push(['_trackEvent', 'mycategory', 'myaction']); }, 0); history.back(); return false; }); 

This will allow history.back() to be executed first before Google Analytics potentially impacts the history stack.

+2
source

Hm, good disgusting: what happens if you move _gaq.push () to a new function and leave history.go () exactly where it is?

+1
source

Perhaps a call to google analytics adds a new entry to the history stack, and history.back () just brings you to the page you're currently on.

Or GA provides a new area of ​​history.

Try calling history.back () twice or use top.history.back () or window.history.back () or self.history.back ()

+1
source

All Articles