@iabtcf/cmpapi
Ensures other in-page digital marketing technologies have access to CMP transparency and consent information for the IAB's Transparency and Consent Framework (TCF).
CmpApi
CmpApi is the only class needed to provide in-page digital marketing technologies access to a CMP transparency and consent information.
The process involves setting the state of a few properties and/or a valid TCModel.
Installation
npm
npm install @iabtcf/cmpapi --save
yarn
yarn add @iabtcf/cmpapi
Create CmpApi
import {CmpApi} from '@iabtcf/cmpapi';
import {TCModel} from '@iabtcf/core';
const cmpApi = new CmpApi(1, 3);
During construction of the CmpApi, the __tcfapi stub is replaced with CmpApi's own function
for handling __tcfapi command requests. Commands that were waiting to be executed in the stub are
filtered out if not valid. Ping and custom commands are executed and removed from the queue while
all other commands remain queued until a valid TCModel is set.
Note: After creation, __tcfapi can service ping commands and custom commands only. All other commands
will be queue until we have a valid TCModel. So lets create and set one.
Set TCModel
Create a valid TCModel and set it in CmpApi. The CmpApi doesn't keep the reference to the passed in TCModel, it will create it's own deep copy.
const tcModel = new TCModel();
cmpApi.tcModel = tcModel;
After creation, __tcfapi can service ping commands and custom commands only. All other commands
will be queue until we have a valid TCModel. So lets create and set one.
Show UI and Update TCModel
The CmpApi needs to know when you are going to show the user the CMP UI.
Note: You do not have to let CmpApi know when you stop showing the UI as setting the TCModel will handle this.
cmpApi.uiVisible = true;
cmpApi.tcModel = tcModel;
GDPR doesn't apply
In the case that GDPR does not apply, simply set the TCModel to null. That's all.
cmpApi.tcModel = null;
UI Options: uiVisible
There are two scenarios in which you would set uiVisible. One for true and one for false.
False - If TCData is current and does not need renewal
If TCData is current and does not need renewal, then we will not show the ui.
cmpApi.uiVisible = false;
True - If TCData is not current and we are about to show the CMP UI
cmpApi.uiVisible = true;
Disabling the CmpApi
If, for any reason, we are unable to perform the operations in compliance with
the TCF and thus should not continue to serve page request commands (other than ping),
the CmpApi provides a disable method. Calling the disabled method will put the CmpApi
into a permanent error state. Only ping and custom commands will continue to be executed
for page requests.
cmpApi.disable();
Custom Commands
The Constructor for CmpApi has an optional parameter to pass in your map of custom commands.
CmpApi will not perform any validation custom commands. The CMP is responsible for handling validations and errors. Custom function signatures
must have parameters at least a callback and any additonal params will be passed to the custom command.
Example
import {CmpApi} from '@iabtcf/cmpapi';
const cmpApi = new CmpApi(1, 3, {
'bingo': (callback, dogName) => {
callback(`There was a farmer who had a dog, and ${dogName} was his name-o`);
},
'connectBones': (callback, startBone, endBone) => {
callback(`The ${startBone} bone is connected to the ${endBone} bone.`);
},
});
const songLyricCallback = (lyrics) => {
if(success) {
console.log(lyrics)
} else {
console.error('Error: could not get song lyrics')
}
}
__tcfapi('bingo', 2, songLyricCallback, 'Bingo');
__tcfapi('connectBones', 2, songLyricCallback, 'knee', 'thigh');
CmpApi Examples
Example 1: Typical Use - Create, Set, Show and Update TCModel
The basic usage of CmpApi would be to create an instance, set the TCModel, optionally show the CMP UI and update the TCModel.
import {CmpApi} from '@iabtcf/cmpapi';
import {TCModel} from '@iabtcf/core';
import {MyCustomCommands} from './MyCustomCommands';
const cmpApi = new CmpApi(1, 3, MyCustomCommands);
const tcModel = new TCModel();
cmpApi.tcModel = tcModel;
cmpApi.uiVisible = true;
cmpApi.tcModel = tcModel;
Example 2: TCData is current and does not need renewal
import {CmpApi} from '@iabtcf/cmpapi';
import {TCModel} from '@iabtcf/core';
const cmpApi = new CmpApi(1, 3);
const tcModel = new TCModel();
cmpApi.tcModel = tcModel;
cmpApi.uiVisible = false;
Example 3: GDPR doesn't apply
import {CmpApi} from '@iabtcf/cmpapi';
import {TCModel} from '@iabtcf/core';
const cmpApi = new CmpApi(1, 3);
cmpApi.tcModel = null;