Skip to main content

Consent Query

Lettura dello stato del consenso a runtime.

MethodTierSince
getConsent()13.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 a hasLegitimateInterest
window.Avacy.getConsent(): Consent | null

Ritorna null quando il CMP non è ancora inizializzato del tutto — un caso distinto da { ready: false }, che invece significa "inizializzato, ma il consenso non ha ancora risolto". Un null-check va fatto PRIMA di controllare .ready.

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: Record<string, Record<string, boolean>>; // framework → id → hasConsent
specialFeatures: Record<string, Record<string, boolean>>; // solo TCF
vendors: Record<string, Record<string, boolean>>;

// vista array — per iterazione, server sync, hasLegitimateInterest
choices: PrivacyChoice[];
};
  • purposes, specialFeatures, vendors e choices ci sono sempre, anche negli stati non validi: con ready: false o valid: false valgono {}, {}, {} e [].
  • purposes contiene solo le finalità (e le finalità dei fornitori custom, sotto custom). Le special feature TCF stanno in specialFeatures.tcf: hanno gli stessi id 1 e 2 delle finalità 1 e 2, quindi in una mappa sola si sovrascriverebbero.
  • purposes[fw][id], specialFeatures[fw][id] e vendors[fw][id] riportano hasConsent. Le chiavi sono stringhe: l'id numerico di TCF/GCM/ACM si legge indifferentemente come [755] o ['755'], quello dei fornitori custom è testuale (vendors.custom?.['d116']). Per hasLegitimateInterest fai fall-back su choices (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) return; // CMP non ancora inizializzato
if (!c.ready) return; // consenso non ancora risolto
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 || !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();

// special feature TCF (vista separata: gli id 1 e 2 non sono le finalità 1 e 2):
if (c.specialFeatures.tcf?.[1]) useGeolocation();

// GCM (Google Consent Mode) — purpose 4 = analytics_storage:
if (c.purposes.gcm?.[4]) initAnalyticsStorage();

// Fornitore custom (id testuale):
if (c.vendors.custom?.['d116']) loadMyVendorPixel();

Example: vista array (iterazione, server sync, LI)​

const c = window.Avacy.getConsent();
if (!c || !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 || !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​