addEventListener

👀

IAB GDPR TCF v2.2

Historically, the tcData object which contains encoded and unencoded values of the TC String, vendors, IAB purposes, etc... pertaining to an end-user's current browser session. was accessed via the getTCData command. This command has since been deprecated in IAB GDPR TCF v2.2.

Under IAB GDPR TCF v2.2, add addEventListener and utilize its callback function to access the tcData object.

The addEventListener command allows your organization to perform automatic actions in response to specific events. This command registers a listener with a listenerId for a callback function with the CMP which is triggered when an end-user's consent status changes.

//Return tcData object
__tcfapi('addEventListener', 2, (tcdata, success) => { console.log(tcdata) })
...
function show_tcdata(success, tcdata)    {         
  // your custom code here
  console.log('update to the tcdata object through message or privacy manager');
  console.log('the value of success is ' + success);
  console.log("tcdata eventStatus is " + tcdata.eventStatus);
  console.log('the value of tcdata is ' + JSON.stringify(tcdata));
}

__tcfapi('addEventListener', 2, function(tcdata, success) {
  if(success) {
    show_tcdata(success, tcdata);
    if (tcdata.eventStatus === 'useractioncomplete') {
      // call code when user has made an action
      show_tcdata(success, tcdata);
    } else if (tcdata.eventStatus === 'tcloaded') {
      // remove event listener when consent string has loaded
      __tcfapi('removeEventListener', 2, (success) => {
        console.log('removed event listener: ' + tcdata.listenerId);
      }, tcdata.listenerId);
    } else if (tcdata.eventStatus === 'cmpuishown') {
      // call code when cmp message is shown
    }
  }
});
...

📘

Click here for additional information on the addEventListenercommand.