PHP code depending on server response

I wanted to load specific Google Analytics tracking codes depending on the HTTP response of our server for a specific page. If a page is ok (HTTP response is 200), the code should be:

 <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-XXXXXXXX-X', 'auto');
   ga('set', 'forceSSL', true);
   ga('require', 'displayfeatures');
   ga('send','pageview');
 </script>

But if the HTTP response is 404 or 410 (page not found), I wanted the code to be:

Read More
 <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-XXXXXXXX-X', 'auto');
   ga('set', 'forceSSL', true);
   ga('require', 'displayfeatures');
   ga('send','pageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer);
 </script>

Note the difference on the 2nd-to-the-last line.

This code is saved on our header.php file (we’re using WordPress) which applies to all pages, including the 404 page. In order for the system to load a different code for the 404 page, here’s what I did:

<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-XXXXXXXX-X', 'auto');
  ga('set', 'forceSSL', true);
  ga('require', 'displayfeatures');
  ga(
<?php
$HTTPStatus = $_SERVER["REDIRECT_STATUS"];
if ($HTTPStatus==404) ['send','pageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer];)
elseif ($HTTPStatus==410) ['send','pageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer];)
else ['send','pageview'];)
?>
</script>

I’m a newbie on this and I can’t seem to get it right. What am I doing wrong? Is it even right for me to put this on header.php (just before </head>)? Any feedback will be greatly appreciated.

Related posts

1 comment

  1. I think this one will be the good idea.

    Follow these steps

    1.Create 404 page, like 404.php

    2.Create new header like, header-nofound.php,

    3.In your 404.php, use header as <?php get_header('nofound'); ?>

    4.In your header-nofound.php, you can put the ga code you want in the case of 404,

    Thanks.this might help you

Comments are closed.