Skip to main content

JavaScript events (v2)

If your site uses the v3 banner

This page describes the v2 banner. For the v3 banner, see Events.

This page lists the events that the v2 banner sends to the site and shows how to listen to them.

Who this is for​

This page is for you if you added to the site a listener on the v2 banner events, for example to activate a tool after consent.

Prerequisites​

Before you start, check that:

  • the site loads the v2 banner;
  • you can add a script to the page.

window events​

The v2 banner emits three CustomEvents on the window.

EventWhen it fires
avacy_consentOn page load (detail.trigger is page_load) and at every save (consent_update)
avacy_interactionAt every click on the banner's choice buttons
avacy_reject_allWhen the visitor rejects everything or closes the banner without choosing

The detail of avacy_reject_all has consent ('reject-all' or 'close-banner') and layer, which is always '1': the reject button and the X are only in the first layer.

PropertyContent
trigger'page_load' or 'consent_update'
optintrue if the visitor has already saved a choice, even a rejection. false on the first visit, before the choice
purposesThe ids of the allowed IAB purposes
customPurposesThe custom purposes, each with id and the flags c (consent) and li (legitimate interest)
legitimateInterestsThe ids of the IAB purposes with legitimate interest
specialFeaturesThe ids of the allowed IAB special features
customVendorsThe custom vendors of the web space, each with id and consent
iabVendorsThe ids of the IAB vendors with consent
iabVendorsWithConsentThe IAB vendors with consent and with all purposes allowed, each with id and name
customVendorsWithConsentThe custom vendors with all purposes allowed, each with id and name
gcmThe array of granted Google Consent Mode types, for example ['analytics_storage']

event.detail.optin values for avacy_interaction​

ValueWhen
consent-allThe visitor presses the accept button
reject-allThe visitor presses the reject button
save-preferencesThe visitor saves the preferences from the panel
close-bannerThe visitor closes the banner without choosing

Listen to an event​

The visitor saved a choice. optin is true even after a rejection. To know what they allowed, read purposes and the vendors.

<script>
window.addEventListener('avacy_consent', function (event) {
var data = event.detail;
if (!data.optin) {
console.log("The visitor has not chosen yet.");
return;
}
console.log("Allowed IAB purposes: " + data.purposes.join(', '));
});
</script>

Button pressed.

<script>
window.addEventListener('avacy_interaction', function (event) {
if (event.detail.optin === 'consent-all') {
console.log("The visitor accepted from the button.");
} else if (event.detail.optin === 'save-preferences') {
console.log("The visitor saved the preferences.");
}
});
</script>

Custom vendors and their status.

<script>
window.addEventListener('avacy_consent', function (event) {
var vendors = event.detail.customVendors || [];
vendors.forEach(function (vendor) {
console.log('id: ' + vendor.id + ' consent: ' + vendor.consent);
});
});
</script>

Consent to Google (vendor 755 of the IAB TCF list), only on save (consent_update).

<script>
window.addEventListener('avacy_consent', function (event) {
var data = event.detail;
if (data.trigger !== 'consent_update') return;
var vendorId = 755;
var consented = data.iabVendorsWithConsent.some(function (vendor) {
return vendor.id === vendorId;
});
console.log('Consent to vendor ' + vendorId + ': ' + consented);
});
</script>

Consent to one or more purposes.

<script>
window.addEventListener('avacy_consent', function (event) {
var purposes = event.detail.purposes;
var required = [1, 3];
var allowed = required.every(function (id) {
return purposes.indexOf(id) !== -1;
});
console.log('Consent to purposes ' + required.join(', ') + ': ' + allowed);
});
</script>

postMessage events​

The v2 banner also sends messages with window.postMessage. Each message is a string with the event name.

EventWhen it fires
oil-checked-optinThe banner found a saved consent that is still valid
oil_shownThe banner was shown
oil_optin_done_button_clickedThe visitor pressed an accept button
oil_optin_doneThe consent was saved
oil_soi_optin_doneThe consent was saved in the site cookie
oil_poi_optin_doneThe consent was also saved for Power Opt-In
oil_optout_doneThe banner deleted the saved consent (opt-out)
oil_close_banner_button_clickedThe visitor closed the banner
oil_as_cpc_privacy_selectedThe visitor saved a choice other than Accept all, for example from the panel or with the X
oil_click_advanced_settingsThe visitor opened the advanced settings

postMessage event.

<script>
window.addEventListener('message', function (event) {
if (event.data === 'oil_shown') {
console.log('The banner was shown.');
}
});
</script>

Event history​

AVACY.eventCollection lists the postMessage events already sent. AVACY.isInCollection(name) returns how many times the event is in the list, or 0 if it is missing.

console.log(AVACY.eventCollection);
// [ { name: 'oil-checked-optin', timestamp: 1735816751078 }, … ]

if (AVACY.isInCollection('oil_optin_done') > 0) {
console.log("The oil_optin_done event has already been sent.");
}

Verify​

  1. Add one of the listeners to the page, reload and open the browser console.
  2. Choose an option on the banner.
  3. Check that the listener writes the message to the console.

Common problems​

The most frequent issues:

  • The listener on consent_update never fires: consent_update is not an event. Listen to avacy_consent and check event.detail.trigger;
  • The listener on avacy_consent fires at every page load: the v2 banner also emits the event with trigger equal to page_load. Filter on event.detail.trigger;
  • The console writes eventCollection is not defined: eventCollection is a property of AVACY. Use AVACY.eventCollection;
  • After the switch to the v3 banner some listeners do not fire: the v3 banner still emits avacy_consent, but not avacy_interaction, avacy_reject_all or the postMessage events. See Backward compatibility with the v2 banner.

Last updated: 09/11/2026.