How can I access to an environment variable in Javascript?

I would like to insert my Hotjar Identifier only on certain environments. How can I do that?

Add the variable using the CLI

You can add an environment variable to a specific environment using the Platform.sh CLI.

In your local repository, type the command platform variable:create. The CLI will then take you through the steps to configure the variable.

  • Level: Since you want the variable to be visible only to specific environments, type environment.
  • Environment: Name the environment you want the variable to be visible to (i.e. updates).
  • Name: Name the variable, which in your case might be HOTJARID.
  • Value: Assign a value for HOTJARID, like the string 'some-value'.
  • JSON: Tell the CLI whether or not the value is JSON formatted.
  • Sensitive: If you do not want anyone to be able to SSH into the application container and view the value of this variable, choose y.
  • Prefix: For environment variables, choose the prefix env:.
  • Enabled: Enable the variable.
  • Inheritable: If you would only like the variable to be visible to this current branch, choose n. On the other hand, if you plan on creating children of this branch that will also need access to HOTJARID, choose y instead.

When you have completed these options, the CLI will create the new variable and redeploy your environment to reflect the changes.

Creating variable env:HOTJARID on the environment updates
+----------------+---------------------------+
| Property       | Value                     |
+----------------+---------------------------+
| id             | env:HOTJARID              |
| created_at     | 2019-05-06T18:15:49-04:00 |
| updated_at     | 2019-05-06T18:15:49-04:00 |
| name           | env:HOTJARID              |
| attributes     | {  }                      |
| value          | 'some-value'              |
| is_json        | false                     |
| project        | wiqte7at4yg22             |
| environment    | updates                   |
| inherited      | false                     |
| is_enabled     | true                      |
| is_inheritable | false                     |
| is_sensitive   | false                     |
| level          | environment               |
+----------------+---------------------------+
Waiting for the activity p67lnusorjsts (Chad Carlson added variable env:HOTJARID to environment updates):
    Building application 'app' (runtime type: php:7.1, tree: efd651e)
      Reusing existing build for this tree ID
    
    Provisioning certificates
      Environment certificates
      - certificate f4a5030: expiring on 2019-08-04 18:31:01+00:00, covering {,www}.updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site
    
    
    Redeploying environment updates
    Environment configuration
        app (type: php:7.1, size: S, disk: 2048)
        mysqldb (type: mysql:10.2, size: S, disk: 2048)
    
      Environment routes
        http://updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/ redirects to https://updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/
        http://www.updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/ redirects to https://www.updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/
        https://updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/ is served by application `app`
        https://www.updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/ redirects to https://updates-gv2zwpa-wiqte7at4yg22.eu-3.platformsh.site/
  [============================] 8 secs (complete)
Activity p67lnusorjsts succeeded

You can also combine these configurations into a single CLI command:

platform variable:create --level environment --environment updates --name HOTJARID --value 'some-value' --json false --sensitive false --prefix 'env:' --enabled true --inheritable false

Add the variable using the management console

Alternatively you can go through the same configuration from within the management console:

  1. From your project, click on the (parent) environment you want the variable to be exposed to.
  2. Click the Settings link at the top of the page, and then the Variables tab on the sidebar of the next page.
  3. Click the Add + button at the top of the page.
  4. Fill out the forms to configure your variable, and when complete click Add variable.
1 Like