Examples

Read TC Data

Before loading partners scripts on your site, you must verify that user has given consent or hasn't objected to the legitimate interest.

You can do this by reading signals with the getTCData command.

window.__tcfapi("getTCData", 2, function (tcData, success) {
 if (!success) {
        return
 }
 if (!tcData.gdprApplies) {
        console.log("GDPR doesn't apply to user");
        return
 }
 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");
 }
});

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

window.__tcfapi("getTCData", 2, function (tcData, success) {
   if (success) {
      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]);

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.

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

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

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

Add a listener

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.

window.__tcfapi("addEventListener", 2, function (tcData, success) {
    if (success) {
        console.log("Listener has been added with ID " + tcData.listenerId);
        console.log("Event status:" + tcData.eventStatus);
    }
});

Last updated