JavaScript events (v2)
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.
| Event | When it fires |
|---|---|
avacy_consent | On page load (detail.trigger is page_load) and at every save (consent_update) |
avacy_interaction | At every click on the banner's choice buttons |
avacy_reject_all | When 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.
event.detail properties for avacy_consent
| Property | Content |
|---|---|
trigger | 'page_load' or 'consent_update' |
optin | true if the visitor has already saved a choice, even a rejection. false on the first visit, before the choice |
purposes | The ids of the allowed IAB purposes |
customPurposes | The custom purposes, each with id and the flags c (consent) and li (legitimate interest) |
legitimateInterests | The ids of the IAB purposes with legitimate interest |
specialFeatures | The ids of the allowed IAB special features |
customVendors | The custom vendors of the web space, each with id and consent |
iabVendors | The ids of the IAB vendors with consent |
iabVendorsWithConsent | The IAB vendors with consent and with all purposes allowed, each with id and name |
customVendorsWithConsent | The custom vendors with all purposes allowed, each with id and name |
gcm | The array of granted Google Consent Mode types, for example ['analytics_storage'] |
event.detail.optin values for avacy_interaction
| Value | When |
|---|---|
consent-all | The visitor presses the accept button |
reject-all | The visitor presses the reject button |
save-preferences | The visitor saves the preferences from the panel |
close-banner | The 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.
| Event | When it fires |
|---|---|
oil-checked-optin | The banner found a saved consent that is still valid |
oil_shown | The banner was shown |
oil_optin_done_button_clicked | The visitor pressed an accept button |
oil_optin_done | The consent was saved |
oil_soi_optin_done | The consent was saved in the site cookie |
oil_poi_optin_done | The consent was also saved for Power Opt-In |
oil_optout_done | The banner deleted the saved consent (opt-out) |
oil_close_banner_button_clicked | The visitor closed the banner |
oil_as_cpc_privacy_selected | The visitor saved a choice other than Accept all, for example from the panel or with the X |
oil_click_advanced_settings | The 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
- Add one of the listeners to the page, reload and open the browser console.
- Choose an option on the banner.
- Check that the listener writes the message to the console.
Common problems
The most frequent issues:
- The listener on
consent_updatenever fires:consent_updateis not an event. Listen toavacy_consentand checkevent.detail.trigger; - The listener on
avacy_consentfires at every page load: the v2 banner also emits the event withtriggerequal topage_load. Filter onevent.detail.trigger; - The console writes
eventCollection is not defined:eventCollectionis a property ofAVACY. UseAVACY.eventCollection; - After the switch to the v3 banner some listeners do not fire: the v3 banner still emits
avacy_consent, but notavacy_interaction,avacy_reject_allor thepostMessageevents. See Backward compatibility with the v2 banner.
Last updated: 09/11/2026.