7

I have a simple page that I need to execute some GWO & GATC js and then redirect to another url.

<head>

<script>
function utmx_section(){}function utmx(){}
(function(){var k='xxx',d=document,l=d.location,c=d.cookie;function f(n){
if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return escape(c.substring(i+n.
length+1,j<0?c.length:j))}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
d.write('<sc'+'ript src="'+
'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'
+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"></sc'+'ript>')})();
</script><script>utmx("url",'A/B');</script>

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['gwo._setAccount', 'UA-xxxxxx-x']);
  _gaq.push(['gwo._trackPageview', '/xxxxxxxx/test']);
  (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);
  })();
</script>

<script>
    window.location = 'MY REDIRECT URL';
</script>
</head>

The problem I'm having is that I need to guarantee that the GWO & GATC code is executed BEFORE my window.location is called. I could do setTimeout, but that offers no guarantees and adds additional load time.

Any ideas on how I can do this?

doremi
  • 14,921
  • 30
  • 93
  • 148

2 Answers2

26

I believe I've found the solution. It turns out, you can push functions to _gaq. The _gaq is then executed sequentially ensuring the GA stuff is taken care of before getting to my redirect.

 var _gaq = _gaq || [];
  _gaq.push(['gwo._setAccount', 'UA-xxxxxx-x']);
  _gaq.push(['gwo._trackPageview', '/xxxxxxxx/test']);

  _gaq.push(function(){
    window.location = 'MY REDIRECT URL';
  });

http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#PushingFunctions

doremi
  • 14,921
  • 30
  • 93
  • 148
  • 9
    This works and makes sense, but for some reason the [official suggestion](http://www.google.com/support/analytics/bin/answer.py?answer=55527) is to use `setTimeout` to redirect a few ms after the event is pushed to the GA queue. – lucasrizoli Jun 03 '11 at 17:18
  • 2
    @lucasrizoli and all I think that the function will be called once the request for the pixel has been made by GA code but not after it returns. Maybe that is the reason that Google suggest to delay the redirect although I don't know if its important to wait for the request to return. – Shay Erlichmen Sep 27 '11 at 15:20
  • 8
    While processed in sequence from the queue, nothing says the previous instructions will have finished being processed before the next one is fired => timeout remains needed. However Google is listening at such requierements, stay tuned on the ga.js changelog http://code.google.com/intl/fr/apis/analytics/community/gajs_changelog.html – Open SEO Mar 29 '12 at 14:30
6

the _gaq.push didn't work for me...

I used the already known setTimeout solution:

$('.link-site').click(function(e){
    e.preventDefault();
    _gaq.push(['_trackPageview', '/btn-link-site']);
    window.setTimeout(function(){
        window.location=$('.link-site').attr('href');
    },300);
});
Jota Russo
  • 69
  • 1
  • 2
  • 4
    This is better than the accepted solution because commands pushed to the async queue happen, well asynchronously :) This probably will not work 100% of the time, but it is more likely to succeed than not pausing at all. – dana Aug 14 '12 at 04:25
  • 1
    It's a hack and not a proper solution. There is no guaranties that event will be executed before the redirect. – Slava Fomin II Jun 28 '13 at 10:43