1

Will Google analytics code be executed prior to a header location redirect? If not is there another method?

Example:

<html>
<body>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-0000000-0', 'domain.com');
  ga('send', 'pageview');

</script>
</body>
</html>
<?php
header('Location: http://domain.com');
?>
ryerye
  • 123
  • 1
  • 7
  • 16

3 Answers3

1

try that - without PHP and (sorry) without any redirect-reason-code (like "301" etc.).

In my case it works fine:

<html>
<body>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-0000000-0', 'domain.com');
  ga('send', 'pageview');
  
  window.location = 'http://www.domain.com/redirect-example';

</script>
</body>
</html>

I'm happy about any feedback :-)

Andy G.
  • 21
  • 2
  • 1
    This does provide a possible solution, but you did not give any explanation for why the code in the question will not work and what your solution solves differently. Regarding your actual code: This is based on luck. The browser might be able to send the tracking call in time or it might not. If the tracking code somehow allows attaching an event to it, that would be the best solution. Otherwise at least a short timeout would increase the chance of the tracking call to actually go through. – Till Helge Jul 05 '16 at 16:15
0
<?PHP
ob_start();

echo "<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-0000000-0', 'domain.com');
  ga('send', 'pageview');
</script>";

header("Location: http://www.domain.com/");        
ob_flush();  
enassr
  • 3
  • 1
  • 1
0

Think about what you are trying to do here:

Google Analytics code is executed by the browser. PHP is a pre-processor that generates a HTTP response on the server.

So, the order of execution is:

  1. Request comes in (on Apache, NGINX, whatever)
  2. PHP gets the requests and performs the necessary actions
  3. The output (HTML usually) generated by PHP is sent back to the client
  4. The client's browser renders the response
  5. Any JavaScript code in the response is run in the client's browser

An HTTP redirect would be generated by PHP in Step 2. And it tells the client's browser to go somewhere else and start over with Step 1. But you basically expect that Step 5 is executed somewhere in-between.

Clearly: That won't work.

Either you find a way to trigger Google Analytics from the server-side (which probably defeats the purpose of doing tracking) or you just will have to get rid of either the redirect (and replace it with a JavaScript location change) or skip the tracking call.

If all you need is e.g. a download counter, you could just have PHP record the hit before it issues the redirect. That would work.

Till Helge
  • 9,253
  • 2
  • 40
  • 56