For one of my projects running on Google App Engine, I’m using Google Analytics to track traffic, but I didn’t want the tracking code showing up in my local development environment. Fortunately, detecting where your code is running is easy.

Take a look at this sample:


import os

try:
  is_dev = os.environ['SERVER_SOFTWARE'].startswith('Dev')
except:
  is_dev = False

is_prod = not(is_dev)

If the is_dev and is_prod variables are exposed to your templates, you can do something like this:


{% if is_prod %}
 {% include "analytics.html" %}
{% endif %}

Hope this helps!