Leading Consent Management Platform

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

UniConsent 标签实施

使用单一 UniConsent 标签快速安装(推荐)

在每个页面添加一个带有许可证密钥的 script 标签,尽量放在 <head> 的最前面。该标签会设置 Google Consent Mode 默认状态,立即定义 unicReady 和 unicUpdate,然后在后台加载完整的 CMP。

<script src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

您可以在安装与设置部分找到许可证密钥。

  • 单一标签取代了下文所述的 stub 标签、Consent Mode 默认状态标签和 cmp.js 加载器。请勿重复添加。
  • 它会设置 Consent Mode 默认值:所有存储均为 denied,wait_for_update 为 3 秒。如果您自行设置 Consent Mode 默认值,请放在该标签之后,否则会被覆盖。
  • 如果页面上已经存在 CMP 脚本,该标签会自动忽略自身。
  • 使用 unicReady 和 unicUpdate 在同意状态最终确定后运行您的代码。请参阅 Consent Ready API。

已经使用经典两段式标签安装了 UniConsent?它可以继续正常工作。请参阅本页底部的经典标签安装部分。

添加隐私设置链接(可选):

您可以关闭 Show Privacy Settings Button 并在页面上添加以下链接。

<a href="#" onclick='__unicapi("openunic");return false;'>隐私设置</a>

添加带有 CSS 规则的隐私设置链接:

您可以使用 CSS 选择器定位 Web 页面上的 DIV,UniConsent 会根据 GEO 位置和已启用的功能在 DIV 中自动插入隐私设置链接。

<div id="uniconsent-config"></div>

自动插入隐私设置链接:

您可以在 Web 页面的页脚放置以下占位符 DIV,UniConsent 会根据 GEO 位置和已启用的功能在占位符中自动插入隐私设置链接。

<div id="uniconsent-config"></div>

使用 WordPress 和 Drupal 插件安装:

您可以在安装与设置部分找到许可密钥。

WordPress WP Rocket 用户

避免延迟加载 CMP 标签:

您必须为所有脚本添加 data-nowprocket:

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

您还需要将内联 JavaScript 排除在合并之外:

  1. 登录 WordPress 管理后台 > 设置 > WP Rocket。
  2. 点击“文件优化”标签,向下滚动到“JavaScript 文件”部分。
  3. 压缩 JavaScript 文件 > 排除 JavaScript 文件:输入 cmp.uniconsent.com。

如果您需要 WP Consent API,请在页面上添加以下标签:

<script data-nowprocket data-cfasync="false" async src="http://cmp.uniconsent.com/v2/wp-consent-api.min.js"></script>

详细了解如何启用 WP Consent API。

Cloudflare Rocket Loader 用户

您必须为脚本添加 data-cfasync="false":

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

通过 Google Tag Manager 安装

通过 Google Tag Manager 安装 UniConsent 的说明请参见 https://www.uniconsent.com/docs/tutorials/gtm-integration。

性能提示:预连接 CMP 域名

在页面顶部的 <head> 中、UniConsent 标签之前添加以下标签。它们让浏览器提前解析 DNS 并与 cmp.uniconsent.com 建立连接,使 CMP 脚本更早开始下载,同意横幅显示得更快。

<link rel='dns-prefetch' href='//cmp.uniconsent.com' />
<link rel="preconnect" href="https://cmp.uniconsent.com" crossorigin>

如果您通过第一方 CMP 域名提供 CMP,请使用您的自定义域名代替 cmp.uniconsent.com。

为什么标签默认是同步加载的

单一标签特意不使用 async 加载。它会先于页面上的其他内容执行,因此 Google Consent Mode 默认状态、IAB TCF、USP 和 GPP stub,以及 unicReady 和 unicUpdate 函数都会在您的 Google 标签、广告标签或自有代码运行之前就绪。标签本身很小且会被缓存,完整的 CMP 在后台加载,因此对页面加载的影响极小。配合上面的预连接标签,可以最快显示横幅,同时避免标签在同意默认值设置之前触发。

如果您希望使用 async 加载标签

您可以为标签添加 async,但需注意以下几点:

  • 您的内联代码可能在标签执行之前运行,此时 unicReady 和 unicUpdate 可能尚不存在。请添加下面的两行队列保护代码,这样提前发出的调用会在标签加载后被重放。
  • Consent Mode 默认状态可能在您的 Google 标签加载之后才被设置。请在 Google 标签之前自行设置 Consent Mode 默认状态,或者在 unicReady 回调中加载您的 Google 标签。
  • IAB TCF stub 也是同样的情况。使用 async 时,您的广告标签或 Prebid 调用 __tcfapi 时它可能尚不存在。请在广告标签之前添加 stub 标签,或者在 unicReady 回调中加载广告标签。

使用 async 加载时如何与 API 集成

<script async src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>
<script>
  window.unicReady  = window.unicReady  || function (cb) { (window.unicReady.q  = window.unicReady.q  || []).push(cb); };
  window.unicUpdate = window.unicUpdate || function (cb) { (window.unicUpdate.q = window.unicUpdate.q || []).push(cb); };

  unicReady(function (status) {
    // 同意状态已最终确定:在此加载您的标签
  });

  unicUpdate(function (status) {
    // 每次同意状态变化
  });
