Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the condition to check google analytics Id #451

Open
Shubhdeep12 opened this issue Apr 28, 2023 · 8 comments
Open

Change the condition to check google analytics Id #451

Shubhdeep12 opened this issue Apr 28, 2023 · 8 comments

Comments

@Shubhdeep12
Copy link

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;

@MauricioRobayo
Copy link
Owner

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?

@bugzpodder
Copy link

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.

@MauricioRobayo
Copy link
Owner

changing the env and redeploying

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

@bugzpodder
Copy link

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.

@Shubhdeep12
Copy link
Author

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

@bugzpodder
Copy link

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/

@Shubhdeep12
Copy link
Author

Shubhdeep12 commented Jun 14, 2023

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

@AlecRust
Copy link

AlecRust commented Sep 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants