@ iabtcf/ 核心
确保对 IAB 透明度和同意框架 TC 字符串 在提供处理和操纵《公约》的工具的同时,明确坚持透明和同意信息 TCF 调制 全球供应商名单 数据全部自由和开放源代码( e)许可证) (中文(简体) ).
- 安装
- 包括你的项目
TCModel- 创建一种状态模型,以存储透明与同意用户与该数据库指定的所有字段的交互 TC 字符串 编码计划.- 全球在线 - 那个 全球供应商名单 本部分包含关于供应商和法律语言的所有信息,在《议定书》/《公约》缔约方会议用户界面中向用户显示,本部分有助于管理.
- 自动装入最新供应商名单。 json
- 自动装入特定版本的供应商列表. json
- 传出供应商列表.json 对象
- 更改 GVL 语言
- 用非默认语言克隆 GVL
- 仅让具有特定特点或目的的供应商在法律依据下参与
- 缩小供应商名单
- 过滤 gVL 子集的供应商.
- 根据目的法律依据对供应商进行分类 《议定书》/《公约》缔约方会议需要让销售商了解情况.
- 装入 GVL JSON 文件
- 装入 语言翻译 目的、特征、特殊特征、特殊目的和堆积
- TCString 键 - 编码 a
TCModel改为 a TC 字符串 并解码 a TC 字符串 改为 aTCModel
安装
无
npm install @iabtcf/core --save
线条
yarn add @iabtcf/core
使用
这个例子证明了CMP创建"默认全无"TC字符串的基本用途案例.
import {TCModel, TCString, GVL} from '@iabtcf/core';
/**
* the IAB requires CMPs to host their own vendor-list.json files. This must
* be set before creating any instance of the GVL class.
*/
GVL.baseUrl = "http://mydomain.com/cmp/vendorlist";
// create a new TC string
const tcModel = new TCModel(new GVL());
// Some fields will not be populated until a GVL is loaded
tcModel.gvl.readyPromise.then(() => {
// Set values on tcModel...
const encodedString = TCString.encode(tcModel);
console.log(encodedString); // TC string encoded begins with 'C'
});
// take an encoded TC string and decode into a TCModel
const decodedTCModel = TCString.decode(encodedString);
三角形模型
创建新的 TCModel
编码 a TCModel (单位:千美元) GVL 必须包含.
import {TCModel} from '@iabtcf/core';
// creates a TCModel
const tcModel = new TCModel();
// to encode you must have a cmpId and cmpVersion
tcModel.cmpId = //{myCMPID}
tcModel.cmpVersion = //{myCMPVersion}
/**
* we now have a TCString assigned with a GVL which will set vendorListVersion,
* tcfPolicyVersion and consentLanguage
*/
CMP 元字段
这个示例说明如何设置所有TC字符串需要设置的基本字段.
import {TCModel} from '@iabtcf/core';
const tcModel = new TCModel();
tcModel.cmpId = // my CMP ID
tcModel.cmpVersion = // my CMP Version
tcModel.consentScreen = // On which 'screen' consent was captured; this is a cmp proprietary number encoded into the TC string
向量
这个 TCModel 杠杆a Set 样式 Vector 数据结构用于设定同意、选择、允许、披露和合法权益建立。 影响此数据结构的属性为:
- 供应商
vendorConsentsvendorLegitimateInterestsvendorsAllowedvendorsDisclosed
- 全球目的
purposeConsentspurposeLegitimateInterests
- 特殊特性
specialFeatureOptins
- 出版商
publisherConsentspublisherCustomConsentspublisherLegitimateInterestspublisherCustomLegitimateInterestspublisherRestrictions- 这个矢量很特别
PurposeRestrictionVector联 合 国PurposeRestrictions
- 这个矢量很特别
示例 vendorConsents
这个 vendorConsents 财产 TCModel 是一个 Vector。这个例子说明一种 Vector。除 publisherRestrictions,用于执行不同类型 PurposeRestrictionVector,上面所有的矢量都会有这个接口和功能.
// Give Vendor ID 24 consent
tcModel.vendorConsents.set(24);
console.log(tcModel.vendorConsents.has(24)); // true
console.log(tcModel.vendorConsents.maxId); // 24
console.log(tcModel.vendorConsents.size); // 1
// remove vendor 24
tcModel.vendorConsents.unset(24);
console.log(tcModel.vendorConsents.has(24)); // false
console.log(tcModel.vendorConsents.maxId); // 0
console.log(tcModel.vendorConsents.size); // 0
// give a group of vendors consent
tcModel.vendorConsents.set([24, 14, 24, 100, 102]);
console.log(tcModel.vendorConsents.has(24)); // true
console.log(tcModel.vendorConsents.has(14)); // true
console.log(tcModel.vendorConsents.has(200)); // false
console.log(tcModel.vendorConsents.maxId); // 102
console.log(tcModel.vendorConsents.size); // 5
// loop through all ids 1 to maxId (102 loops)
tcModel.vendorConsents.forEach((hasConsent, vendorId) => {
// check each id for consent
});
// empty everything
tcModel.vendorConsents.empty();
console.log(tcModel.vendorConsents.has(24)); // false
console.log(tcModel.vendorConsents.maxId); // 0
console.log(tcModel.vendorConsents.size); // 0
设置出版商限制
A 类 出版者限制 是由出版商限制供应商处理个人数据的目的。 这个 TCModel.publisherRestrictions 是一个实例 PurposeRestrictionVector,这是含有 PurposeRestrictions对.
设置出版商限制的实例
import {TCModel, PurposeRestriction, RestrictionType} from '@iabtcf/core';
// first you must create a PurposeRestriction
const purposeRestriction = new PurposeRestriction();
purposeRestriction.purposeId = 2;
purposeRestriction.restrictionType = RestrictionType.NOT_ALLOWED;
// vendorID and restriction
tcModel.publisherRestrictions.add(2000, purposeRestriction);
全球在线
- 自动装入最新供应商名单。 json
- 自动装入特定供应商名单
- 传出供应商列表.json 对象
- 更改 GVL 语言
- 用非默认语言克隆 GVL
- 仅让具有特定特点或目的的供应商在法律依据下参与
- 缩小供应商名单
这个 GVL 阶级提供了两种即时反应的方法. 要么通过一个销售商列表. json反对插入,要么从一个url自动装入一个销售商列表. json.
自动装入最新供应商名单。 json
自动加载一个供应商列表. json会接受一个供应商列表的版本号,或者如果没有任何内容或"LATEST"通过,它会加载最新的供应商列表. 说明。 你必须设置 GVL.baseUrl 参数在即时化 GVL 实例前。 如果需要的话 GVL.latestFilename 如果 vendor-list.json 未使用.
正在装入默认文件名
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
// loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'
const gvl = new GVL();
gvl.readyPromise.then(() => {
// GVL has loaded and it's ready to use
});
装入自定义文件名
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
GVL.latestFilename = 'latest/vendorlist.js';
// loads 'http://cmp.mysupercoolcmp.com/cmp/latest/vendorlist.json'
const gvl = new GVL();
gvl.readyPromise.then(() => {
// GVL has loaded and it's ready to use
});
自动装入特定版本的供应商列表. json
自动加载特定版本需要你们两个设置 GVL.baseUrl 静态变量并传递到构建器中您想要加载的版本编号。 如果您的文件名有其它版本, 可选择 vendor-list-v[VERSION].json 您可以设置一个不同的文件名, 版本为 GVL.versionedFilename = 'vendorlist[VERSION].json'; 和标志 [version] 将替换为您通过到构建器的版本编号.
正在装入版本的默认文件名
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
// loads 'http://cmp.mysupercoolcmp.com/cmp/archives/vendor-list-v23.json'
const gvl = new GVL(23);
gvl.readyPromise.then(() => {
// GVL has loaded and it's ready to use
});
更改版本名称方案
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
GVL.versionedFilename = 'vendorlist[VERSION].json';
// loads 'http://cmp.mysupercoolcmp.com/cmp/vendorlist23.json'
const gvl = new GVL(23);
gvl.readyPromise.then(() => {
// GVL has loaded and it's ready to use
});
传出供应商列表.json 对象
您也可以在 Json 对象中通过( 未被固定)
import {GVL} from '@iabtcf/core';
const gvl = new GVL(gvljson);
// no need for ready promise because no asynchronous action has occurred
// gvl is ready to use
更改 GVL 语言
所有卖家名单默认以英文发布. iab出版的还有替代语言,即没有供应商的供应商名单. GVL.baseUrl 必须被设定用于改变标签。 要装入候补您只需拨打 gvl.changeLanguage(/**language*/); laanguge是同位素639-1双字母laanguge代码. 提供的全部iab语言翻译列表请点击此处。如果需要的话 GVL.languageFilename 如果 purposes-[LANG].json 未使用.
默认文件名负载
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/';
const gvl = new GVL(gvljson);
// loads the French langauge
gvl.changeLanguage('fr').then(() => {
// French Language GVL is ready for use
});
更改文件名负载
import {GVL} from '@iabtcf/core';
const gvl = new GVL(gvljson);
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/';
GVL.langaugeFilename = 'langauges/purp[LANG].json';
// loads the French langauge from 'http://cmp.mysupercoolcmp.com/purpfr.json'
gvl.changeLanguage('fr').then(() => {
// French Language GVL is ready for use
});
用非默认语言克隆 GVL
在以非默认语言复制 GVL 时, 请确保任何先前的 changeLanguage 电话解决了。 如果改变语言还没有解决, clone 将请求 http 的当前语言,但无法显示该请求是否得到解决,因为它是同步的.
import {GVL} from '@iabtcf/core';
const gvl = new GVL();
// Resolving changeLanguage promise using thenables.
gvl.changeLanguage('fr').then(() => {
const clone = gvl.clone();
})
// Resolving changeLanguage through async/await
const someAsyncFunction = async () => {
await gvl.changeLanguage('fr');
const clone = gvl.clone();
}
仅让具有特定特点或目的的供应商在法律依据下参与
《议定书》/《公约》缔约方会议UI不妨将供应商分组,按其使用何种法律依据和/或特征。 通过使用6个分组方法之一,可以很容易地做到这一点:
getVendorsWithConsentPurpose(purposeId)getVendorsWithFeature(featureId)getVendorsWithFlexiblePurpose(purposId)getVendorsWithLegIntPurpose(purposId)getVendorsWithSpecialFeature(featureId)getVendorsWithSpecialPurpose(purposId)
所有6个分组方法都返回 IntMap<Vendor> 对象
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
// loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'
const gvl = new GVL();
gvl.readyPromise.then(() => {
const vendorMap = gvl.getVendorsWithConsentPurpose(1);
// logs all vendor ids who have specified they require consent for purpose 1
Object.keys(vendorMap).forEach(console.log);
});
缩小供应商名单
如果装入《议定书》/《公约》缔约方会议想要显示供应商名单的一个子集,则只能将过滤器传给与名单上的供应商一起工作.
import {GVL} from '@iabtcf/core';
// only needs to be set once per application as this is a static variable
GVL.baseUrl = 'http://cmp.mysupercoolcmp.com/cmp/';
// loads 'http://cmp.mysupercoolcmp.com/cmp/vendor-list.json'
const gvl = new GVL();
gvl.readyPromise.then(() => {
// now this gvl instance only has these 3 vendors
gvl.narrowVendorsTo([1,2,3]);
// will only show the Vendor objects for 1, 2, and 3
console.log(gvl.vendors);
// will only return the vendors within the narrowed vendor list
const vendorsWithLegInt2 = gvl.getVendorsWithLegIntPurpose(2);
});
TCString 键
解码 IAB TC 字符串
import {TCString, TCModel} from '@iabtcf/core';
const myTCModel = TCString.decode(encodedTCString);
返回 : TCModel
编码 IAB TC 字符串
import {TCString, TCModel, GVL} from '@iabtcf/core';
/**
* 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
const gvl = new GVL("LATEST");
// have to wait for it to fetch the json
gvl.readyPromise.then(() => {
const tcModel = new TCModel(gvl);
const encodedTCString = TCString.encode(tcModel);
/**
* this will output a default all "no" encoded string to the console at the
* lastest GVL version.
*/
console.log(encodedTCString);
});
一次解码一段
可以通过提及已创建的 TCModel 并在模型中添加单个片段。 这方面的一个重要用途是使用全球范围的TC字符串,并将出版商TC片段单独存储在一党饼干中.
import {TCString, Segment} from '@iabtcf/core';
// if you had a getCookie function that returned just that segment
const publisherTCSegment = getCookie('euconsent-ptc');
let tcModel;
// if you had an asynchronous function to get the cookie from global
getGlobalTCString().then((encodedTCString) => {
tcModel = TCString.decode(encodedTCString);
TCString.decode(publisherTCSegment, tcModel);
// now you have a combined TCModel
});
编码选项
选项可能会被传递到编码器来覆盖默认值. 在大多数情况下,编码器可以找出要包括哪些部分,但如果字符串被浮出销售商的表面或用于将编码存储在饼干中,则编码略有不同. 这个 @iabtcf/cmpapi 处理这种差异, CMPs不需要担心.
import {TCString, Segment} from '@iabtcf/core';
// produces the version of the string for the __tcfapi function
const cmpApiEncodedString = TCString.encode(tcModel, { isForVendors: true });
// Overrides default segments (not recommended unless you know what you're doing)
const encodingOptions = {
segments: [
Segment.CORE,
Segment.VENDORS_DISCLOSED,
Segment.VENDORS_ALLOWED,
Segment.PUBLISHER_TC,
]
}
const customEncodedString = TCString.encode(tcModel, encodingOptions);
编码一个出版商 TC 片段
默认,如果 TCModel.isServiceSpecific = true 然后编码一个字符串,包括出版商TC段。 不过如果 TCModel.isServiceSpecific = false 然后,该段只应被浮出 __tcfapi 界面,而不保存到全局饼干。 然而,人们需要一种方法来访问并保存出版商TC部分与主TC String分开,以作为一党饼干来存储. 这样的话,你可以使用 EncodingOptions 只生成一个 Publicer TC 部分.
const encodingOptions = {
segments: [Segment.PUBLISHER_TC]
}
const publisherTCSegment = TCString.encode(tcModel, encodingOptions);