Leading Consent Management Platform

Compliant with GDPR, CCPA, COPPA, LGPD, PECR, PDPA, PIPEDA, and more.

Execute JavaScript code and determining the consent signal

如果您使用单一标签(https://cmp.uniconsent.com/v2/cmp.js)安装 UniConsent,标签运行后即可使用两个函数:

  • unicReady(callback) 在同意状态最终确定时触发一次。
  • unicUpdate(callback) 与 unicReady 同时触发,之后每次同意状态变化时再次触发。
unicReady(function (status) {
  if (status.UNIC_P_1) {
    loadAnalytics();
  }
});

unicUpdate(function (status) {
  applyConsent(status);
});

完整参考(包括状态对象、DOM 事件和异步队列保护)请见 Consent Ready API。

UniConsent CMP v2 API

  • openunic

openunic API 用于向用户显示 UniConsent 同意管理平台(CMP)界面。需要让用户查看和修改同意偏好时调用它。

__unicapi('openunic')
  • agreeAll

agreeAll API 表示用户同意所有同意选项。在您自定义的 CMP 界面中,当用户点击第一层的“全部同意”按钮时调用,表示用户同意所有类别的数据处理。

__unicapi('agreeAll')
  • rejectAll

rejectAll API 表示用户拒绝所有同意选项。在您自定义的 CMP 界面中,当用户点击第一层的“全部拒绝”按钮时调用,表示用户不同意任何类别的数据处理。

__unicapi('rejectAll')
  • openOptions

openOptions API 用于打开 UniConsent CMP 的第二层界面,提供更详细的同意选项和设置。

__unicapi('openOptions')
  • openVendorList

openVendorList API 用于在 UniConsent CMP 中显示供应商列表界面,介绍可能处理用户数据的第三方供应商。

__unicapi('openVendorList')
  • openCookiesList

openCookiesList API 用于在 UniConsent CMP 中显示 Cookie 列表界面,介绍网站使用的 Cookie。

__unicapi('openCookiesList')

HTML 链接示例:

<a class="font-bold underline cursor-pointer" href="javascript:void(0);" onclick="__unicapi('openCookiesList');">Cookies List</a>
  • getUConsent

获取自定义供应商的同意状态,格式为供应商 ID 对应 true 或 false。未启用自定义供应商时返回 false。

__unicapi('getUConsent', 2, (uConsent, success) => {
  if (success && uConsent && uConsent['1']) {
    // custom vendor 1 is allowed
  }
})
  • getUCData

获取自定义供应商的同意数据,每个供应商的同意状态位于 vendor.consents。

__unicapi('getUCData', 2, (ucData, success) => {
  if (success && ucData.vendor) {
    console.log(ucData.vendor.consents)
  }
})
  • getEConsent

在同意类别横幅下,获取访客允许的类别,格式为类别 ID(1 至 4)对应 true,例如 {"1": true, "2": true}。它不跟随 IAB TCF 选择:如需在所有地区都有值,请使用 unicReady 和 unicUpdate 中的 UNIC_EP_1 至 UNIC_EP_4。

__unicapi('getEConsent', 2, (eConsent, success) => {
  if (success && eConsent && eConsent['2']) {
    // category 2 (Performance / Analytics) is allowed
  }
})
  • getCustomConsent

获取您的自定义目的的同意状态。

__unicapi('getCustomConsent', 2, (data, success) => {
  if (success) {
    console.log(data);
    // Example: { "custom_analytics": true, "ab_testing": false }

    if (data['custom_analytics']) {
      // custom analytics consent is granted
    }
  }
});
  • setCustomConsent

以编程方式设置您的自定义目的的同意状态。提供的值会与现有状态合并,并与访客的其他选择一起保存。

__unicapi('setCustomConsent', 2, { "custom_analytics": true, "ab_testing": false });
  • getConsentId

获取用户同意状态的审计同意 ID。

__unicapi('getConsentId', 2, (consentId) => {
  console.log(consentId);
});

IAB TCF V2 同意管理平台 API

  • getTCData(已弃用,请使用 addEventListener)
__tcfapi(
  'getTCData',
  2,
  (tcData, success) => {
    if (success) {
      // do something with tcData
    } else {
      // do something else
    }
  },
  [1, 2, 3]
) // vendors Ids
  • ping
__tcfapi('ping', 2, (pingReturn) => {
  // do something with pingReturn
})
  • addEventListener
const callback = (tcData, success) => {
  if (success && (tcData.eventStatus === 'tcloaded' || tcData.eventStatus === 'useractioncomplete')) {
    // do something with tcData.tcString

    // remove the ourself to not get called more than once
    __tcfapi(
      'removeEventListener',
      2,
      (success) => {
        if (success) {
          // oh good...
        }
      },
      tcData.listenerId
    )
  } else {
    // do something else
  }
}

__tcfapi('addEventListener', 2, callback)
  • getVendorList
__tcfapi(
  'getVendorList',
  2,
  (gvl, success) => {
    if (success) {
      // do something with gvl
    } else {
      // do something else
    }
  },
  'LATEST'
)

完整 API 请见 IAB TCF 文档。

美国隐私用户信号机制 “USP API”(CCPA 合规机制)

  • getUSPData

获取当前的 USP 同意数据。

__uspapi('getUSPData', 1, (uspData, success) => {
  if (success) {
    // do something with uspData
  } else {
    // do something else
  }
})

返回的 uspData:

{
 "version": 1, /* number indicating the U.S. Privacy spec version */
 "uspString": "1YNN" /* string; not applicable: "1---" */
}
  • registerDeletion

注册一个回调函数,当用户在 CCPA/USP 同意界面中点击 “删除我的数据” 按钮时调用。发布商可借此按照《加州消费者隐私法》(CCPA)的要求处理数据删除请求。

__uspapi('registerDeletion', 1, (param) => {
  // Handle user data deletion request
  // For example: call your backend API to delete user data
  console.log('User requested data deletion');

  // Example: send a deletion request to your server
  fetch('/api/delete-user-data', {
    method: 'POST',
    body: JSON.stringify({ action: 'delete' })
  });
})

用户点击“删除我的数据”后,依次执行以下操作:

  1. 调用已注册的删除回调,由发布商实际删除数据。
  2. 用户的 USP 同意设为退出(1YY),表示用户不同意出售其个人数据。
  3. USP 同意字符串保存到 usprivacy Cookie。
  4. 关闭同意界面。
  • performDeletion

以编程方式触发数据删除流程。它会调用通过 registerDeletion 注册的回调,可用于构建您自己的删除界面。

__uspapi('performDeletion', 1, null, param)

在启动广告前等待 IAB TCF 选择

一旦访客的 IAB TCF 选择已知,tcData.unicLoad 即为 true:页面加载时已保存的选择,或刚刚做出的选择(同意或拒绝)。随后您的广告技术栈会从 TC 字符串读取该选择。在不适用 IAB TCF 的地区始终为 true。

__tcfapi('addEventListener', 2, function (tcData, success) {
  if (success && tcData.unicLoad === true) {
    if (!window._initAds) {
      window._initAds = true
      // your code to start the ads
    }
  }
})

unic_data 事件中的变量

数据层中的 unic_data 事件包含以下变量,您可以在 Google Tag Manager 中将其作为数据层变量读取。它会在页面加载时推送,并在每次同意选择或更改后再次推送。在页面代码中,unicReady 和 unicUpdate 提供相同的同意值:请参阅 Consent Ready API。

变量含义
UNIC_IS_LOAD在适用 IAB TCF 的地区,仅当访客允许存储或任一自定义供应商时为 true:做出选择前和拒绝后为 false。在不适用 IAB TCF 的地区始终为 true
GEO适用规则的地区,例如 EU、CA、USCA 或 USTX
LOC检测到的位置
GEO_IS_EU, GEO_IS_US, GEO_IS_USCA, GEO_IS_CA, GEO_IS_BR, GEO_IS_CN, GEO_IS_ZA, GEO_IS_APAC, GEO_IS_LATAM, GEO_IS_OTHER与访客位置对应的标志为 true
US_STATE访客所在美国州的两位字母代码,美国以外为空
GEO_IS_MSPA位于 US State Privacy 覆盖的州时为 true
TC_STRINGIAB TCF 字符串
GPP_STRINGIAB GPP 字符串
uspDataUS Privacy 字符串
MSPA_STRINGUS State Privacy 字符串(IAB GPP 美国全国部分)
GPC浏览器发送 Global Privacy Control 时为 true
UNIC_P_1 … UNIC_P_10, UNIC_SF_1, UNIC_SF_2IAB TCF 目的和特殊功能
UNIC_EP_1 … UNIC_EP_4同意类别 1 至 4
UNIC_CP_{id}您的自定义用途,按用途 ID
customVendors您的自定义供应商的同意,按供应商 ID
CONSENT_MODEGoogle Consent Mode 值

unic_data 会在 unicUpdate 运行前推送到数据层,因此在 unicUpdate 回调中,最新的 unic_data 事件包含当前的 Google Consent Mode 值(启用 Google Consent Mode 时):

unicUpdate(function () {
  var events = (window.dataLayer || []).filter(function (e) { return e && e.event === 'unic_data'; });
  var latest = events[events.length - 1];
  if (latest) {
    console.log(latest.CONSENT_MODE);
  }
});

如何在 iFrame 中加载带 UniConsent CMP 的网页

当您的页面在 iFrame 中加载(例如在 Discord 中)时,可能需要使用 SameSite=None; Partitioned; 同步 Cookie,可在 CMP 标签下方添加以下代码。

const callback = (tcData, success) => {
  if (success && tcData.eventStatus === "useractioncomplete") {
    __tcfapi(
      'removeEventListener',
      2,
      (success) => {
        if (success) {
            const date = new Date();
            date.setTime(date.getTime() + (300 * 24 * 60 * 60 * 1000));
            const expires = `expires=${date.toUTCString()}`;
            document.cookie = `euconsent-v2=${tcData.tcString}; ${expires}; path=/; Secure; SameSite=None; Partitioned`;
        }
      },
      tcData.listenerId
    )
  }
}
__tcfapi('addEventListener', 2, callback);

还有其他问题?

联系我们:support@uniconsent.com

掌握隐私与同意管理的最新动态

隐私法规、同意管理与合规动态,每月几封邮件。

无垃圾邮件,可随时退订。 隐私