@didomi/iabtcf-core

中文文档
1.6.6发布于 2026/10/20Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 2973

确保对iab的TC信号进行一致的编码和解码. 透明度和同意框架.

中文使用文档
文档目录16 节
  1. 创建新的 TCModel
  2. CMP 元字段
  3. 向量
  4. 设置出版商限制
  5. 自动装入最新供应商名单。 json
  6. 自动装入特定版本的供应商列表. json
  7. 传出供应商列表.json 对象
  8. 更改 GVL 语言
  9. 用非默认语言克隆 GVL
  10. 仅让具有特定特点或目的的供应商在法律依据下参与
  11. 缩小供应商名单
  12. 解码 IAB TC 字符串
  13. 编码 IAB TC 字符串
  14. 一次解码一段
  15. 编码选项
  16. 编码一个出版商 TC 片段

NPM version npm module downloads per month InteractiveAdvertisingBureau

@ iabtcf/ 核心

确保对 IAB 透明度和同意框架 TC 字符串 在提供处理和操纵《公约》的工具的同时,明确坚持透明和同意信息 TCF 调制 全球供应商名单 数据全部自由和开放源代码( e)许可证) (中文(简体) ).

安装

无

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 数据结构用于设定同意、选择、允许、披露和合法权益建立。 影响此数据结构的属性为:

  • 供应商
    • vendorConsents
    • vendorLegitimateInterests
    • vendorsAllowed
    • vendorsDisclosed
  • 全球目的
    • purposeConsents
    • purposeLegitimateInterests
  • 特殊特性
    • specialFeatureOptins
  • 出版商
    • publisherConsents
    • publisherCustomConsents
    • publisherLegitimateInterests
    • publisherCustomLegitimateInterests
    • publisherRestrictions
      • 这个矢量很特别 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);

全球在线

这个 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);

版本与发布记录

@didomi/iabtcf-core 共发布 20 个版本,首次发布于 2021/06/29;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.6.6latest2026/10/2022 天前
1.6.52026/02/238 个月前
1.6.42025/09/241.1 年前
1.6.32025/04/151.6 年前
1.6.22024/07/152 年前
1.6.12024/03/272 年前

关于 @didomi/iabtcf-core 的常见问题

@didomi/iabtcf-core 是什么?

确保对iab的TC信号进行一致的编码和解码. 透明度和同意框架.

如何安装 @didomi/iabtcf-core?

在项目目录运行 npm install @didomi/iabtcf-core,或使用 pnpm add / yarn add / bun add。

@didomi/iabtcf-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@didomi/iabtcf-core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@didomi/iabtcf-core 可以商用吗?

@didomi/iabtcf-core 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@didomi/iabtcf-core 的下载量有多少?

根据 npm 官方下载统计,@didomi/iabtcf-core 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,973 次,日均约 99 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@didomi/iabtcf-core 还在维护吗?

最新版本 1.6.6 发布于 2026/10/20(22 天前);至今共发布 20 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/06/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。