# Examples

### Read TC Data

{% hint style="info" %}
Before loading partners scripts on your site, you must verify that user has given consent or hasn't objected to the legitimate interest.
{% endhint %}

With the `addEventListener` command, you can register a listener to the CMP that will be called with the TC Data whenever an event occurs. When adding a listener, the callback is called immediately with the current TC Data as well as a listenerId that you must store if you need to remove the listener.

{% tabs %}
{% tab title="Read TC Data" %}

```javascript
window.__tcfapi("addEventListener", 2, function (tcData, success) {
 if (success && tcData && (tcData.eventStatus == "cmpuiclosed" || tcData.eventStatus == "tcloaded")) {
      if (!tcData.gdprApplies) {
                        console.log("GDPR doesn't apply to user");
      } else {
        if (tcData.vendor.consents[53] && tcData.purpose.consents[1]) {
                                console.log("Vendor ID 53 has consent for  purpose ID 1");
             }
        if (!tcData.vendor.consents[53] || !tcData.purpose.consents[2]) {
                                console.log("Vendor ID 53 has no consent for purpose ID 2");
             }
        if (tcData.vendor.legitimateInterests[53] && tcData.purpose.legitimateInterests[9]) {
                                console.log("User has been informed of vendor ID 53's legitimate interest for purpose ID 9 and hasn't objected to it");
             }
         if (!tcData.vendor.legitimateInterests[53] || !tcData.purpose.legitimateInterests[10]) {
                                console.log("User has objected to vendor ID 53's legitimate interest for purpose ID 10");
             }
         }
       }
});
```

{% endtab %}
{% endtabs %}

You can specify a list of vendor IDs for which the TC Data is requested.

{% tabs %}
{% tab title="Read TC Data filtered with a list of vendor IDs" %}

```javascript
window.__tcfapi("addEventListener", 2, function (tcData, success) {
 if (success && tcData && (tcData.eventStatus == "cmpuiclosed" || tcData.eventStatus == "tcloaded")) {
      if (!tcData.gdprApplies) {
                        console.log("GDPR doesn't apply to user");
       } else {
         if (tcData.vendor.consents[53] && tcData.purpose.consents[1]) {
                                console.log('Vendor ID 53 has consent for
                                 purpose ID 1');
          }
          if (tcData.vendor.consents[100] && tcData.purpose.consents[1]) {
                   // This will never happen since vendor ID 100 is not
                   // included in the vendorIDs parameter
                                console.log('Vendor ID 53 has consent for
                                 purpose ID 1');
           }
         }
       }
}, [4, 10, 53]);
```

{% endtab %}
{% endtabs %}

### Read the Global Vendor List

The Global Vendor List containing the purposes and vendors participating in the Transparency & Consent Framework can be retrieved with the `getVendorList` command.

{% tabs %}
{% tab title="Read the Global Vendor List" %}

```javascript
window.__tcfapi("getVendorList", 2, function (globalVendorList, success) {
 if (success) {
  console.log("vendorListVersion = " + globalVendorList.vendorListVersion);
 }
});
```

{% endtab %}
{% endtabs %}

You can specify a *vendorListVersion* for which the Global Vendor List is requested.

{% tabs %}
{% tab title="Read the Global Vendor List for a specific version" %}

```javascript
window.__tcfapi("getVendorList", 2, function (globalVendorList, success) {
           if (success) {
               console.log("globalVendorList with version 10 is retrieved");
      } else {
                console.log("Invalid vendorListVersion");
        }
}, 10);
```

{% endtab %}
{% endtabs %}

### Revoke Utiq Consent

If the user has revoked their consent for Utiq outside of the CMP, you can set the choice status via the CMP API using the following command:

{% tabs %}
{% tab title="Add a listener" %}

```javascript
window.__sdcmpapi("revokeUtiq", 2, function (success) {
    if (success) {
        console.log("Consent for Uti is revoked");
    }
});
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cmp.docs.sirdata.net/en/cmp-api/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
