Analytics: consentMode 'basic' keeps Google tags out until consent
Opt-in; the default stays 'advanced' (tag loads with denied signals). shouldLoadGoogle holds the rule so it is testable without a DOM. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0bfc509102
commit
9a72ebfaff
@@ -40,6 +40,17 @@ function injectScript(src: string) {
|
||||
}
|
||||
|
||||
export type AnalyticsProps = {
|
||||
/**
|
||||
* Google Consent Mode flavour.
|
||||
* - `'advanced'` (default): the Google tag (GA4 / GTM) loads before the
|
||||
* visitor decides, with every signal `denied`; Google gets cookieless
|
||||
* pings and models conversions.
|
||||
* - `'basic'`: no request to Google at all until the visitor grants analytics
|
||||
* or marketing — "zero tags before consent", like the non-Google pixels.
|
||||
* The Consent Mode default is still set first, so the tag starts with the
|
||||
* stored choice.
|
||||
*/
|
||||
consentMode?: 'advanced' | 'basic'
|
||||
/**
|
||||
* Core Web Vitals mode: hold back every tag's NETWORK load until the first
|
||||
* user interaction (scroll, pointer, touch, key) or `deferTimeoutMs` after the
|
||||
@@ -58,6 +69,24 @@ export type AnalyticsProps = {
|
||||
deferTimeoutMs?: number
|
||||
} & AnalyticsConfig
|
||||
|
||||
/**
|
||||
* Whether the Google tag may load now. Pure, so the consent rule is testable
|
||||
* without a DOM: 'basic' waits for an analytics or marketing grant, 'advanced'
|
||||
* relies on the Consent Mode default and loads as soon as `ready`.
|
||||
*/
|
||||
export function shouldLoadGoogle({
|
||||
consentMode,
|
||||
ready,
|
||||
state,
|
||||
}: {
|
||||
consentMode: 'advanced' | 'basic'
|
||||
ready: boolean
|
||||
state: { analytics?: boolean; marketing?: boolean }
|
||||
}): boolean {
|
||||
if (!ready) {return false}
|
||||
return consentMode === 'advanced' || Boolean(state.analytics || state.marketing)
|
||||
}
|
||||
|
||||
const INTERACTION_EVENTS = ['scroll', 'pointerdown', 'touchstart', 'keydown'] as const
|
||||
|
||||
/** True once third-party tags may load (immediately, or per `defer` rules). */
|
||||
@@ -101,7 +130,8 @@ function useDeferredReady(defer: boolean, timeoutMs: number): boolean {
|
||||
* - **Google (GA4 / GTM)** — Consent Mode: the visitor's stored choice is set as
|
||||
* the default BEFORE the tag loads (always immediately, even with `defer`),
|
||||
* and useConsent pushes an update the moment they decide. GTM wins if both
|
||||
* IDs are set.
|
||||
* IDs are set. With `consentMode: 'basic'` the tag itself waits for an
|
||||
* analytics or marketing grant.
|
||||
* - **Non-Google (Meta Pixel, Clarity, LinkedIn, TikTok)** — no Consent Mode, so
|
||||
* each loads ONLY after its category is granted (marketing for
|
||||
* Pixel/LinkedIn/TikTok, analytics for Clarity). Reacts live to the banner.
|
||||
@@ -115,6 +145,7 @@ function useDeferredReady(defer: boolean, timeoutMs: number): boolean {
|
||||
export function Analytics(props: AnalyticsProps) {
|
||||
const {
|
||||
clarityProjectId,
|
||||
consentMode = 'advanced',
|
||||
defer = false,
|
||||
deferTimeoutMs = 3500,
|
||||
ga4MeasurementId,
|
||||
@@ -139,9 +170,11 @@ export function Analytics(props: AnalyticsProps) {
|
||||
defaultConsentSet.current = true
|
||||
}, [ga4MeasurementId, gtmContainerId, texts.version])
|
||||
|
||||
// --- Google: load once `ready`; Consent Mode handles gating ---
|
||||
// --- Google: load once `ready` (and, in 'basic' mode, once consented);
|
||||
// Consent Mode handles what the tag may store ---
|
||||
const googleAllowed = shouldLoadGoogle({ consentMode, ready, state })
|
||||
useEffect(() => {
|
||||
if (!ready) {return}
|
||||
if (!googleAllowed) {return}
|
||||
if (!ga4MeasurementId && !gtmContainerId) {return}
|
||||
if (gtmContainerId) {
|
||||
window.dataLayer = window.dataLayer || []
|
||||
@@ -152,7 +185,7 @@ export function Analytics(props: AnalyticsProps) {
|
||||
gtag('js', new Date())
|
||||
gtag('config', ga4MeasurementId)
|
||||
}
|
||||
}, [ready, ga4MeasurementId, gtmContainerId])
|
||||
}, [googleAllowed, ga4MeasurementId, gtmContainerId])
|
||||
|
||||
// --- Non-Google: each when `ready` AND its consent category is granted ---
|
||||
// Loaders are idempotent, so re-running on state changes is safe; a tag loads
|
||||
|
||||
Reference in New Issue
Block a user