I have a localised site. I want to add in kiss metric and google analytic tracking codes for each locale, however, I don't want the tracking codes to output in staging and dev environments.

  • I considered using globals, but then I'd need a way to print the global in the template if the environment is production.
  • I have also considered using environment variables for in general.php file for the production environment and print the environment variable in the template.

Does anyone have advice on best practice for this.

2 Answers 2


If people don't need to change these through the control panel, I would put them in the general.php config file.

Here's how I would do it:

return array(
  '*' => array (
    // default to not doing analytics
    'doAnalytics' => false,
    // all the analytics codes, by locale
    'gaCodes' => array(
      'fr_fr' => 'UAxxx',
      'en_us' => 'UAxxy'
  // CRAFT_ENVIRONMENT is set to one of 'LOCAL', 'STAGING', or 'PRODUCTION' in index.php
  'LOCAL' => array (
  'STAGING' => array (
  'PRODUCTION' => array (
    'doAnalytics' => true,

Then in your template:

{% if craft.config.doAnalytics %}
  ...{{ craft.config.gaCodes[craft.locale] }}...
{% endif %}

Here {% if ... %} tests if we want to do analytics, and {{ craft.config.gaCodes[craft.locale] }} pulls out the gaCode for the current locale.

  • Yup - I think that is a good approach.
    – Matt
    Commented Nov 26, 2014 at 8:54

You could create an array for your analytics codes by local in general.php and set the devMode configuration variable to "true" for your dev and staging environments. Then just test for devMode in your templates.

For example, in craft/config/general.php...

return array(

  '*' => array(

    // local analytics
    'google_analytics' => array(
      'en_us' => 'UA-xxxx',
      'fr_fr' => 'UA-xxxx',

  // Production env
  'example.com' => array(

  // Development env
  'dev.example.com' => array(
      'devMode' => true,

  // Staging env
  'staging.example.com' => array(
      'devMode' => true,


Then in your templates use a conditional to test if you're not in devMode:

{% if not craft.config.devMode %}

  ( Analytics code here... )
  {{ craft.config.google_analytics[craft.locale] }}

{% endif %}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.