Answers.Body: <p>You could create an array for your analytics codes by local in <code>general.php</code> and set the <code>devMode</code> configuration variable to "true" for your dev and staging environments. Then just test for devMode in your templates.</p>
  
  <p>For example, in <code>craft/config/general.php</code>...</p>
  
  <pre><code>return array(
  
    '*' =&gt; array(
  
      // local analytics
      'google_analytics' =&gt; array(
        'en_us' =&gt; 'UA-xxxx',
        'fr_fr' =&gt; 'UA-xxxx',
      ),
    ),
  
    // Production env
    'example.com' =&gt; array(
    ),
  
    // Development env
    'dev.example.com' =&gt; array(
        'devMode' =&gt; true,
    ),
  
    // Staging env
    'staging.example.com' =&gt; array(
        'devMode' =&gt; true,
    ),
  
  );
  </code></pre>
  
  <p>Then in your templates use a conditional to test if you're not in devMode:</p>
  
  <pre><code>{% if not craft.config.devMode %}
  
    ( Analytics code here... )
    {{ craft.config.google_analytics[craft.locale] }}
  
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-26T12:20:40.050Z
Answers.LastActivityDate: 2014-11-26T12:20:40.050Z
Answers.UserDisplayName: Alex Roper
Answers.Body: <p>If people don't need to change these through the control panel, I would put them in the general.php config file.</p>
  
  <p>Here's how I would do it:</p>
  
  <pre><code>return array(
    '*' =&gt; array (
      // default to not doing analytics
      'doAnalytics' =&gt; false,
      // all the analytics codes, by locale
      'gaCodes' =&gt; array(
        'fr_fr' =&gt; 'UAxxx',
        'en_us' =&gt; 'UAxxy'
      ),
    ),
    // CRAFT_ENVIRONMENT is set to one of 'LOCAL', 'STAGING', or 'PRODUCTION' in index.php
    'LOCAL' =&gt; array (
    ),
    'STAGING' =&gt; array (
    ),
    'PRODUCTION' =&gt; array (
      'doAnalytics' =&gt; true,
    ),
  );
  </code></pre>
  
  <p>Then in your template:</p>
  
  <pre><code>{% if craft.config.doAnalytics %}
    ...{{ craft.config.gaCodes[craft.locale] }}...
  {% endif %}
  </code></pre>
  
  <p>Here <code>{% if ... %}</code> tests if we want to do analytics, and <code>{{ craft.config.gaCodes[craft.locale] }}</code> pulls out the gaCode for the current locale.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-26T08:54:48.597Z
Answers.Text: Yup - I think that is a good approach.
Answers.UserDisplayName: Matt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-26T08:49:21.133Z
Answers.LastActivityDate: 2014-11-26T08:49:21.133Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>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. </p>
  
  <ul>
  <li>I considered using globals, but then I'd need a way to print the global in the template if the environment is production. </li>
  <li>I have also considered using environment variables for in general.php file for the production environment and print the environment variable in the template.</li>
  </ul>
  
  <p>Does anyone have advice on best practice for this. </p>
  
Tags: <multi-environment><analytics>
Title: Add localised tracking codes for production environments
ThreadText: # Add localised tracking codes for production environments
  
  (Asked by: username_0 on 2014-11-26)
  
  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.
  
  **Tags:** multi-environment, analytics
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-11-26. Score: 7 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: Yup - I think that is a good approach.  
  
  ---
  
  ### Answer by username_1 on 2014-11-26. Score: 3
  
  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 %}
  
  ```
  
  
  ---
  
  