Skip to main content

Consent Query

Lettura dello stato del consenso a runtime.

MethodTierSince
getConsent()11.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

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] e vendors[fw][id] riportano hasConsent. 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.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​