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)