didomi-iabtcf-core
Ensures consistent encoding and decoding of IAB's Transparency and Consent Framework (TCF) TC Strings and the stateful persistence of the Transparency and Consent information while providing tools for the handling and manipulation of the TCF Global Vendor List data all free and open sourced (License).
- Installation
- Including in your project
TCModel
- Creates a stateful model to store a Transparency and Consent user interaction with all the fields specifed in the TC string encoding schema.- GVL - The Global Vendor List contains all of the information about vendors and legal language to display to users in a CMP user interface, this component helps manage it.
- Autoload latest vendor-list.json
- Autoload specific version vendor-list.json
- Pass vendor-list.json object
- Change GVL Language
- Get only vendors with a specific feature or purpose under legal basis
- Narrow the list of vendors
- filtering vendors for subsets of the GVL.
- sorting of vendors based on purpose legal bases that the CMP will need to show the vendors in.
- loading GVL JSON files
- loading language translations of Purposes, Features, Special Features, Special Purposes, and Stacks
- TCString - Encodes a
TCModel
into a TC string and decodes a TC string into aTCModel
Installation
npm
npm install didomi-iabtcf-core --save
yarn
yarn add didomi-iabtcf-core
Using
This example demonstrates the basic use case of a CMP creating a "default all-no" TC string.
; /** * the IAB requires CMPs to host their own vendor-list.json files. This must * be set before creating any instance of the GVL class. */GVLbaseUrl = "http://mydomain.com/cmp/vendorlist"; // create a new TC stringconst tcModel = ; // Some fields will not be populated until a GVL is loadedtcModelgvlreadyPromise; // take an encoded TC string and decode into a TCModelconst decodedTCModel = TCString;
TCModel
Creating a new TCModel
To encode a TCModel
a GVL
must be included.
; // creates a TCModelconst tcModel = ; // to encode you must have a cmpId and cmpVersiontcModelcmpId = //{myCMPID}tcModelcmpVersion = //{myCMPVersion} /** * we now have a TCString assigned with a GVL which will set vendorListVersion, * tcfPolicyVersion and consentLanguage */
CMP Meta Fields
This exmple shows how to set the basic fields that all TC strings need to have set.
; const tcModel = ;tcModelcmpId = // my CMP IDtcModelcmpVersion = // my CMP VersiontcModelconsentScreen = // On which 'screen' consent was captured; this is a cmp proprietary number encoded into the TC string
Vectors
The TCModel
leverages a Set
style Vector
data structure to set consents, optins, allowed, disclosed, and legitimate interest establishment. Properties that leverage this data structure are:
- Vendors
vendorConsents
vendorLegitimateInterests
vendorsAllowed
vendorsDisclosed
- Global Purposes
purposeConsents
purposeLegitimateInterests
- Special Feature Opt-Ins
specialFeatureOptIns
- Publisher
publisherConsents
publisherCustomConsents
publisherLegitimateInterests
publisherCustomLegitimateInterests
publisherRestrictions
- This Vector is a special
PurposeRestrictionVector
ofPurposeRestrictions
- This Vector is a special
Example with vendorConsents
The vendorConsents
property on the TCModel
is a Vector
. This example illustrates the methods of a Vector
. With the exception of the publisherRestrictions
, which implements a different type of PurposeRestrictionVector
, all of the above Vectors will have this interface and functionality.
// Give Vendor ID 24 consenttcModelvendorConsents; console; // trueconsole; // 24console; // 1 // remove vendor 24tcModelvendorConsents;console; // falseconsole; // 0console; // 0 // give a group of vendors consenttcModelvendorConsents;console; // trueconsole; // trueconsole; // falseconsole; // 102console; // 5 // loop through all ids 1 to maxId (102 loops)tcModelvendorConsents; // empty everythingtcModelvendorConsents; console; // falseconsole; // 0console; // 0
Setting Publisher Restrictions
A Publisher Restriction is a restriction placed on a Vendor by a publisher limiting the purposes for which that Vendor is allowed to process personal data. The TCModel.publisherRestrictions
is an instance of the PurposeRestrictionVector
, which is a vector containing PurposeRestrictions
's.
Example of setting publisher restrictions
; // first you must create a PurposeRestrictionconst purposeRestriction = ; purposeRestrictionpurposeId = 2;purposeRestrictionrestrictionType = RestrictionTypeNOT_ALLOWED; // vendorID and restrictiontcModelpublisherRestrictions;
GVL
- Autoload latest vendor-list.json
- Autoload specific vendor-list.json
- Pass vendor-list.json object
- Change GVL Language
- Get only vendors with a specific feature or purpose under legal basis
- Narrow the list of vendors
The GVL
class provides two ways to instantiate. Either by passing in a vendor-list.json object to populate it or autoloading a vendor-list.json from a url.
Autoload latest vendor-list.json
Autoloading a vendor-list.json will accept into the constructor a vendor list version number or if nothing or "LATEST" is passed it will load the latest version of the vendor list. NOTE. You must set the GVL.baseUrl
parameter before instantiating a GVL instance. If desired the GVL.latestFilename
may be set if vendor-list.json
is not used.
Loading default filename
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; // loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'const gvl = ; gvlreadyPromise;
Loading with custom filename
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; GVLlatestFilename = "latest/vendorlist.js"; // loads 'http://cmp.mysupercoolcmp.com/cmp/latest/vendorlist.json'const gvl = ; gvlreadyPromise;
Autoload specific version vendor-list.json
Autoloading a specific version requires that you both set the GVL.baseUrl
static variable and pass into the constructor the version number you wish to load. Optionally if your filename has a version other than vendor-list-v[VERSION].json
you may set a different filename with version as GVL.versionedFilename = 'vendorlist[VERSION].json';
and the token [version]
will be replaced with the version number you pass into the constructor.
Loading default filename for version
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; // loads 'http://cmp.mysupercoolcmp.com/cmp/archives/vendor-list-v23.json'const gvl = 23; gvlreadyPromise;
Changing version name scheme
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; GVLversionedFilename = "vendorlist[VERSION].json"; // loads 'http://cmp.mysupercoolcmp.com/cmp/vendorlist23.json'const gvl = 23; gvlreadyPromise;
Pass vendor-list.json object
You may also just pass in the json object (not strigified)
; const gvl = gvljson; // no need for ready promise because no asynchronous action has occurred// gvl is ready to use
Change GVL Language
All vendorlists are published by default as english. There are alternate languages that the iab publishes which are essetnailly a vendor list without the vendors. GVL.baseUrl
must be set for langauges changes. To load an alternate you simply call gvl.changeLanguage(/**language*/);
langauge is the iso639-1 two-letter langauge code. For the full list of iab provided language translations click here. If desired the GVL.languageFilename
may be set if purposes-[LANG].json
is not used.
Default filename load
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/"; const gvl = gvljson; // loads the French langaugegvl;
Changing filename load
; const gvl = gvljson; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/"; GVLlangaugeFilename = "langauges/purp[LANG].json"; // loads the French langauge from 'http://cmp.mysupercoolcmp.com/purpfr.json'gvl;
Get only vendors with a specific feature or purpose under legal basis
A CMP UI may want to group vendors by what purpose they use under what legal basis and/or features. This can be accomplished quite easily by using one of the 6 grouping methods:
getVendorsWithConsentPurpose(purposeId)
getVendorsWithFeature(featureId)
getVendorsWithFlexiblePurpose(purposId)
getVendorsWithLegIntPurpose(purposId)
getVendorsWithSpecialFeature(featureId)
getVendorsWithSpecialPurpose(purposId)
All 6 grouping methods return an IntMap<Vendor>
object
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; // loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'const gvl = ; gvlreadyPromise;
Narrow the list of vendors
If loading a CMP would like to show a subset of the Vendor List a filter may be passed to only work with those vendors on the list.
; // only needs to be set once per application as this is a static variableGVLbaseUrl = "http://cmp.mysupercoolcmp.com/cmp/"; // loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'const gvl = ; gvlreadyPromise;
TCString
Decode an IAB TC String
; ;
returns: TCModel
Encode an IAB TC String
; /** * With v2.0 of the TCF, CMPs are required to host their own vendor-list.json for * their client-side scripts to consume. This GVL class follows the convention * outlined in the GVL URL Version scheme. (latest at vendor-list.json and * version specific at archives/vendor-list-v{vendor-list-version}.json */GVL.baseUrl = "http://mydomain.com/cmp/vendorlist"; // we'll get the latest GVL to encode this TCString to; // have to wait for it to fetch the jsongvl.readyPromise.then;
Decoding A Segment At A Time
It is possible to pass a reference to an already created TCModel
and add individual segments to the model. An important use case for this is if using a globally-scoped TC string and storing the Publisher TC Segment separately in a first-party cookie.
; // if you had a getCookie function that returned just that segment; ; // if you had an asynchronous function to get the cookie from globalgetGlobalTCString.then;
Encoding Options
Options may be passed to the encoder to override defaults. In most cases the encoder can figure out what segments to include but if the string is surfaced to vendors or for storing in a cookie the encoding is slightly different. The didomi-iabtcf-cmpapi
handles this difference and CMPs don't need to worry about it.
; // produces the version of the string for the __tcfapi function; // Overrides default segments (not recommended unless you know what you're doing); ;
Encoding a Publisher TC Segment
By default if the TCModel.isServiceSpecific = true
then encoding a string will include the publisherTC segment. But if TCModel.isServiceSpecific = false
then the segment should only be surfaced through the __tcfapi
interface and not saved to the global cookie. However, one will need a way to access and save the publisher TC segment separately from the main TC String to store as a first-party cookie. In that case you can use the EncodingOptions
to generate only a Publisher TC segment.
;;