0

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?

nickf
548k199 gold badges660 silver badges727 bronze badges
asked Sep 1, 2009 at 13:48
4
  • 3
    What does google-analytics.php contain? Commented Sep 1, 2009 at 13:52
  • Maybe you really were getting that much traffic from test/dev Commented Sep 1, 2009 at 13:53
  • Sorry, google-analytics.php contains the javascript code for analytics tracking. No PHP tags, just javascript. Commented Sep 1, 2009 at 15:08
  • Is the tracking code google-analytics.php produces is really correct? Otherwise did you check the values of Hostname field in Google Analytics content reports? It should show up the non-production hostnames, too, and that way you can see how much traffic they brought. Commented Sep 2, 2009 at 10:47

1 Answer 1

3

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?

answered Sep 1, 2009 at 13:54
Sign up to request clarification or add additional context in comments.

1 Comment

No, the traffic from local,dev & test was minimal. Our site gets tens of thousands of hits/day.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.