LibreChat/client/src/hooks/Config/useAnalytics.ts
Marco Beretta 74e7252c3e
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
📊 feat: Let Deployments Skip the Analytics Consent Prompt (#17552)
* feat: Let Deployments Skip the Analytics Consent Prompt

* test: Cover the consent-free analytics setting
2026-10-29 08:55:00 +01:00

112 lines
4.5 KiB
TypeScript

import { useEffect } from 'react';
import { useAtom } from 'jotai';
import TagManager from 'react-gtm-module';
import { analyticsConsentAtom, hasAnalyticsGtmId } from '~/store/analytics';
type ConsentState = 'granted' | 'denied';
/**
* Sends a Google consent mode command (`gtag('consent', ...)`) to the data layer.
* The viewer answers one question, analytics, so advertising storage and ad data
* use stay denied whatever the answer. A loaded container cannot be unloaded, so
* after a withdrawal tags that ignore consent mode keep running until the page
* reloads; the reload is what completes the revocation.
*/
function pushConsent(command: 'default' | 'update', analytics: ConsentState) {
window.dataLayer = window.dataLayer ?? [];
const dataLayer = window.dataLayer;
/** GTM reads consent commands only in the `arguments` shape gtag pushes, not as a plain array. */
function gtag(..._args: unknown[]) {
// eslint-disable-next-line prefer-rest-params -- GTM requires the Arguments object itself
dataLayer.push(arguments);
}
gtag('consent', command, {
analytics_storage: analytics,
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
});
}
/** The `gtm.js` script of this deployment's container, while it loads or after. */
function findContainerScript(gtmId: string): HTMLScriptElement | undefined {
return Array.from(document.querySelectorAll<HTMLScriptElement>('script[src*="gtm.js"]')).find(
(script) => {
try {
return new URL(script.src, window.location.href).searchParams.get('id') === gtmId;
} catch {
return false;
}
},
);
}
/** Whether this deployment's container is on the page, loading or loaded. GTM
* registers each container it runs under its id, so another container does not
* count. A script that failed to load is removed (see `initializeContainer`). */
function isContainerPresent(gtmId: string) {
const registry = window.google_tag_manager as Record<string, unknown> | undefined;
return registry?.[gtmId] != null || findContainerScript(gtmId) != null;
}
/** Loads the container with consent defaults queued ahead of its first tag. A
* script that fails to load is removed together with the `gtm.js` start event
* the snippet queued for it, so the next grant retries without a container
* that loads later replaying two start events. */
function initializeContainer(gtmId: string) {
/** The snippet keeps an existing data layer, so the defaults stay first in it. */
pushConsent('default', 'granted');
const queued = window.dataLayer?.length ?? 0;
TagManager.initialize({ gtmId });
const startEvent = window.dataLayer
?.slice(queued)
.find((entry) => (entry as { event?: unknown } | null)?.event === 'gtm.js');
const script = findContainerScript(gtmId);
script?.addEventListener(
'error',
() => {
script.remove();
const index = window.dataLayer?.indexOf(startEvent) ?? -1;
if (startEvent != null && index !== -1) {
window.dataLayer?.splice(index, 1);
}
},
{ once: true },
);
}
/**
* The only place Google Tag Manager is initialized. It loads when the
* deployment set `ANALYTICS_GTM_ID` and the viewer granted consent, never before.
* With `consentRequired` false (`interface.analyticsConsent: false`) the
* deployment has granted it for every viewer and the stored answer is ignored.
* Mount it once per page shell (the signed-in layout and the shared-link view).
*/
export default function useAnalytics(gtmId: string | null | undefined, consentRequired = true) {
const [storedConsent, setConsent] = useAtom(analyticsConsentAtom);
const enabled = hasAnalyticsGtmId(gtmId);
const consent = consentRequired ? storedConsent : 'granted';
useEffect(() => {
if (!enabled || consent !== 'granted') {
return;
}
if (isContainerPresent(gtmId)) {
/** Consent given again after a withdrawal in this page, or a second shell
* mounting: the container is already here and only needs the answer. */
pushConsent('update', 'granted');
return;
}
initializeContainer(gtmId);
}, [enabled, gtmId, consent]);
useEffect(() => {
/** Nothing is pushed unless this deployment's container is on the page; one
* still loading reads the update from the data layer queue when it starts. */
if (enabled && consent === 'denied' && isContainerPresent(gtmId)) {
pushConsent('update', 'denied');
}
}, [enabled, gtmId, consent]);
return { enabled, consent, setConsent };
}