.env variables staging and prod

I am inserting some analytics tags into my wp_head using an analytics.php file in /site/web/app/themes/roots/app/analytics.php and including it in my functions .php collect(['setup', 'filters', 'analytics'])
I am loading the tracking code IDs from my .env file like so

 add_action( 'wp_head', function() {
    $tag_code = '';
    if(getenv('GTAG')) {
        $tag_code .= "
        <!-- Google tag (gtag.js) -->
        <script async src='https://www.googletagmanager.com/gtag/js?id=".getenv('GTAG')."'></script>
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());
    
          gtag('config', );
        </script>
        ";
    }
    if(getenv('GTM_ID')) {
        $tag_code .= "
        <!-- Google Tag Manager -->
        <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer',".getenv('GTM_ID').");</script>
        <!-- End Google Tag Manager -->
        ";
    }

    if(getenv('FB_PIXEL_CODE')) {
        $tag_code.= "
        <!-- Facebook Pixel Code -->
        <script>
        !function(f,b,e,v,n,t,s)
        {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
        n.callMethod.apply(n,arguments):n.queue.push(arguments)};
        if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
        n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];
        s.parentNode.insertBefore(t,s)}(window, document,'script',
        'https://connect.facebook.net/en_US/fbevents.js');
        fbq('init', ".getenv('FB_PIXEL_CODE').");
        fbq('track', 'PageView');
        </script>
        <noscript>
        <img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=".getenv('FB_PIXEL_CODE')."&ev=PageView&noscript=1' />
        </noscript>
        <!-- End Facebook Pixel Code -->
        ";
    }
    echo $tag_code;
});

my .env file I have added

GTM_ID='XXXXXX'
GTAG='XXXXXX'
FB_PIXEL_CODE='XXXXXX'

This is working on development with the code outputting in the head as expected but when I deploy to staging the scripts are not being outputted which makes me think that the env variables are not being copied across.

Any thoughts?
Am I doing something wrong?

That all looks good to me.

I know you may already have checked, but is .env actually present on staging? Typically, env / dotfiles aren’t deployed, as they relate to a single environment, and you’re deploying a new environment.

Bedrock excludes .env (and others) from the repo via .gitignore by default.

That’s what I thought, so how do we get around that?
Should I scp an env file to the current directory with just those env vars?

Yes, exactly. If one already exists, add to the end of it.

So I think the best way to go about adding environment specific env variables is to add them to your environment group_vars in trellis (assuming you are using rellis which I am)

2 Likes