> For the complete documentation index, see [llms.txt](https://doc.commandersact.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.commandersact.com/features/consent-management/onsite-api/consent.onupdate.md).

# consent.onUpdate

Method to subscribe to Commanders Act consent status updates OnSite via JavaScript.

{% hint style="info" %}
Method to subscribe to Commanders Act consent updates OnSite via JavaScript.The Commanders Act [OnSite API stub](/features/consent-management/onsite-api/getting-started.md) has to be installed before using any of the OnSite API functions.
{% endhint %}

```javascript
cact('consent.onUpdate', function (result) { ... })
```

The `consent.onUpdate` method allow to subscribe a callback function for consent updates. The callback function will be called with the updated Consent Object. It is called whenever the consent is changed via a Commanders Act banner interaction or the `consent.update` method of the OnSite API.

The `consentObject` argument will also be enriched with an additional property `updateEvent` to signal how the update happened. It can have following values:

| Value     | Description                                                                               |
| --------- | ----------------------------------------------------------------------------------------- |
| `set`     | The consent was set.                                                                      |
| `changed` | Consent was already established and then changed.                                         |
| `revoked` | Consent was revoked by the user. Used for cleanup tasks like deleting cookie identifiers. |

{% hint style="info" %}
When consent is revoked it fires two events: One changed followed by one revoked.
{% endhint %}

## Examples

### Example to react to consent changes of a specific category

In this example the Analytics category was configured with the consent category id 2 in Commanders Act Consent settings.

```javascript
cact('consent.onUpdate', function (result) { 
    
    var ANALYTICS_ID = 2;
    var analyticsCategory = result.consent.categories[ANALYTICS_ID] || {};
     
    if (analyticsCategory.status === 'on') {
    
        // Consent was provided for the category. 
    
    } else {
        
        // Consent was not provided for the category. 
           
    }
    
});
```

### Example only react to consent changes of a specific category after the initial consent was given

In this example the Analytics category was configured with the consent category id 2 in TrustCommander.

```javascript
cact('consent.onUpdate', function (result) { 

    var ANALYTICS_ID = 2;
    var analyticsCategory = result.consent.categories[ANALYTICS_ID] || {};

    if (result.updateEvent === "changed" && analyticsCategory.status === 'on') {
    
        // Consent was provided for the category during an update.
    
    } 
    
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://doc.commandersact.com/features/consent-management/onsite-api/consent.onupdate.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
