In trying to improve our Google Analytics data for our website, I created a piece of PHP code that would determine the server that the website is being served from, and only serve up the GA code when being run from our Production server. The code is below, and works as it should:
<?php
switch( $_SERVER['HTTP_HOST'] ){
case 'website.dev':
echo '<!-- local - no google tracking code -->';
break;
case 'dev.website.com':
echo '<!-- dev - no google tracking code -->';
break;
case 'test.website.com':
echo '<!-- test -no google tracking code -->';
break;
default:
require ("google-analytics.php");
break;
} ?>
I had tried loading the analytics javascript with the require statement in a .js file, but the code wouldn't display for some reason. So I changed the name of the javascript file to .php, and it loads just fine (or so I thought). The code block above generates the desired results, and the code is only loaded when it is on our production server.
The problem is that since I've implemented this solution, our stats have dropped off a cliff. It's like the code isn't working. Analytics says that the code is installed fine, and I can see it in the raw HTML, but I suspect that it's not being run because it's being pulled in via a PHP page.
Anybody have any ideas?