Files
ipal-kit/src/modules/consent/vendorConsent.ts
T
Radosław SmolińskiandClaude Opus 5.5 99036eb70c Consent compliance: Global Privacy Control honoured, equal choice by default, withdrawal that stops running tools, consent log on by default
- GPC (gpc.ts): read in the browser before the first paint (consentBannerScript) and in the consent store; pages stay
  static. 'honour' (default): no banner, analytics and marketing off, no request to Google or a pixel; an explicit
  choice on the site wins; a grant made without the signal gives way when it appears. 'ask' and false as opt-outs.
- equalChoice defaults to true; a development warning when the banner has no 'reject'.
- Withdrawal: Meta / TikTok / Clarity told at once, listed localStorage / sessionStorage keys removed.
- Clarity gets consentv2 when it loads; ads_data_redaction with the Consent Mode default.
- consentLog on by default (consentLog: false turns it off); SiteProviders posts to it by itself; a missing SQL table
  answers 503 with one log line.
- Default banner text: one sentence on changing the choice and browser settings; the GPC line (PL / EN).
- Doctor: consent-log-migration, consent-banner rules.
- Stats: Sec-GPC over a choice made without it keeps the visitor id out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 13:38:39 +01:00

42 lines
2.0 KiB
TypeScript

import type { ConsentState } from './categories.js'
/* eslint-disable @typescript-eslint/no-explicit-any -- vendor globals (fbq, clarity, ttq) are untyped */
const granted = (on: boolean) => (on ? 'granted' : 'denied')
/**
* Tells the tools that are already running on this page about a change of consent, so a withdrawal stops them at
* once rather than at the next page load. Only tools that have a call for it, and only when they are loaded:
*
* - Microsoft Clarity (analytics): `clarity('consentv2', …)`; analytics off → `clarity('consent', false)`, which
* erases its cookies and stops it.
* - Meta Pixel (marketing): `fbq('consent', 'revoke' | 'grant')`.
* - TikTok Pixel (marketing): `ttq.revokeConsent()` / `ttq.grantConsent()`.
*
* Google tags get the Consent Mode update separately (googleConsent.ts). LinkedIn Insight has no such call: it stops
* with the next page load. Never throws.
*/
export function signalVendors(prev: ConsentState, next: ConsentState): void {
if (typeof window === 'undefined') {return}
const w = window as any
try {
if (prev.marketing !== next.marketing) {
if (typeof w.fbq === 'function') {w.fbq('consent', next.marketing ? 'grant' : 'revoke')}
const ttq = w.ttq
const call = next.marketing ? ttq?.grantConsent : ttq?.revokeConsent
if (typeof call === 'function') {call.call(ttq)}
}
if (typeof w.clarity === 'function' && (prev.analytics !== next.analytics || prev.marketing !== next.marketing)) {
if (next.analytics) {w.clarity('consentv2', clarityConsent(next))}
else {w.clarity('consent', false)}
}
} catch {
// a vendor's call must never break the banner
}
}
/** Clarity's consent API v2 for a state (`ad_Storage` follows marketing, `analytics_Storage` analytics). */
export function clarityConsent(state: Pick<ConsentState, 'analytics' | 'marketing'>): { ad_Storage: string; analytics_Storage: string } {
return { ad_Storage: granted(state.marketing), analytics_Storage: granted(state.analytics) }
}