JavaScript Consent Events: A Developer’s Guide
Last updated: September 7, 2026
This guide is for people who write code: frontend developers, backend developers who handle tracking, anyone building custom integrations for a client.
If you have the Statistics feature of My Agile Privacy enabled and you use Google Analytics 4, consent changes already reach GA4: you do not need to write a single line of code, just enable the feature. The JavaScript events described here are for everything else. Synchronizing your data layer, activating proprietary modules, measuring how users interact with the banner: everything the standard configuration does not cover.
Consent, by definition, is stored inside the Consent Management Platform. With these events you can propagate it wherever you need it.
The problem: consent happens inside the cookie banner
You have an internal analytics module. A third-party script that does not go through automatic blocking. An interface that must adapt when the user refuses.
Your code needs to know, at the exact moment it happens, that the user has accepted, rejected or customized consent. But that choice takes place inside the My Agile Privacy banner, and your JavaScript, outside, knows nothing about it.
Waiting for the page to reload is not enough: consent changes without reloading. Reading cookies from your script means querying a state, not reacting to an event.
Consent is not a state to query whenever you need it. It is an event to listen to when it happens.
Why you should not improvise a hack
The artisanal route exists: a MutationObserver on the banner, periodic polling of cookies, an observer on the button’s attributes.
It works until it does not. Then the widget gets redesigned, a CSS class changes name, the markup moves, and your observer stops seeing something that keeps happening. No error in the console: just data that stops arriving.
And above all: a DOM hack cannot tell “Accept all” from a toggle on a single service. It does not know the IAB panel exists, or reconsent, or the two banner layers. You lose exactly the granularity you needed.
If you are polling a cookie, you are building your next bug.
The solution: documented events fired on the window
My Agile Privacy emits a JavaScript event for every consent action, as a CustomEvent fired on the window. These are public events, with a stable, documented name: you hook them with window.addEventListener() and your code reacts at the exact moment, with the right granularity.
No polling, no DOM observer, no dependencies on the banner markup. Just a contract: the user makes a choice, MAP fires the event, your listener runs.
How the events work
Base pattern:
window.addEventListener('event_name', function() {
// your logic here
});
Timing rule: register the listener before the event fires. The safe spot is a <script> tag in the <head> or the top of your main JavaScript file. A listener registered at the bottom of the page may miss the events of the initial loading phase.
The complete event map
| Event | When it fires | Typical use |
|---|---|---|
myagileprivacy_gcmode_consent_granted |
Consent granted in Google Consent Mode | Enabling analytics, pushing to the data layer |
myagileprivacy_gcmode_consent_denied |
Consent denied in Google Consent Mode | Stopping custom tracking |
myagileprivacy_cookie_accept |
Click on “Accept all” in the banner | Activating your modules, GTM triggers |
myagileprivacy_cookie_reject |
Click on “Reject all” in the banner | Logging the refusal, updating the UI |
myagileprivacy_cookie_customize |
The personalization panel opens | Measuring engagement with the banner |
myagileprivacy_resurface |
The banner reappears after a click on a reconsent widget | Suspending tracking while waiting for the new consent |
myagileprivacy_single_cookie_accept |
Toggle enabled for a single service | Unlocking proprietary modules tied to that service |
myagileprivacy_single_cookie_reject |
Toggle disabled for a single service | Stopping modules and removing related data |
myagileprivacy_iab_item_accept |
Consent to a single IAB vendor/purpose | Updating your TCF logic |
myagileprivacy_iab_item_reject |
Refusal of a single IAB vendor/purpose | Blocking the corresponding vendor |
myagileprivacy_iab_accept_all |
“Accept all” in the IAB panel | Activating the entire custom advertising chain |
myagileprivacy_iab_reject_all |
“Reject all” in the IAB panel | Stopping the entire chain |

