> 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.get.md).

# consent.get

Method to receive Commanders Act consent and consent metadata OnSite via JavaScript.

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

`cact('consent.get', function (result) { ... });`

The `consent.get` method takes one callback JavaScript function as an argument that gets called with the [Consent Object](https://doc.commandersact.com/features/consent-management/knowledge-base/consent-object.md) that is currently stored on the browser. The callback is called once after Commanders Act CMP JavaScript loaded and validated the stored consent.

{% hint style="info" %}
The OnSite API works asynchronous. In case you need the consent synchronous (e.g. in the `<head>` of a document for AB Testing or personalisation solutions) it is recommended to cache the object in the `localStorage` of the browser. In this case it is crucial to implement the `consent.onUpdate` method to keep the cached consent in sync.
{% endhint %}

### Examples <a href="#examples" id="examples"></a>

#### Executing code based on the consent setting of a category <a href="#executing-code-based-on-the-consent-setting-of-a-category" id="executing-code-based-on-the-consent-setting-of-a-category"></a>

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

```
cact('consent.get', 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.
   
   }
        
});
```

#### Executing code based on the consent setting of a category and vendor <a href="#executing-code-based-on-the-consent-setting-of-a-category-and-vendor" id="executing-code-based-on-the-consent-setting-of-a-category-and-vendor"></a>

In this example the Analytics category was configured with the consent category id 2 and the vendor Google with vendor id 5 in Commanders Act CMP.

```
cact('consent.get', function (result) {
   
   var ANALYTICS_ID = 2;
   var analyticsCategory = result.consent.categories[ANALYTICS_ID] || {};

   var GOOGLE_ID = 5;
   var googleVendor = result.consent.vendors[GOOGLE_ID] || {};
   
   if (analyticsCategory.status === 'on' && googleVendor.status === 'on') {
         
      // Consent was provided for the category. 
      
   } else {
      
      // Consent was not provided for the category.
   
   }
        
});
```

#### Check if consent was already set by the visitor <a href="#check-if-consent-was-already-set-by-the-visitor" id="check-if-consent-was-already-set-by-the-visitor"></a>

```
cact('consent.get', function (result) {
   
   if (result.consent.status === 'unset') {
         
      // Consent was not yet provided.
      
   } else {
      
      // Consent was accepted or refused.
   
   }
        
});
```

#### Checking when the current consent expires <a href="#checking-when-the-current-consent-expires" id="checking-when-the-current-consent-expires"></a>

```
cact('consent.get', function (result) {

    var dateExpires = result.meta.dateExpires;
                
});
```


---

# 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.get.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.
