Added not-found support for sitesettings
This commit is contained in:
@@ -3,8 +3,10 @@ import { useEffect } from 'react'
|
||||
|
||||
import type { AnalyticsConfig } from './types.js'
|
||||
|
||||
import { useConsentContext } from '../consent/client.js'
|
||||
import { gtag } from '../consent/googleConsent.js'
|
||||
import { CONSENT_COOKIE, DEFAULT_CONSENT, parseConsent, setDefaultConsent } from '../consent/index.js'
|
||||
import { loadClarity, loadLinkedIn, loadMetaPixel, loadTikTok } from './pixels.js'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@@ -34,37 +36,66 @@ function injectScript(src: string) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads Google Analytics — GTM if a container ID is set, otherwise GA4 gtag.
|
||||
* Loads every configured analytics / marketing tag, each respecting consent:
|
||||
*
|
||||
* Consent Mode is initialised to the visitor's stored consent BEFORE the tag
|
||||
* loads (via setDefaultConsent from the consent module), so tags respect the
|
||||
* choice from the first hit; useConsent sends an update the moment the visitor
|
||||
* decides. IDs are public and come from SiteIntegrations, passed in as props
|
||||
* (the client project reads them server-side).
|
||||
* - **Google (GA4 / GTM)** — loaded immediately with Consent Mode: the visitor's
|
||||
* stored choice is set as the default BEFORE the tag loads, and useConsent
|
||||
* pushes an update the moment they decide. GTM wins if both IDs are set.
|
||||
* - **Non-Google (Meta Pixel, Clarity, LinkedIn, TikTok)** — these vendors have
|
||||
* no Consent Mode, so they load ONLY after the matching category is granted
|
||||
* (marketing for Pixel/LinkedIn/TikTok, analytics for Clarity). Reacts live to
|
||||
* the consent banner: accept → the tag loads immediately, no reload.
|
||||
*
|
||||
* Renders nothing — it only injects the scripts. Mount once, high in the tree
|
||||
* (e.g. root layout), inside ConsentProvider.
|
||||
* All IDs are public and come from SiteIntegrations (getAnalyticsConfig), passed
|
||||
* in as props. Renders nothing. Mount once, high in the tree, INSIDE
|
||||
* <ConsentProvider> (required — it reads live consent state).
|
||||
*/
|
||||
export function Analytics({ ga4MeasurementId, gtmContainerId }: AnalyticsConfig) {
|
||||
export function Analytics(props: AnalyticsConfig) {
|
||||
const {
|
||||
clarityProjectId,
|
||||
ga4MeasurementId,
|
||||
gtmContainerId,
|
||||
linkedInPartnerId,
|
||||
metaPixelId,
|
||||
tiktokPixelId,
|
||||
} = props
|
||||
|
||||
// Live consent state — drives the non-Google pixels.
|
||||
const { state } = useConsentContext()
|
||||
|
||||
// --- Google: load once, Consent Mode handles gating ---
|
||||
useEffect(() => {
|
||||
if (!ga4MeasurementId && !gtmContainerId) {return}
|
||||
|
||||
// 1. Consent Mode default = the visitor's stored choice, before tags load.
|
||||
setDefaultConsent(readConsent())
|
||||
|
||||
// 2. Load the tag.
|
||||
if (gtmContainerId) {
|
||||
window.dataLayer = window.dataLayer || []
|
||||
window.dataLayer.push({ event: 'gtm.js', 'gtm.start': Date.now() })
|
||||
injectScript(GTM_SCRIPT(gtmContainerId))
|
||||
} else if (ga4MeasurementId) {
|
||||
injectScript(GA4_SCRIPT(ga4MeasurementId))
|
||||
// Uses the shared gtag (pushes `arguments`, as Google's tags expect);
|
||||
// real gtag.js wires itself up once the script loads.
|
||||
gtag('js', new Date())
|
||||
gtag('config', ga4MeasurementId)
|
||||
}
|
||||
}, [ga4MeasurementId, gtmContainerId])
|
||||
|
||||
// --- Non-Google: load each when its consent category is granted ---
|
||||
// The loaders are idempotent, so re-running on state changes is safe; a tag
|
||||
// loads the first time its category flips to true and never doubles up.
|
||||
useEffect(() => {
|
||||
if (state.marketing) {
|
||||
if (metaPixelId) {loadMetaPixel(metaPixelId)}
|
||||
if (linkedInPartnerId) {loadLinkedIn(linkedInPartnerId)}
|
||||
if (tiktokPixelId) {loadTikTok(tiktokPixelId)}
|
||||
}
|
||||
if (state.analytics && clarityProjectId) {loadClarity(clarityProjectId)}
|
||||
}, [
|
||||
state.marketing,
|
||||
state.analytics,
|
||||
metaPixelId,
|
||||
linkedInPartnerId,
|
||||
tiktokPixelId,
|
||||
clarityProjectId,
|
||||
])
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user