Skip to content

Add consent mode for Google Analytics registry script #243

Closed
@joachimhviid

Description

@joachimhviid

🆒 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions