nuxt / scripts

Third-Party Scripts Meets Nuxt Developer Experience.
https://scripts.nuxt.com
MIT License
308 stars 41 forks source link

Add consent mode for Google Analytics registry script #243

Open joachimhviid opened 2 months ago

joachimhviid commented 2 months ago

πŸ†’ Your use case

The current Google Analytics registry does not use consent mode. This is used for communicating the user's consent status to Google.

πŸ†• The solution you'd like

I would like to have it as part of the options for the registry script so I can easily define default consent settings as well as the option of opting into consent mode v2.

πŸ” Alternatives you've considered

Currently I have opted to just copy the registry script and redefine it with an expanded clientInit property since the default consent needs to be set before gtag('js', ...) and gtag('config', ...) is called.

[...]
clientInit: import.meta.server
    ? undefined
    : () => {
        window.dataLayer = window.dataLayer || []
        window.gtag = function () {
          // eslint-disable-next-line prefer-rest-params
          window.dataLayer.push(arguments)
        }

        window.gtag('consent', 'default', {
          ad_user_data: 'denied',
          ad_personalization: 'denied',
          ad_storage: 'denied',
          analytics_storage: 'denied',
        })
        window.gtag('set', { ads_data_redaction: 'true' })

        window.gtag('js', new Date())
        window.gtag('config', website.analytics4Key)
      },
[...]

ℹ️ Additional info

No response

harlan-zw commented 1 month ago

Thoughts @huang-julien, @flashdesignory ?

flashdesignory commented 1 month ago

ah I see... it has to happen before the config gets send, otherwise it wouldn't be a problem. Seems reasonable to include.

huang-julien commented 1 month ago

Thanks for the issue ! This is a must have for EU users πŸ˜