When you are developing a website you want to get as close as production as possible, to guarantee the final result will be appropriate and to make sure everything is going to work fine. But there are special cases when you want to add some code only on production, like stats code for example.
One easy way to do this is to check the server HTTP variables, the HTTP_HOST to be exact. This will contain your server host name, without the “http://” and the page URL. In my case, the value is “dev.enekoalonso.com”.
In PHP:
-
<?php if ($_SERVER['HTTP_HOST'] == 'dev.enekoalonso.com'): ?>
-
[Google Analytics code here]
-
<?php endif; ?>
That way I don’t mess up my stats while spending hours creating a demo in my development environment.

I think the solution depends on the programmer, and there could be as many solutions as programmers out there ;)
I like to use a variable (e.g: “IS_ALIVE”, “IS_PRODUCTION”, …) in the configuration file that will be TRUE after deploying the web app.
The only advantage I see for this method is that you have to write less if your page has different parts that are only enabled in production, but the usual case is just enabling analytics, so is not a great improvement anyway ;)
Yes, is true there have to be thousands of ways to do this.
What I like of this solution is that the code is the same on both development and production server. This is, I don’t have to change variables or flags every time I push something to production and the code on SVN is always clean and safe.
Of course, the problem with this approach is that it wont work if the production domain changes.
Finally, as you said, if you where to check this condition multiple times, it would be nice to have the result stored on a variable like
$isProduction = ($_SERVER['HTTP_HOST'] == 'dev.enekoalonso.com');?>