2

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.

3

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'  %}
  <script>
    (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');
  </script>
{% 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 Sep 12 '14 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. – Stuart McCoy Sep 12 '14 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. – Brandon Kelly Sep 12 '14 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. – André Elvan Sep 12 '14 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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