0

I am kinda new in javascript and I have a question about async calls. For example:

_gaq.push(['_xxyyz', 'xyz']);
window.location.href = "www.wwwwww.com";

Is this a valid code? Will the _gaq.push analytics code run properly and I won't interrupt the call with the redirect? Because our analytics shows invalid data, and this is the only suspicious area in the javascript.

Wheeler
  • 296
  • 1
  • 4
  • 12

1 Answers1

1

I found the solution in a similar question

So the trick is to wrap your redirect in the _gaq.push() function, since these functions are executed in sequence.

_gaq.push(['_xxyyz', 'xyz']);
_gaq.push(function(){
   window.location = 'MY REDIRECT URL';
});

More information in the official google Analytics documentation

Community
  • 1
  • 1
Akkumulator
  • 995
  • 1
  • 9
  • 26
  • FYI, if the `['_xxyyz', 'xyz']` command being pushed is something that records data (like `_trackPageview`, `_trackEvent`, `_trackTrans`...), the data may not get recorded. The problem is that the while the `['_xxyyz', 'xyz']` command may return, the tracking pixel request to record the data may not have been sent before the page redirect. – mike Dec 16 '13 at 14:56
  • [here](http://stackoverflow.com/a/12786181/787464) one says that it should work without a timer, but you may be right. – Akkumulator Dec 16 '13 at 19:24