__tcfapi function - addEventListener, removeEventListener

In this article we will cover the IAB __tcfapi() function with the addEventListener command. Your organization can use these commands in Javascript to perform automatic actions in response to specific events, for example when an end-user updates their consent status. Click here to find more information about the TCF API commands from the IAB.


the addEventListener command

The addEventListener command registers a listener for a callback function with the CMP. The listener's callback function is triggered when the end-user consent status changes. The listener is given an ID for later removal.

Your organization can respond to specific events or actions made by the end-user with your own custom functions using the addEventListener command.

addEventListener command

__tcfapi('addEventListener', 2, (tcdata, success) => { console.log('the updated consent data object is: ' + JSON.stringify(tcdata)); });

the removeEventListener command

To remove an event listener you call the __tcfapi() function using the removeEventListener command. An additional parameter is required, the listener id created with the addEventListener command.

removeEventListener command

__tcfapi('removeEventListener', 2, (success) => { console.log('removed event listener: ' + tcdata.listenerId); }, tcdata.listenerId);

Example implementation

Your organization can call the addEventListener & removeEventListener command through the browser console or Javascript code on a webpage.

With browser console Javascript code

Once your organization's website has loaded enter this command directly into the browser tools console window and press enter.

In this example an event listener is created through addEventListener. Opening the privacy manager and making changes to the end-user consent (updating the tcdata object) will display the updated tcdata object automatically.

__tcfapi('addEventListener', 2, (tcdata, success) => { console.log('the updated consent data object is: ' + JSON.stringify(tcdata)); });
Was this article helpful?
0 out of 0 found this helpful