Consent Query
Lettura dello stato del consenso a runtime.
| Method | Tier | Since |
|---|---|---|
getConsent() | 1 | 1.0 |
π‘ Per scrivere consenso vedi Consent actions. Per uno snapshot completo del CMP (config + language + UI state + glossary) vedi
getData().
getConsentβ
Ritorna lo stato del consenso. Un'unica chiamata che espone tre viste dello stesso dato β usa quella piΓΉ ergonomica per il tuo caso d'uso:
- macro (
type,timestamp): gating "ha accettato tutto?" / "ha rifiutato?" - mappa (
purposes,vendors): gating veloce su purpose/vendor specifico β il caso d'uso piΓΉ comune (gating script analytics, social embed) - array (
choices): iterazione completa, server sync, accesso ahasLegitimateInterest
window.Avacy.getConsent(): Consent
Return shapeβ
type Consent =
| { ready: false }
| { ready: true; valid: false }
| {
ready: true;
valid: true;
type: 'AcceptAll' | 'RejectAll' | 'CloseBanner' | 'Personalized';
timestamp: number; // ms epoch
// vista mappa β per gating veloce
purposes: { [framework: string]: { [purposeId: number]: boolean } };
vendors: { [framework: string]: { [vendorId: number]: boolean } };
// vista array β per iterazione, server sync, hasLegitimateInterest
choices: PrivacyChoice[];
};
purposes[fw][id]evendors[fw][id]riportanohasConsent. PerhasLegitimateInterestfai fall-back suchoices(vedi esempio sotto).- Se un framework non Γ¨ configurato, la chiave non esiste (non c'Γ¨
tcf: {}vuoto). PrivacyChoiceβ vedi Tier 2.
Example: macroβ
const c = window.Avacy.getConsent();
if (!c.ready) return; // CMP non inizializzato
if (!c.valid) return; // utente non ha consentito
if (c.type === 'AcceptAll') trackOnce('cmp.accept_all');
Example: vista mappa (gating veloce)β
Il caso piΓΉ comune. Pochissimo boilerplate.
const c = window.Avacy.getConsent();
if (!c.ready || !c.valid) return;
// gating per TCF purpose:
if (c.purposes.tcf?.[1]) initAnalytics();
if (c.purposes.tcf?.[3]) initPersonalization();
// gating per TCF vendor:
if (c.vendors.tcf?.[79]) loadMetaPixel();
if (c.vendors.tcf?.[755]) loadGoogleAds();
// GCM (Google Consent Mode):
if (c.purposes.gcm?.[2]) initAnalyticsStorage();
// Custom framework:
if (c.purposes.custom?.[1]) loadMyVendorPixel();
Example: vista array (iterazione, server sync, LI)β
const c = window.Avacy.getConsent();
if (!c.ready || !c.valid) return;
// server sync (POST tutto il dettaglio al backend):
fetch('/api/consent', {
method: 'POST',
body: JSON.stringify({ type: c.type, timestamp: c.timestamp, choices: c.choices }),
});
// gating con legitimate interest (non disponibile nelle viste mappa):
const meta = c.choices.find(
(x) => x.framework === 'tcf' && x.type === 'vendor' && x.id === 79
);
if (meta?.hasConsent || meta?.hasLegitimateInterest) loadMetaPixel();
When to use itβ
- Gating script analytics, marketing pixel, social embed β vista mappa
- Conditional render UI sensibile al consenso β vista macro + mappa
- Bridge native (vedi Events β bridge native WebView)
- Sync server-side del dettaglio consensi β vista array
Reactive: combina con avacy:consent-savedβ
function applyConsent() {
const c = window.Avacy.getConsent();
if (!c.ready || !c.valid) return;
if (c.purposes.tcf?.[1]) initAnalytics();
if (c.vendors.tcf?.[79]) loadMetaPixel();
}
// 1. al boot
document.addEventListener('avacy:ready', applyConsent);
// 2. quando l'utente salva
document.addEventListener('avacy:consent-saved', applyConsent);
See alsoβ
avacy:consent-savedβ evento DOM emesso a ogni savePrivacyChoiceβ shape dell'elementochoicesgetData()β snapshot completo (oltre al solo consent)