# .env variables staging and prod

**URL:** https://discourse.roots.io/t/env-variables-staging-and-prod/24901
**Category:** sage
**Tags:** deploys, sage10, trellis
**Created:** 2023-03-03T12:24:44Z
**Posts:** 5

## Post 1 by @manu.weg — 2023-03-03T12:24:44Z

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?

---

## Post 2 by @talss89 — 2023-03-03T13:55:50Z

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.

---

## Post 3 by @manu.weg — 2023-03-03T16:32:59Z

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?

---

## Post 4 by @talss89 — 2023-03-03T16:54:21Z

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

---

## Post 5 by @manu.weg — 2023-03-05T11:12:43Z

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)

> [@Custom env variables trellis](https://discourse.roots.io/t/custom-env-variables-trellis/6330/2):
>
> If you want custom variables in the .env file created on deploy, each WP site will create env variables using the [wordpress\_env\_defaults](https://github.com/roots/trellis/blob/fc44e94d6f174792b00833d79a89dd92d4b1daf8/deploy.yml#L16-L23) from deploy.yml. To override or add to these env variables, you can edit group\_vars/\<environment\>/wordpress\_sites.yml. For example, if for example.com you wanted to override the default db\_name and add a new var new\_env\_var, you could add this for the site example.com: wordpress\_sites: example.com: ... env: db\_name: override\_name new\_env\_var: fo…
