Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Redirect with Google Analytics

I need help figuring out how to successfully redirect while including Analytics code.

  • I have a subdomain setup http://buuf.fractalsystems.org
  • The subdomain is actually just a subfolder http://fractalsystems.org/buuf
  • I have an HTML file in that subfolder which redirects to https://market.android.com/developer?pub=Fractal%20Systems

The code for that redirect file:

<head> <script type="text/javascript"> function delayedRedirect(){     window.location = "https://market.android.com/developer?pub=Fractal%20Systems" } </script> <script type="text/javascript">    var _gaq = _gaq || [];   _gaq.push(['_setAccount', 'UA-1234567-8']); <!--I have my real ID there-->   _gaq.push(['_trackPageview']);    (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> </head> <body onLoad="setTimeout('delayedRedirect()', 3000)"> <h2>ADW.BuuF.Theme is no more! You will be redirected to new and better apps in 3 seconds.</h2> </body> </html> 

This works as a redirect only if I don't include my Analytics code. I've tried moving the code around with no change.

QUESTION How can I add a redirect, of any kind, and still be able to track with Google Analytics?

I've tried PHP redirects with no success and am pretty sure htaccess redirects wont help although I'm open to suggestions.

The reason I'm using a JavaScript redirect is so I can continue to track with Google Analytics and also show a little message or make a custom page with the delay.

Thanks for any help. Doesn't have to be JS, please, any input is welcome if you know of a solution.

like image 826
TryTryAgain Avatar asked Jan 01 '12 09:01

TryTryAgain


1 Answers

Note: _gaq.push allows pushing of functions onto the queue. The following code should redirect after 250 milliseconds (to allow time for the tracking pixel) after the _trackPageview:

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1234567-8']); _gaq.push(['_trackPageview']); _gaq.push(function() {     setTimeout(function() {         window.location = "https://market.android.com/developer?pub=Fractal%20Systems";     }, 250); });  (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); })(); 
like image 174
mike Avatar answered Sep 19 '22 10:09

mike