getTCData
- 1 Functionality
- 2 Syntax
- 3 Description
- 3.1 Scope
- 4 Examples
- 4.1 Basic usage
Functionality
This function is used to retrieve the users' consent. It is part of the IAB Europe Transparency and Consent Framework (TCF) Specifications.
Syntax
function __tcfapi()
argument name | type | optional | value | description |
---|---|---|---|---|
command | string |
|
|
|
version | integer |
|
| The TCF API version to use. Can be null instead. |
callback | function |
|
| The callback function that is called with the function call result. |
parameter | int array | ✔️ |
| Array holding the specific vendor ids from which we want to know consent. |
Description
In order to retrieve user consent, the IAB specification provides two different functions: addEventListener and getTCData
. Although both can retrieve user consent, addEventListener is recommended, given it can "follow" the user interaction, making sure that it will eventually return the consent data once the user has finished interacting with the CMP GUI. On the other hand, getTCData
function just returns the current consent status. This means that it may return uninitialized, incomplete, or full consent data, depending on the timing it is called. This means that the responsibility to obtain the definite data for the consent falls over the publisher’s lap. With addEventListener every consent status change triggers a new call to the given callback, which means that the only thing needed to process the consent data is a simple check for whether or not that consent data is final, thus usable down the line.
Scope
The function can be called at any time, but the response will change depending on when it is called.
Examples
Basic usage
When getTCData
is called, it will immediately respond with a result, no matter if the user consented already or not. The callback will be only called once.
__tcfapi('getTCData', 2, function(tcData, success) {
if(success && (tcData.eventStatus === 'tcloaded' || tcData.eventStatus === 'useractioncomplete')) {
if(Object.keys(tcData.vendor.consents).length == 0 && Object.keys(tcData.purpose.consents).length == 0) {
// User clicked "Customize choices" and then "Save preferences"
console.log("User rejected consent", tcData);
} else {
// User either clicked "Accept All" (most likely) and gave full consent or configured individual consent (unlikely). If required, check vendor and purpose individual user choices.
console.log("User gave full or partial consent", tcData);
}
} else {
console.log("User consent not available yet", tcData);
}
});