views:

245

answers:

1

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?

+3  A: 

If it's in the HTML then PHP has done its job, and the browser doesn't care what generated the code.

Have you considered that between your local development servers, online dev servers and online test servers, you may have been generating all the traffic that you've now seen drop off the cliff?

nickf
SerpicoLugNut