Global events (accept all / reject all)
myagileprivacy_gcmode_consent_granted
Fires when the user grants consent through Google Consent Mode. It triggers after “Accept all” or after a customization that results in at least one granted purpose.
window.addEventListener('myagileprivacy_gcmode_consent_granted', function() {
console.log('GCM - Consent GRANTED');
// Example: enable analytics, push to the dataLayer
});
myagileprivacy_gcmode_consent_denied
Fires when the user denies consent through Google Consent Mode. It triggers after “Reject all” or after a customization that results in all purposes denied.
window.addEventListener('myagileprivacy_gcmode_consent_denied', function() {
console.log('GCM - Consent DENIED');
// Example: disable tracking, delete non-technical cookies
});
myagileprivacy_cookie_accept
Fires when the user clicks the “Accept all” button in the banner. It reflects the explicit action on the interface, regardless of the GCM state. All configured cookies/services are activated.
window.addEventListener('myagileprivacy_cookie_accept', function() {
console.log('Banner - ACCEPT ALL');
// Example: activate all scripts, fire GTM triggers
});
myagileprivacy_cookie_reject
Fires when the user clicks the “Reject all” button in the banner. It reflects the explicit action on the interface, regardless of the GCM state. All configured cookies/services are deactivated.
window.addEventListener('myagileprivacy_cookie_reject', function() {
console.log('Banner - REJECT ALL');
// Example: log the refusal, update the UI
});
myagileprivacy_cookie_customize
Fires when the user opens the consent personalization panel, before confirming a definitive choice. It is the event that measures engagement with the interface: how many people actually want to choose.
window.addEventListener('myagileprivacy_cookie_customize', function() {
console.log('Banner - CUSTOMIZE opened');
// Example: start the deferred loading of the IAB vendor list
});
myagileprivacy_resurface
Fires when the banner is shown again following a click on one of the reconsent widgets. It is the moment when consent already given is back on the table: your custom code should behave accordingly.
window.addEventListener('myagileprivacy_resurface', function() {
console.log('Banner - RESURFACED');
// Example: suspend analytics until the new consent
});
Granular events (single cookie/service)
myagileprivacy_single_cookie_accept
Fires when the user enables consent for a single third-party cookie/service from the personalization panel (second layer). Each toggle corresponds to a specific service: Google Analytics, Facebook Pixel and so on. It is not emitted by “Accept all”: it only covers individual toggles.
window.addEventListener('myagileprivacy_single_cookie_accept', function() {
console.log('Preferences - Cookie/service ENABLED');
// Example: unlock the script of the service just accepted
});
myagileprivacy_single_cookie_reject
Fires when the user disables consent for a single third-party cookie/service from the personalization panel (second layer). It is not emitted by “Reject all”: it only covers individual toggles.
window.addEventListener('myagileprivacy_single_cookie_reject', function() {
console.log('Preferences - Cookie/service DISABLED');
// Example: remove the cookies of the service just rejected
});
IAB TCF events (vendors and purposes)
myagileprivacy_iab_item_accept
Fires when the user grants consent to a single IAB TCF vendor or purpose from the personalization panel (IAB tab). It only covers individual toggles: for the overall button there is myagileprivacy_iab_accept_all.
window.addEventListener('myagileprivacy_iab_item_accept', function() {
console.log('IAB TCF - Item ACCEPTED');
// Example: update the TC String, notify the CMP framework
});
myagileprivacy_iab_item_reject
Fires when the user denies consent to a single IAB TCF vendor or purpose from the personalization panel (IAB tab). It only covers individual toggles: for the overall button there is myagileprivacy_iab_reject_all.
window.addEventListener('myagileprivacy_iab_item_reject', function() {
console.log('IAB TCF - Item REJECTED');
// Example: update the TC String, block the corresponding vendor
});
myagileprivacy_iab_accept_all
Fires when the user presses “Accept all” inside the advertising preferences panel (IAB tab). It only covers the overall button: individual toggles emit myagileprivacy_iab_item_accept.
window.addEventListener('myagileprivacy_iab_accept_all', function() {
console.log('IAB TCF - Accept all');
});
myagileprivacy_iab_reject_all
Fires when the user presses “Reject all” inside the advertising preferences panel (IAB tab). It only covers the overall button: individual toggles emit myagileprivacy_iab_item_reject.
window.addEventListener('myagileprivacy_iab_reject_all', function() {
console.log('IAB TCF - Reject all');
});
Global binding: all events in one shot
When you need several events, or you want a single listening point for debugging and centralized logging, you can chain them all:
const MAP_EVENTS = [
'myagileprivacy_gcmode_consent_granted',
'myagileprivacy_gcmode_consent_denied',
'myagileprivacy_cookie_accept',
'myagileprivacy_cookie_reject',
'myagileprivacy_cookie_customize',
'myagileprivacy_resurface',
'myagileprivacy_single_cookie_accept',
'myagileprivacy_single_cookie_reject',
'myagileprivacy_iab_item_accept',
'myagileprivacy_iab_item_reject',
'myagileprivacy_iab_accept_all',
'myagileprivacy_iab_reject_all',
];
MAP_EVENTS.forEach(function(eventName) {
window.addEventListener(eventName, function() {
console.log('[MAP Event]', eventName);
// Your centralized logic here
});
});
Two concrete use cases
1. Push to the data layer for Google Tag Manager
You turn every consent action into a dataLayer event, so in GTM you can create triggers, variables and custom dimensions. GA4 with the Statistics feature is already covered: this pattern is for the other tools connected to GTM.
window.dataLayer = window.dataLayer || [];
['cookie_accept', 'cookie_reject', 'cookie_customize', 'resurface'].forEach(function(name) {
window.addEventListener('myagileprivacy_' + name, function() {
window.dataLayer.push({
event: 'map_consent',
map_action: name,
});
});
});
In GTM all you need is a trigger on the custom event map_consent and a map_action variable to tell the four actions apart.
2. Pausing tracking when consent is back on the table
When the user reopens the banner through a reconsent widget, previously given consent can no longer be taken for granted: your custom tracking should stop until a new choice arrives. A few lines are enough.
(function() {
let awaitingConsent = false;
window.addEventListener('myagileprivacy_resurface', function() {
awaitingConsent = true;
});
window.addEventListener('myagileprivacy_cookie_accept', function() {
awaitingConsent = false;
});
window.addEventListener('myagileprivacy_gcmode_consent_granted', function() {
awaitingConsent = false;
});
// Your custom send checks the flag before firing
function sendEvent(payload) {
if (awaitingConsent) return;
// ...your sending logic
}
})();
Things to watch
- Register the listener early. Best in the
<head>or at the top of your main script: events can fire right away, and a late listener misses the first ones. - Tell the levels apart. “Accept all” does not emit the
single_cookie_*events: they are two different channels, telling different actions. If you count consents, do not add up the wrong things. - Reconsent is an event of its own. After
myagileprivacy_resurfaceprevious consent cannot be taken for granted: wait for the new choice before reactivating anything.
When consent speaks, your code listens
The banner asks, the user answers, My Agile Privacy translates. This API is the meeting point between compliance and your code: a stable, documented contract that does not depend on the widget’s markup or styles.
If you need an integration these events do not cover, or you want advice on your tracking architecture, our developers (real people, not bots or artificial intelligences) are at your disposal: Contact us!

