Skip to main content

Reading the consent

The banner's window.Avacy.getConsent() function returns the visitor's choice in three views of the same data.

v2 banner

The v2 banner does not provide window.Avacy.getConsent(). See JavaScript events (v2). If you don't know which banner your site uses, read Which banner your site is using.

Prerequisites​

Before you start, check that:

  • the banner is installed on the page. See HTML snippet;
  • your code runs after the banner has loaded. To find out when, see Banner events.

What getConsent returns​

window.Avacy.getConsent(): Consent | null

The function returns null until the banner has finished starting up. After startup it returns an object in one of these three states.

StateWhen
{ ready: false }The banner has started and has not yet read the saved consent.
{ ready: true, valid: false }The banner has read the consent and the visitor has no usable one.
{ ready: true, valid: true, type, timestamp, choices }The visitor has a saved, valid consent.

The valid state also contains these fields.

FieldContent
typeType of saved choice: 'AcceptAll', 'RejectAll' or 'Personalized'.
timestampTime of the choice, in milliseconds since 1970.
consentIdIdentifier of the consent in the consent archive. It appears only when the banner's consent archiving is on, after the archive has recorded the choice. To turn it on, see Consent archive (in Italian).

The three views​

ViewFieldsWhen to use it
Macrotype, timestampFind out whether the visitor accepted everything, rejected everything or chose manually.
Mappurposes, specialFeatures, vendorsDecide on a specific purpose or vendor.
ListchoicesGo through all the choices.

The purposes, specialFeatures, vendors and choices fields are always present. In the states without consent they are {}, {}, {} and [].

Map view​

Each map has the shape map[framework][id] = true | false. The framework is tcf, gcm, acm or custom. A framework that is not configured has no key.

The content of the three maps:

  • purposes contains the purposes and the purposes of custom vendors;
  • specialFeatures contains the TCF special features. They have the same ids 1 and 2 as purposes 1 and 2, so they live in a separate map;
  • vendors contains the vendors. The id of TCF vendors is a number. The id of custom vendors is a string, for example 'd116'. The dashboard shows the id in the ID column of the vendor list.

Map keys are strings: purposes.tcf[1] and purposes.tcf['1'] give the same result.

Google Consent Mode purposes have these ids.

IdGoogle permission
1ad_storage
2ad_user_data
3ad_personalization
4analytics_storage
5functionality_storage
6personalization_storage
7security_storage

List view​

Each item in choices has this shape.

{ framework: 'tcf' | 'gcm' | 'acm' | 'custom', type: string, id: number | string, hasConsent?: boolean, hasLegitimateInterest?: boolean }

The list also contains information-only items, for example the TCF special purposes (type: 'specialPurpose'). The visitor cannot choose them, so don't use them to decide whether to start a script.

The valid: false state means the visitor has no usable consent, not that they rejected. The banner returns it in these cases:

  • the visitor has not chosen yet;
  • the saved consent has expired;
  • the saved consent belongs to a previous version of the policy;
  • the banner cannot read the saved value.

A visitor who rejects everything has a valid consent: valid: true, type: 'RejectAll' and all maps set to false.

With valid: false, don't start the scripts that require consent and don't treat the case as a rejection.

Examples​

The examples work when pasted into the browser console, with the banner loaded.

function describeConsent() {
var consent = window.Avacy.getConsent();

if (!consent) return 'banner not started yet';
if (!consent.ready) return 'consent not read yet';
if (!consent.valid) return 'no valid consent (missing, expired or to be renewed)';
return 'saved choice: ' + consent.type + ', ' + new Date(consent.timestamp).toISOString();
}

console.log(describeConsent());

Deciding on a purpose or a vendor​

function hasMarketingConsent() {
var consent = window.Avacy.getConsent();
if (!consent || !consent.ready || !consent.valid) return false;

// Google Consent Mode, purpose 1 (ad_storage)
var googleConsent = !!(consent.purposes.gcm && consent.purposes.gcm[1]);

// TCF, purpose 1 and vendor 52
var tcfConsent = !!(
consent.purposes.tcf && consent.purposes.tcf[1] &&
consent.vendors.tcf && consent.vendors.tcf[52]
);

return googleConsent || tcfConsent;
}

console.log(hasMarketingConsent());

The numbers 1 and 52 are placeholders. Replace them with the ids of your web space. TCF vendor ids are those of the IAB Europe Global Vendor List. The dashboard shows them in the ID column of the vendor list. Google Consent Mode ids are in the table above.

Going through all the choices​

function listChoices() {
var consent = window.Avacy.getConsent();
if (!consent || !consent.ready || !consent.valid) return [];

return consent.choices.map(function (choice) {
return choice.framework + ':' + choice.type + ':' + choice.id + ' = ' + choice.hasConsent;
});
}

console.log(listChoices());

Check​

  1. Open a page with the banner without choosing. window.Avacy.getConsent() returns ready: true and valid: false.
  2. Click Reject all, if the banner configuration shows it. getConsent() returns valid: true and type: 'RejectAll'.
  3. Reload the page. The response stays the same, because the banner reads the saved consent again.

Common problems​

The most frequent problems:

  • getConsent() returns null: the banner has not finished starting up. Wait for the avacy:ready event. If window.Avacy does not exist, the snippet is missing from the page or the bundle has not loaded yet;
  • getConsent() returns ready: false: the banner has not loaded the saved consent yet. Use the avacy:consent-loaded event;
  • A purpose or a vendor is undefined: the framework or the id is not in the banner configuration. Check with Object.keys(consent.purposes);
  • A TCF special feature does not appear in purposes: special features live in specialFeatures;
  • consentId is missing even with archiving on: the archive accepts only choices that come from the web space's domains. Locally, or on a test domain that is not among the web space's domains, the choice is not archived.

Last updated: 27/10/2026.