</script>

您也可以监听 window 上的 unic:ready 和 unic:update DOM 事件(需在标签之前注册)。状态对象和完整细节请参阅 Consent Ready API。

下一步

如果您想集成您的 JavaScript 标签,请转到第三方 JavaScript 标签集成部分。

验证安装

您可以查看开发者控制台中的警告信息,或使用 UniConsent Consent Validator 确认 Consent Mode、IAB TCF 和 IAB GPP 数据在您的网页上可用并正确传递。

请定期在控制台中查看实施审核状态。

经典标签安装(现有安装)

本部分适用于在单一标签推出之前安装 UniConsent 的网站。经典两段式标签可继续正常工作,无需迁移。如需切换,请移除下方所有标签,并添加本页顶部的单一标签。

每个网站都有一个独特的 JavaScript Tag,需要安装在每个页面上。

您可以在安装与设置部分找到 UniConsent Tag。

Tag 有两个部分。第一部分是应嵌入页面顶部的 stub。第二部分可以使用 async 加载。对您的网站几乎没有性能影响。

stub 代码

您可以选择使用以下 JavaScript 标签加载 stub 代码:

<script src="https://cmp.uniconsent.com/v2/stub.min.js"></script>

或者用以下代码替换上述 stub 标签:

<script type="text/javascript">
!function(){var e,t,a;e="__tcfapiLocator",t=[],(a=window.frames[e])||(function t(){var n=window.document,i=!!a;if(!i)if(n.body){var o=n.createElement("iframe");o.style.cssText="display:none",o.name=e,n.body.appendChild(o)}else setTimeout(t,50);return!i}(),window.__tcfapi=function(){for(var e,a=[],n=0;n<arguments.length;n++)a[n]=arguments[n];if(!a.length)return t;if("setGdprApplies"===a[0])a.length>3&&2===parseInt(a[1],10)&&"boolean"==typeof a[3]&&(e=a[3],"function"==typeof a[2]&&a[2]("set",!0));else if("ping"===a[0]){var i={gdprApplies:e,cmpLoaded:!1,cmpStatus:"stub"};"function"==typeof a[2]&&a[2](i,!0)}else t.push(a)},window.addEventListener("message",(function(e){var t="string"==typeof e.data,a={};try{a=t?JSON.parse(e.data):e.data}catch(e){}var n=a.__tcfapiCall;if(n)try{window.__tcfapi(n.command,n.version,(function(a,i){var o={__tcfapiReturn:{returnValue:a,success:i,callId:n.callId}};t&&(o=JSON.stringify(o)),e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(o,"*")}),n.parameter)}catch(e){}}),!1))}();
!function(){var e,a,t;e="__uspapiLocator",a=[],(t=window.frames[e])||(function a(){var n=window.document,s=!!t;if(!t)if(n.body){var i=n.createElement("iframe");i.style.cssText="display:none",i.name=e,n.body.appendChild(i)}else setTimeout(a,50);return!s}(),window.__uspapi=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(!e.length)return a;"ping"===e[0]?"function"==typeof e[2]&&e[2]({cmpLoaded:!1,cmpStatus:"stub"},!0):a.push(e)},window.addEventListener("message",(function(e){var a="string"==typeof e.data,t={};try{t=a?JSON.parse(e.data):e.data}catch(e){}var n=t&&t.__uspapiCall;if(n)try{window.__uspapi(n.command,n.version,(function(t,s){var i={__uspapiReturn:{returnValue:t,success:s,callId:n.callId}};a&&(i=JSON.stringify(i)),e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(i,"*")}),n.parameter)}catch(e){}}),!1))}();
window.__gpp_addFrame=function(e){if(!window.frames[e])if(document.body){var t=document.createElement("iframe");t.style.cssText="display:none",t.name=e,document.body.appendChild(t)}else window.setTimeout(window.__gpp_addFrame,50,e)},window.__gpp_stub=function(){var e=arguments;if(__gpp.queue=__gpp.queue||[],__gpp.events=__gpp.events||[],!e.length||1==e.length&&"queue"==e[0])return __gpp.queue;if(1==e.length&&"events"==e[0])return __gpp.events;var t=e[0],p=e.length>1?e[1]:null,s=e.length>2?e[2]:null;if("ping"===t)p&&p({gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnat","8:usca","9:usva","10:usco","11:usut","12:usct","13:usfl","14:usmt","15:usor","16:ustx","17:usde","18:usia","19:usne","20:usnh","21:usnj","22:ustn","23:usmn","24:usmd","25:usin","26:usky","27:usri"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}},!0);else if("addEventListener"===t){"lastId"in __gpp||(__gpp.lastId=0),__gpp.lastId++;var n=__gpp.lastId;__gpp.events.push({id:n,callback:p,parameter:s}),p({eventName:"listenerRegistered",listenerId:n,data:!0,pingData:{gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnat","8:usca","9:usva","10:usco","11:usut","12:usct","13:usfl","14:usmt","15:usor","16:ustx","17:usde","18:usia","19:usne","20:usnh","21:usnj","22:ustn","23:usmn","24:usmd","25:usin","26:usky","27:usri"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}}},!0)}else if("removeEventListener"===t){for(var a=!1,i=0;i<__gpp.events.length;i++)if(__gpp.events[i].id==s){__gpp.events.splice(i,1),a=!0;break}p({eventName:"listenerRemoved",listenerId:s,data:a,pingData:{gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnat","8:usca","9:usva","10:usco","11:usut","12:usct","13:usfl","14:usmt","15:usor","16:ustx","17:usde","18:usia","19:usne","20:usnh","21:usnj","22:ustn","23:usmn","24:usmd","25:usin","26:usky","27:usri"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}}},!0)}else"hasSection"===t?p(!1,!0):"getSection"===t||"getField"===t?p(null,!0):__gpp.queue.push([].slice.apply(e))},window.__gpp_msghandler=function(e){var t="string"==typeof e.data;try{var p=t?JSON.parse(e.data):e.data}catch(e){p=null}if("object"==typeof p&&null!==p&&"__gppCall"in p){var s=p.__gppCall;try{window.__gpp(s.command,(function(p,n){var a={__gppReturn:{returnValue:p,success:n,callId:s.callId}};e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(t?JSON.stringify(a):a,"*")}),"parameter"in s?s.parameter:null,"version"in s?s.version:"1.1")}catch(e){}}},"__gpp"in window&&"function"==typeof window.__gpp||(window.__gpp=window.__gpp_stub,window.addEventListener("message",window.__gpp_msghandler,!1),window.__gpp_addFrame("__gppLocator"));
</script>

上述 stub 代码或标签支持以下 API:IAB TCF API、IAB GPP API、IAB USP API。

如果您未使用 Google Tag Manager,但希望启用 Consent Mode V2,请在页面顶部添加:

<script src="https://cmp.uniconsent.com/v2/stubgcm.min.js"></script>

或在页面顶部添加以下代码:

<script>
window.gtag||(window.dataLayer=window.dataLayer||[],window.gtag=function(){window.dataLayer.push(arguments)}),window.gtag("set","developer_id.dZTcxZD",!0),window.gtag("consent","default",{ad_storage:"denied",functionality_storage:"denied",personalization_storage:"denied",analytics_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",security_storage:"granted",wait_for_update:3e3}),window.gtag("set","ads_data_redaction",!0),window.gtag("set","url_passthrough",!1);
</script>

有关 Google Consent Mode 设置的更多信息,请参见 https://www.uniconsent.com/docs/tutorials/google-consent-mode

使用经典标签的 WP Rocket 和 Cloudflare Rocket Loader 用户

为两个脚本都添加 data-nowprocket 和 data-cfasync="false"。上面的 WP Rocket 排除设置同样适用。

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/stub.min.js"></script>
<script data-nowprocket data-cfasync="false" async src='https://cmp.uniconsent.com/v2/YOUR_ID/cmp.js'></script>
  1. 已启用 Google Consent Mode,但缺少默认状态 stub 代码

    问题: 您可能会在 Validator 或开发者控制台中看到以下错误信息:

    Error: Consent Mode Default Status is not added.

    解决方法: 在页面顶部添加默认状态 stub 代码即可解决。请按照以下说明操作:Consent Mode 默认状态标签安装。

  2. 已启用 Google Consent Mode,但默认状态 stub 代码添加在 Google 标签之后

    问题: 如果默认状态 stub 代码添加在 Google 标签之后,您可能会在 Validator 或开发者控制台中看到以下错误信息:

    Error: Your Google tags are loaded before Consent Mode Default Status and CMP.

    解决方法: 确保默认状态 stub 代码位于页面最顶部、所有 Google 标签之前。请参阅指南:Consent Mode 默认状态标签安装。

  3. 通过 Google Tag Manager 启用了 Google Consent Mode,但并非所有 Google 标签都由 Google Tag Manager 管理

    问题: 当部分 Google 标签不由 Google Tag Manager 管理时,您可能会看到与上述类似的错误。

    解决方法: 在页面顶部添加默认状态 stub 代码,或确保所有 Google 标签都由 Google Tag Manager 管理。请参阅指南:Consent Mode 默认状态标签安装。

  4. 缺少 IAB TCF stub 代码

    问题: 未包含 IAB TCF stub 代码,可能导致错误。

    解决方法: 在页面顶部添加 IAB TCF stub 代码。请按照以下说明操作:stub 代码安装。

  5. 缺少 IAB GPP stub 代码

    问题: 您的实施中缺少 IAB GPP stub 代码。

    解决方法: 在页面顶部添加 IAB GPP stub 代码。相关指南请见:stub 代码安装。

  6. 缺少 USP API stub 代码

    问题: 未包含 IAB USP stub 代码。

    解决方法: 在页面顶部添加 IAB USP stub 代码。详细说明请见:stub 代码安装。