DEV Community

Discussion on: Add google analytics to your Next.js app ✨

Collapse
 
murdercode profile image
Stefano Novelli

When you say:
“As you can see we are using env variables for the google-analytics tag because it must remain a secret 🤫”
Is not correct. You can use env for make the script “domain-agnostic” or whatever else, but there’s not any secret to print a prefixed NEXT_PUBLIC variable.

Collapse
 
avneesh0612 profile image
Avneesh Agarwal

Oh okay, what do you mean by domain agnostic?

Collapse
 
murdercode profile image
Stefano Novelli

If you have two o more domains (like a staging portal, or another website which is based to your actual project) you can fill the .env with your actual domain; an agnostic approach to a domain which will host your software.

Regarding to .env content instead, you can expose them – and your code do it – to public. This is necessary for Google Analytics and your guests to init the correct Analytics property.

More info here: nextjs.org/docs/basic-features/env...

Thread Thread
 
avneesh0612 profile image
Avneesh Agarwal

Oh I see, but still it would be better to use env, right?

Thread Thread
 
murdercode profile image
Stefano Novelli

Sure :)

Thread Thread
 
avneesh0612 profile image
Avneesh Agarwal

Cool, also removed that secret part from blog thanks :P