I want to use an environment variable to store Google Analytics tracking ID for a project. I've created an environment variable on Netlify named 'GOOGLE_ANALYTICS_ID'. I want to use this variable in my JavaScript file to replace the actual tracking ID.
Right now, all I'm doing is using 'GOOGLE_ANALYTICS_ID' as is to call the variable. It's showing a Reference Error in my console.
What's the correct way of doing this? I just want the tracking ID number in the JS file to be replaced with the environment variable name so it gets the value from Netlify rather than hardcoding it in the file.
您应该使用process.env.GOOGLE_ANALYTICS_ID
引用 env var。
https://app.netlify.com/teams/USERNAME/sites
https://app.netlify.com/sites/SITENAME/overview
https://app.netlify.com/sites/SITENAME/settings/general
https://app.netlify.com/sites/SITENAME/settings/deploys#environment
GOOGLE_ANALYTICS_ID
UA-XXXXXXX-X
I had some problems getting analytics to work in GatsbyJS. There are some caveats:
gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: GOOGLE_ANALYTICS_ID,
head: true,
anonymize: true,
},
},
]
gatsby build
commandThis means you cannot test tracking during local development with gatsby develop
.
From the plugin docs :
Note that this plugin is disabled while running
gatsby develop
. This way, actions are not tracked while you are still developing your project. Once you rungatsby build
the plugin is enabled. Test it withgatsby serve
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.