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 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.

1 Answer 1


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'  %}
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('send', 'pageview');
{% endif %}

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

  • 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
    Commented Sep 12, 2014 at 16:12
  • 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. Commented Sep 12, 2014 at 16:15
  • 1
    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. Commented Sep 12, 2014 at 16:25
  • 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. Commented Sep 12, 2014 at 17:00

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.