MauricioRobayo/nextjs-google-analytics

Change the condition to check google analytics Id

Shubhdeep12 opened this issue · 8 comments

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;

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?

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.

changing the env and redeploying

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

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.

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

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/

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

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.