> ## Documentation Index
> Fetch the complete documentation index at: https://developer.onetrust.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Web CMP Events Guide

The OneTrust Web CMP solution has several JavaScript events to enable your website to respond to changes in a user’s consent.

> 📘
>
> All events are raised in the window object.

## OneTrustGroupsUpdated

This event is triggered each time the script loads, such as when the website is accessed or refreshed, and whenever the user updates their consent preferences.

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

**Event properties:**

| Property Name | Value                                                                                                                             | Example Value              |
| :------------ | :-------------------------------------------------------------------------------------------------------------------------------- | :------------------------- |
| `details`     | An array containing the IDs of all active groups, meaning those that either have user consent or are designated as Always Active. | \[“C0001”,”C0002”,”C0003”] |

## OTConsentApplied

This event is triggered when the user's consent status changes and the corresponding consent receipt call has been made. It is similar to the `OneTrust.OnConsentChanged` method, but occurs later due to its dependency on the consent receipt logging process.

```javascript
window.addEventListener("OTConsentApplied", event => {
    console.log("The user has updated their consent.");
    console.log(`New consent states: ${OnetrustActiveGroups}`);
});
```

## OnConsentChanged

This public method is triggered when the user's consent status changes. While similar to the `OTConsentApplied` event, it is invoked earlier because it does not depend on the consent receipt logging workflow.

```javascript
OneTrust.OnConsentChanged(function() {
  console.log('User consent updated.');
});
```

## OneTrustPCLoaded

This event is triggered when the preference center UI is surfaced. It occurs at the moment the call to open the preference center is made, without waiting for any associated animations to complete.

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

## Tips

### Actioning consent

Event listeners offer a flexible way to handle consent related logic in modular code outside the scope of the `OptanonWrapper`. Since they have no dependencies, listeners can be registered at any point in your application's lifecycle, regardless of whether OneTrust has already loaded.

The example below demonstrates how to start and stop a sample myAnalyticsTool based on the user's consent for the 'Performance' category (C0002).

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

### Firing Listeners Once

The `addEventListener` method accepts several options as its third parameter. One particularly useful option is `once`, which ensures the listener is automatically removed after it handles the event a single time.

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