JavaScript Events Guide

The OneTrust Cookies JavaScript SDK raises several JavaScript events to enable your application to respond to changes in a user’s consent.

📘

All of the events are raised on the window object.

OneTrustGroupsUpdated

Example

`window.addEventListener(“OneTrustGroupsUpdated”,event =>{
console.log(The following groups are now active: ${event.detail.join(“, “)})
});`

//Prints “The following groups are now active: C0001, C0002, C0003, C0004”

Timing

This event is called whenever consent is updated as well as on the initial load when consent becomes available. Before a user interacts with the banner or preference center, this event is raised with the default consent values.

Event properties

Property NameValueExample Value
detailsAn array of the IDs of all groups that are active, that is, either have consent or are in an Always Active state.[“C0001”,”C0002”,”C0003”]

OTConsentApplied

Example

Window.addEventListener(“OTConsentApplied”, event => {  
        console.log(“The user has updated their consent.”)  
        console.log(Reference OnetrustActiveGroups for updated consent = ${OnetrustActiveGroups})  
});

Timing

This event is only raised when a user manually changes their consent. The event will be raised for consent changes made via the banner or preference center, or programmatically through OneTrust.AcceptAll(), OneTrust.RejectAll(), or OneTrust.UpdateConsent().

Event properties

This event does not pass any relevant properties. You can use OnetrustActiveGroups in the event’s handler to view updated consent.


OneTrustPCLoaded

Example

window.addEventListener(“OneTrustPCLoaded”, event => {  
        console.log(“The preference center has been surfaced.”);  
});

 Timing

This event is raised when the preference center is initially opened. It is fired when the call to open the preference center is made and will be raised regardless of the status of the animation (i.e., the event does not wait for the animation to finish before being called).

Event properties

This event does not pass any relevant properties.


Other Tips

Actioning consent

Listeners are a great way to action consent in smaller pieces of code outside of the OptanonWrapper control. There are no dependencies in the event listeners, meaning you can set them at any time in your application’s lifecycle, regardless of whether or not OneTrust has already loaded.

The code below gives an example of starting and stopping a fictional myAnalyticsTool based on the user’s consent to category C0002.

Example

window.addEventListener(“OneTrustGroupsUpdated”, event => {  
        if(event.detail.includes(“C0002”) && !myAnalyticsTool.isOn){  
                myAnalyticsTool.startCapture();  
        }else{  
                myAnalyticsTool.stopCapture();  
        }  
});

Firing Listeners once

Event Listeners have several options that can be passed into addEventListener as a third parameter. One of the most useful ones is the once parameter, which disables the event listener after it has been fired.

Example

window.addEventListener(“OneTrustGroupsUpdated”, event => {  
        console.log(“Only fire this listener once.”)  
}, {once:true});