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.
Read TC Data
1
window.__tcfapi("getTCData", 2, function (tcData, success) {
2
if (!success) {
3
return
4
}
5
if (!tcData.gdprApplies) {
6
console.log("GDPR doesn't apply to user");
7
return
8
}
9
if (tcData.vendor.consents[53] && tcData.purpose.consents[1]) {
10
console.log("Vendor ID 53 has consent for purpose ID 1");
11
}
12
if (!tcData.vendor.consents[53] || !tcData.purpose.consents[2]) {
13
console.log("Vendor ID 53 has no consent for purpose ID 2");
14
}
15
if (tcData.vendor.legitimateInterests[53] && tcData.purpose.legitimateInterests[9]) {
16
console.log("User has been informed of vendor ID 53's legitimate
17
interest for purpose ID 9 and hasn't objected to it");
18
}
19
if (!tcData.vendor.legitimateInterests[53] || !tcData.purpose.legitimateInterests[10]) {
20
console.log("User has objected to vendor ID 53's legitimate
21
interest for purpose ID 10");
22
}
23
});
Copied!
You can specify a list of vendor IDs for which the TC Data is requested.
Read TC Data filtered with a list of vendor IDs
1
window.__tcfapi("getTCData", 2, function (tcData, success) {
2
if (success) {
3
if (!tcData.gdprApplies) {
4
console.log("GDPR doesn't apply to user");
5
} else {
6
if (tcData.vendor.consents[53] && tcData.purpose.consents[1]) {
7
console.log('Vendor ID 53 has consent for
8
purpose ID 1');
9
}
10
if (tcData.vendor.consents[100] && tcData.purpose.consents[1]) {
11
// This will never happen since vendor ID 100 is not
12
// included in the vendorIDs parameter
13
console.log('Vendor ID 53 has consent for
14
purpose ID 1');
15
}
16
}
17
}
18
}, [4, 10, 53]);
Copied!

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.
Read the Global Vendor List
1
window.__tcfapi("getVendorList", 2, function (globalVendorList, success) {
2
if (success) {
3
console.log("vendorListVersion = " + globalVendorList.vendorListVersion);
4
}
5
});
Copied!
You can specify a vendorListVersion for which the Global Vendor List is requested.
Read the Global Vendor List for a specific version
1
window.__tcfapi("getVendorList", 2, function (globalVendorList, success) {
2
if (success) {
3
console.log("globalVendorList with version 10 is retrieved");
4
} else {
5
console.log("Invalid vendorListVersion");
6
}
7
}, 10);
Copied!

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.
Add a listener
1
window.__tcfapi("addEventListener", 2, function (tcData, success) {
2
if (success) {
3
console.log("Listener has been added with ID " + tcData.listenerId);
4
console.log("Event status:" + tcData.eventStatus);
5
}
6
});
Copied!
Last modified 1mo ago