johannschopplich / nuxt-gtag

🔸 Google Analytics & Ads integration made easy
https://developers.google.com/tag-platform/gtagjs
MIT License
302 stars 9 forks source link

`useGtagConsent(true)` should initialize the Gtag script #19

Closed chester-af closed 1 year ago

chester-af commented 1 year ago

Environment

Reproduction

https://stackblitz.com/edit/github-vihbux?file=app.vue

Describe the bug

Hello,

When I use the useGtagConsent() function and set it to true after clicking a button, it does not insert the script into the head which means that events aren't sent to Google Analytics. if you place the useGtagConsent(true) in onMounted of the component, it will insert the script into the head though but I only want to insert the script if the user clicks an accept button.

Additional context

No response

Logs

No response

hajiskyy commented 1 year ago

Doesn't look like you have it installed it yet on that reproduction link. you might want to check it again properly

martinsjek commented 1 year ago

For me it is that useGtagConsent(true) does not add gtag script to the head when the nuxt.config.ts looks like this:

gtag: { initialConsent: false, },

And .env file looks like this: NUXT_PUBLIC_GTAG_ID=G-XXXXXXXXXX

If I add ID to nuxt.config.ts then it works:

gtag: { id: 'G-XXXXXXXXXX' initialConsent: false, },

But it seems that the issue is when running in production, when running npm run dev, it seems that everything works just fine.

johannschopplich commented 1 year ago

This feature has been added in the latest release of Nuxt Gtag! You can now omit the id in your Nuxt Gtag module options and use useGtagConsent to pass a custom Gtag ID. If Gtag hasn't been initialized yet, it will be.