使用 UnicScript 管理页面元素和代码

您可以使用 UnicScript 管理页面元素,根据用户对特定目的 ID 或自定义供应商 ID 的同意来加载代码,这些 ID 由 IAB TCF 2.3 目的或 Google Consent Mode 类型定义。

IAB TCF 2.3 目的 ID

目的 ID描述
1在设备上存储和/或访问信息
2使用有限数据选择广告
3创建个性化广告档案
4使用档案选择个性化广告
5创建个性化内容档案
6使用档案选择个性化内容
7衡量广告效果
8衡量内容效果
9通过统计数据或来自不同来源的数据组合来了解受众
10开发和改进服务
11使用有限数据选择内容

基于目的 ID 同意加载内联 JavaScript

您可以根据特定目的 ID 的同意动态加载内联 JavaScript。

示例 1:为单个目的 ID 加载脚本

<script type="text/unicscript" unic-purpose-id="1">
  console.log('Consent granted for purpose 1');
</script>

示例 2:使用目的 ID 加载外部脚本

<script type="text/unicscript" unic-purpose-id="1" src="path-to-external-script.js"></script>

基于多个目的 ID 加载内联 JavaScript

示例:为多个目的 ID 加载脚本

<script type="text/unicscript" unic-purpose-ids="1,2,3">
  console.log('Consent granted for purposes 1, 2, and 3');
</script>

基于自定义供应商 ID 同意加载内联 JavaScript

示例:

<script type="text/unicscript" unic-vendor-id="1">
  console.log('hello vendor 1');
</script>

示例 2:

<script type="text/unicscript" unic-vendor-id="1" src="xxxxx"></script>

基于目的 ID 同意加载内联 Iframe

您可以根据特定目的 ID 的同意动态加载 iframe。

示例:为单个目的 ID 加载 iframe

<iframe
  data-unicscript
  unic-purpose-id="1"
  width="560"
  height="315"
  data-src="https://www.youtube.com/embed/XXXX"
  frameborder="0"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
></iframe>

基于多个目的 ID 加载内联 Iframe

示例:为多个目的 ID 加载 iframe

<iframe
  data-unicscript
  unic-purpose-ids="1,2,3"
  width="560"
  height="315"
  data-src="https://www.youtube.com/embed/XXXX"
  frameborder="0"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
></iframe>

基于自定义供应商 ID 同意加载内联 Iframe

示例:为单个供应商 ID 加载 iframe

<iframe
  unic-vendor-id="1"
  width="560"
  height="315"
  data-src="https://www.youtube.com/embed/XXXX"
  frameborder="0"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
></iframe>

基于自定义目的 ID 加载内联 JavaScript

您可以使用 unic-cp-idunic-cp-ids 属性,根据 Custom Purposes 的同意来加载脚本和 iframe。

示例 1:为单个自定义目的 ID 加载脚本

<script type="text/unicscript" unic-cp-id="custom_analytics">
  console.log('Custom analytics consent granted');
</script>

示例 2:为自定义目的 ID 加载外部脚本

<script type="text/unicscript" unic-cp-id="custom_analytics" src="https://example.com/analytics.js"></script>

示例 3:为多个自定义目的 ID 加载脚本(所有目的都必须已同意)

<script type="text/unicscript" unic-cp-ids="custom_analytics,ab_testing">
  console.log('Both custom purposes consented');
</script>

示例 4:为自定义目的 ID 加载 iframe

<iframe
  data-unicscript
  unic-cp-id="custom_analytics"
  width="560"
  height="315"
  data-src="https://example.com/widget"
  frameborder="0"
></iframe>

本节介绍如何根据 Google Consent Mode 中的同意类型加载内联 JavaScript,并与 UniConsent 的 Simple Mode 同意类型相匹配。

下表展示了各种 Google Consent Mode 同意类型如何映射到 UniConsent Simple Mode 同意类型及其各自用途:

Google Consent 类型UniConsent Simple Mode 类型用途
ad_storageTargeting and Advertising启用广告 Cookie。
analytics_storagePerformance启用分析 Cookie。
functionality_storageFunctionality启用网站功能和设置的功能性 Cookie。
personalization_storageFunctionality启用用户个性化的功能性 Cookie。
security_storageStrictly Necessary启用网站安全、保护和界面偏好所必需的 Cookie。这些 Cookie 不需要同意。
ad_user_dataTargeting and Advertising启用广告 Cookie 并设置向 Google 核心服务发送个人数据的同意。
ad_personalizationTargeting and Advertising启用广告 Cookie 以用于广告个性化中的数据使用,例如再营销。

示例:加载内联 JavaScript

使用以下格式根据用户同意加载内联 JavaScript:

<script type="text/unicscript" unic-gcm-id="functionality_storage">
  console.log('Consent for functionality storage is granted, script is executed.');
</script>

您可以使用 unic-gcm-ids 属性指定多个同意类型,如下所示:

<script type="text/unicscript" unic-gcm-ids="analytics_storage,ad_user_data">
  console.log('Consent for analytics storage and ad user data is granted, script is executed.');
</script>

示例:基于同意加载 Iframe

您也可以将同意设置应用于 iframe。以下是示例:

<iframe data-unicscript unic-gcm-ids="functionality_storage,ad_personalization"
  width="560" height="315"
  data-src="https://www.youtube.com/embed/XXXXXXX"
  frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen>
</iframe>

此示例确保 iframe 内容仅在用户同意指定类别(functionality_storagead_personalization)后才加载。

使用 UnicScript 管理 Google Adsense

将:

<script
  async
  src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
  type="text/javascript"
></script>

修改为:

<script
  async
  src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
  unic-purpose-id="3"
  type="text/unicscript"
></script>

修改页面上添加的 Google Adsense 广告位代码,将:

<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXX" data-ad-slot="XXXXXXX" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script type="text/javascript">
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

修改为:

<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXX" data-ad-slot="XXXXXXX" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script type="text/unicscript" unic-purpose-id="3" >
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

还有其他问题?

联系我们:support@uniconsent.com