nextjs-google-analytics icon indicating copy to clipboard operation
nextjs-google-analytics copied to clipboard

Change the condition to check google analytics Id

Open Shubhdeep12 opened this issue 2 years ago • 8 comments

https://github.com/MauricioRobayo/nextjs-google-analytics/blob/86b434f09d57ef7a591c4246ee532e52013d8118/src/components/GoogleAnalytics.tsx#L33

Here in above referenced code _gaMeasurementId is formed using - const _gaMeasurementId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID ?? gaMeasurementId;

What if a user wants to use multiple google analytics IDs for different purposes in any use case? Why we didn't go with - const _gaMeasurementId = gaMeasurementId ?? process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID;

Shubhdeep12 avatar Apr 28 '23 03:04 Shubhdeep12

Hello!

The reason is mainly historical, as the library initially only provided the option to get the gaMeasurementId from the env file. Later it was suggested to also be able to provide it as a prop.

As there were already users of the library relying on the env, we added the prop kind of as a fallback option.

Also, having the env take precedence over the prop let user modify the value by changing the env and redeploying without having to commit any changes, which can be also convenient in many cases.

All that being said, I'm not really sure I get what you mean by:

What if a user wants to use multiple google analytics IDs for different purposes in any use case?

Do you have an example for this?

MauricioRobayo avatar May 22 '23 01:05 MauricioRobayo

Also, having the env take precedence over the prop let user modify the value by changing the env and redeploying without having to commit any changes, which can be also convenient in many cases.

Is this accurate? According to NextJS this is a a build time variable.

All that being said, I'm not really sure I get what you mean by:

What if a user wants to use multiple google analytics IDs for different purposes in any use case?

Do you have an example for this?

I have a staging build and a production build and would like to deploy and use the same code. I can have different env vars but because NEXT_PUBLIC_GA_MEASUREMENT_ID is injected at "build time" I cannot actually change it by setting a different env var at the run time.

bugzpodder avatar Jun 10 '23 18:06 bugzpodder

changing the env and redeploying

After changing your env you'll need to re-deploy

MauricioRobayo avatar Jun 10 '23 19:06 MauricioRobayo

changing the env and redeploying

After changing your env you'll need to re-deploy

Ok, not too familiar with vercel but i guess re-deploy means it'll re-do build as part of it, which makes sense.

bugzpodder avatar Jun 11 '23 17:06 bugzpodder

Hello!

The reason is mainly historical, as the library initially only provided the option to get the gaMeasurementId from the env file. Later it was suggested to also be able to provide it as a prop.

As there were already users of the library relying on the env, we added the prop kind of as a fallback option.

Also, having the env take precedence over the prop let user modify the value by changing the env and redeploying without having to commit any changes, which can be also convenient in many cases.

All that being said, I'm not really sure I get what you mean by:

What if a user wants to use multiple google analytics IDs for different purposes in any use case?

Do you have an example for this?

Hey @MauricioRobayo Sorry for the late reply.

Here is an example - https://github.com/EddieHubCommunity/LinkFree

In the above project, there can be different pages for each user. I proposed a feature here above to allow the users to include their G-tag in data and then they can track their analytics individually. So this way there can be different Google Analytics IDs for a single project. But I was not able to achieve that by next-google-analytics. Let me know if I am wrong somewhere.

Thanks

Shubhdeep12 avatar Jun 12 '23 03:06 Shubhdeep12

Hello! The reason is mainly historical, as the library initially only provided the option to get the gaMeasurementId from the env file. Later it was suggested to also be able to provide it as a prop. As there were already users of the library relying on the env, we added the prop kind of as a fallback option. Also, having the env take precedence over the prop let user modify the value by changing the env and redeploying without having to commit any changes, which can be also convenient in many cases. All that being said, I'm not really sure I get what you mean by:

What if a user wants to use multiple google analytics IDs for different purposes in any use case?

Do you have an example for this?

Hey @MauricioRobayo Sorry for the late reply.

Here is an example - https://github.com/EddieHubCommunity/LinkFree

In the above project, there can be different pages for each user. I proposed a feature here above to allow the users to include their G-tag in data and then they can track their analytics individually. So this way there can be different Google Analytics IDs for a single project. But I was not able to achieve that by next-google-analytics. Let me know if I am wrong somewhere.

Thanks

I was able to do this via _app.js from https://www.makeuseof.com/nextjs-google-analytics/

bugzpodder avatar Jun 14 '23 06:06 bugzpodder

Yes @bugzpodder this is an alternate way. But can we do this using only nextjs-google-analytics?

Shubhdeep12 avatar Jun 14 '23 06:06 Shubhdeep12

What if a user wants to use multiple google analytics IDs for different purposes in any use case?

This is how I landed here. I want to set two tracking IDs, one for Google Ads, another for GA4. There should not be two separate tracking snippets on a page.

react-ga4 supports this but doesn't seem as well tied into Next.js.

AlecRust avatar Sep 01 '23 13:09 AlecRust