<p>Yes, if you set up proper <a href="http://buildwithcraft.com/docs/multi-environment-configs" rel="nofollow">multi environment configs</a>, this isn't very hard. My config/general.php file usually look something like this:</p>

<pre><code>return array(

  '*' =&gt; array(
     // some general settings here
  ),

  'mydomain.craft.dev' =&gt; array(
    'environment' =&gt; 'local',
    'devMode' =&gt; true,
    'useCompressedJs' =&gt; false,
    'siteUrl' =&gt; 'http://mydomain.craft.dev/',

    'environmentVariables' =&gt; array(
      'fileSystemPath' =&gt; $_SERVER['DOCUMENT_ROOT'] . '/../',
      'publicRootPath'    =&gt; 'public/'
    ),
  ),

  // staging and other environments here

  'mydomain.com' =&gt; array(
    'environment' =&gt; 'prod',
    'devMode' =&gt; false,
    'useCompressedJs' =&gt; true,
    'siteUrl' =&gt; 'http://mydomain.com/',

    'environmentVariables' =&gt; array(
      'fileSystemPath' =&gt; '/path/to/system/root/',
      'publicRootPath' =&gt; 'html/'
    )
  ) 

);
</code></pre>

<p>Then, to only include the GA tracking tag when I'm on the prod server, I include this in my html head:</p>

<pre><code>{% if craft.config.environment=='prod'  %}
  &lt;script&gt;
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('send', 'pageview');
  &lt;/script&gt;
{% endif %}
</code></pre>

<p>You could of course check siteUrl instead, but I prefer to use something a little more abstract. Just in case the siteUrl changes.</p>

CC BY-SA 3.0
2014-09-12T16:12:48.820Z
As Andre demonstrated, you can set environment variables to anything you want... If you really want to be specific in this use-case, you could make an environment variable called "showGA", and just have it return a boolean true/false. `{% if craft.config.environmentVariables['showGA'] %}`
Lindsey D
CC BY-SA 3.0
2014-09-12T16:15:42.713Z
Thanks André.

@LindseyD, also a very good point. For this I'll stick with the André's suggestion since there is only one instance when I want analytics to load but if I ever need to load something in multiple environments yours will be a better approach.
Stuart McCoy
CC BY-SA 3.0
2014-09-12T16:25:12.520Z
Unless you’re planning on having `{environment}`/`{showGA}`/etc. tags inside your CP’s Site URL setting, or one of your asset source settings, there’s **no** point in putting those custom variables inside your `environmentVariables` config array - they will only slow the site down as Craft will start having to parse for them for any settings that support environment variable tags. Instead you can just put them in the main config array, and reference them using `craft.config.environment`, etc., from your templates.
Brandon Kelly
CC BY-SA 3.0
2014-09-12T17:00:58.863Z
Oh, I had no idea we could define whatever variables we wanted in the config, and access them like that. Has it been like this since Craft was released, or was it introduced at some point? I've fixed this in my examples, and also moved my siteUrl out of the environmentVariables, I guess that's the correct way to define it now.
André Elvan
CC BY-SA 3.0
2014-09-12T15:35:17.280Z
2014-09-12T16:56:42.213Z
2014-09-12T16:56:42.213Z
André Elvan
<p>In the past I would comment out analytics code on my dev and staging sites then uncomment the code when it went live. I ran across <a href="http://buildwithcraft.com/docs/templating/craft.config" rel="nofollow">this page in the Craft docs</a> the other day and started thinking, can I use this to access the siteUrl from the environment variable array and specify a value in my conditional (i.e. {% craft.config.environmentVariable('siteUrl', '<a href="http://www.example.com" rel="nofollow">http://www.example.com</a>') %}? This way I can add the analytics code and only worry about it loading on the live site.</p>

<config><conditional><environments>
Is using craft.config to access siteUrl possible?
# Is using craft.config to access siteUrl possible?

(Asked by: username_0 on 2014-09-12)

In the past I would comment out analytics code on my dev and staging sites then uncomment the code when it went live. I ran across [this page in the Craft docs](http://buildwithcraft.com/docs/templating/craft.config) the other day and started thinking, can I use this to access the siteUrl from the environment variable array and specify a value in my conditional (i.e. {% craft.config.environmentVariable('siteUrl', '<http://www.example.com>') %}? This way I can add the analytics code and only worry about it loading on the live site.

**Tags:** config, conditional, environments

---

## 1 answers

### Answer by username_1 on 2014-09-12. Score: 3 | Accepted answer

Yes, if you set up proper multi environment configs, this isn't very hard. My config/general.php file usually look something like this:
return array(

  '*' => array(
     // some general settings here
  ),

  'mydomain.craft.dev' => array(
    'environment' => 'local',
    'devMode' => true,
    'useCompressedJs' => false,
    'siteUrl' => 'http://mydomain.craft.dev/',

    'environmentVariables' => array(
      'fileSystemPath' => $_SERVER['DOCUMENT_ROOT'] . '/../',
      'publicRootPath'    => 'public/'
    ),
  ),

  // staging and other environments here

  'mydomain.com' => array(
    'environment' => 'prod',
    'devMode' => false,
    'useCompressedJs' => true,
    'siteUrl' => 'http://mydomain.com/',

    'environmentVariables' => array(
      'fileSystemPath' => '/path/to/system/root/',
      'publicRootPath' => 'html/'
    )
  ) 

);

Then, to only include the GA tracking tag when I'm on the prod server, I include this in my html head:
{% if craft.config.environment=='prod'  %}
  
{% endif %}

You could of course check siteUrl instead, but I prefer to use something a little more abstract. Just in case the siteUrl changes.

> **Comments:**
> **username_2**: As Andre demonstrated, you can set environment variables to anything you want... If you really want to be specific in this use-case, you could make an environment variable called "showGA", and just have it return a boolean true/false. `{% if craft.config.environmentVariables['showGA'] %}`  
> **username_0**: Thanks André.  @LindseyD, also a very good point. For this I'll stick with the André's suggestion since there is only one instance when I want analytics to load but if I ever need to load something in multiple environments yours will be a better approach.  
> **username_3**: Unless you’re planning on having `{environment}`/`{showGA}`/etc. tags inside your CP’s Site URL setting, or one of your asset source settings, there’s **no** point in putting those custom variables inside your `environmentVariables` config array - they will only slow the site down as Craft will start having to parse for them for any settings that support environment variable tags. Instead you can just put them in the main config array, and reference them using `craft.config.environment`, etc., from your templates.  
> **username_1**: Oh, I had no idea we could define whatever variables we wanted in the config, and access them like that. Has it been like this since Craft was released, or was it introduced at some point? I've fixed this in my examples, and also moved my siteUrl out of the environmentVariables, I guess that's the correct way to define it now.  

---

