隐私权和消息 JavaScript API

简介

此 API 提供了一些工具,可用于与“隐私权和消息”标签页提供的消息进行互动。借助该控制台,您可以执行以下操作:

等等。

您还可以使用以下工具,通过业界标准协议征求用户意见:

在这些情况下,系统会通过这些 API 传达用户同意情况。

您可以通过以下几种方式在网站上部署此用户消息功能:

  1. 在大多数情况下,您根本无需重新添加代码 - 在相关产品中发布用户消息后,现有的 Google 发布商代码AdSense 代码就会部署用户消息。
  2. 如果您要使用广告拦截收入挽回消息,则需要明确向您的网页添加广告拦截代码。如需了解详情,请参阅 Ad ManagerAdSense 代码植入说明。

googlefc 是用户消息功能在 JavaScript Window 上为其 API 使用的全局命名空间。

字段摘要

名称 类型 定义
googlefc.controlledMessagingFunction function(!Object) 用于确定是否继续发送任何消息的函数。所有消息类型都支持此功能。
googlefc.callbackQueue !Array<!Object<string, function()>> | !Array<function()> | !googlefc.CallbackQueue 对用于异步执行用户消息查询的回调队列的引用。
googlefc.CallbackQueue !Object 回调队列对象的类型。
googlefc.AdBlockerStatusEnum !Object<string, number> 用于表示用户的广告拦截器状态的枚举。
googlefc.AllowAdsStatusEnum !Object<string, number> 用于表示用户的允许广告状态的枚举。
googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum !Object<string, number> 一个枚举,用于表示用户在美国各州的初始选择不出售/分享个人信息状态。此值会考虑用户所在的美国州。
googlefc.GoogleFcConsentModeUserStatus !Object googlefc.getGoogleConsentModeValues 的返回类型。
googlefc.ConsentModePurposeStatusEnum !Object<string, number> 一个枚举,用于表示最终用户针对意见征求模式目的做出的决定。
googlefc.usstatesoptout.overrideDnsLink undefined|boolean 一个布尔值,可设置为 true 以使用您自己的自定义“不出售或分享”链接。
googlefc.ccpa.InitialCcpaStatusEnum

旧版。首选 googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum
!Object<string, number> 一个枚举,用于表示用户的初始美国州级法规状态。
googlefc.ccpa.overrideDnsLink

旧版。首选 googlefc.usstatesoptout.overrideDnsLink
undefined|boolean 一个布尔值,可设置为 true 以使用您自己的自定义“不出售或分享”链接。

方法摘要

名称 返回类型 定义
googlefc.showRevocationMessage() undefined 清除意见征求记录并重新加载 googlefc 脚本,以显示适用于用户的意见征求消息。
googlefc.getAdBlockerStatus() 编号 根据用户的广告拦截状态,在 AdBlockerStatusEnum 中返回一个值。
googlefc.getAllowAdsStatus() 编号 根据用户的允许广告状态,在 AllowAdsStatusEnum 中返回一个值。
googlefc.usstatesoptout.getInitialUsStatesOptOutStatus() 编号 根据用户的初始美国州级法规选择不出售/分享个人信息状态,在 InitialUsStatesOptOutStatusEnum 中返回一个值。这会考虑根据用户当前所在的位置而适用的法规。
googlefc.usstatesoptout.openConfirmationDialog(function(boolean)) undefined 如果默认的“不出售/分享”链接被替换,系统会打开美国州级法规“不出售/分享”功能停用确认对话框。
googlefc.getGoogleConsentModeValues() !Object 返回一个 googlefc.GoogleFcConsentModeUserStatus 对象,其中包含用户的当前意见征求模式值,每个可用的意见征求模式用途对应一个值。
googlefc.ccpa.getInitialCcpaStatus()

旧版。首选 googlefc.usstatesoptout.getInitialUsStatesOptOutStatus()
编号 根据用户的初始美国州级法规选择停用状态,在 InitialCcpaStatusEnum 中返回一个值。
googlefc.ccpa.openConfirmationDialog(function(boolean))

旧版。首选 googlefc.usstatesoptout.openConfirmationDialog()
undefined 如果默认的“不出售/分享”链接被替换,系统会打开“不出售/分享”功能的美国州级法规选择停用确认对话框。

在您的网站上进行测试和调试

“隐私权和消息”工具提供调试和测试功能,可让您查看特定消息(或消息组合)在实际网站上的显示效果。

前提条件:

  • 您要预览的消息必须发布在您要测试的网站下

您可以使用以下调试网址参数在自己的网站上查看实时预览:

调试参数 允许使用的值
fc alwaysshow(用于触发调试/预览模式)
fctype ab(广告拦截消息)、ccpa(美国州级法规选择停用消息)、gdpr(GDPR 意见征求消息)、monetization(积分墙消息)

以下示例展示了如何使用此方法在您的网站 (foo.com) 上进行预览:

  • 测试美国州级法规消息 - http://foo.com/?fc=alwaysshow&fctype=ccpa
  • 测试 GDPR 消息 - http://foo.com/?fc=alwaysshow&fctype=gdpr

字段:说明和示例

googlefc.controlledMessagingFunction {function(!Object)}

用于确定是否应显示消息的函数。它可用于根据发布商指定的条件(例如订阅者状态或网页网址)控制消息呈现。

如果您在其他脚本加载之前在窗口上定义 googlefc.controlledMessagingFunction,则系统不会在您调用 message.proceed(boolean) 之前显示消息。调用 message.proceed(true) 可让消息照常发送,而调用 message.proceed(false) 则会阻止针对网页浏览显示任何消息。

示例:假设您在网页上使用以下脚本,其中定义了一个异步函数 determineIfUserIsSubscriber(),用于检查已登录的用户是否为订阅者。

<head>
  <script>
    window.isSubscriber = undefined;
    function determineIfUserIsSubscriber() {
      if (isSubscriber !== undefined) {
        return isSubscriber;
      }
      return new Promise(resolve => {
        setTimeout(() => {
          // Change this to true if you want to test what subscribers would see.
          window.isSubscriber = false;
          resolve(window.isSubscriber);
        }, 1000);
      });
    }
  </script>
</head>

以下示例展示了如何使用 googlefc.controlledMessagingFunction 仅向非订阅者显示消息。

<head>
  <script>
    // Define googlefc and the controlled messaging function on the Window.
    window.googlefc = window.googlefc || {};
    googlefc.controlledMessagingFunction = async (message) => {
      // Determine if the user is a subscriber asynchronously.
      const isSubscriber = await determineIfUserIsSubscriber();

      if (isSubscriber) {
        // If the user is a subscriber, don't show any messages.
        message.proceed(false);
      } else {
        // Otherwise, show messages as usual.
        message.proceed(true);
      }
    }
  </script>
</head>

此功能还有一项扩展功能,可让发布商指定仅抑制积分墙。使用此功能扩展可在不屏蔽其他消息类型的情况下屏蔽积分墙消息。

通过向 message.proceed() 传递一个额外的参数(类型为 googlefc.MessageTypeEnumArray)来实现积分墙专用受控消息传递。

示例:以下示例展示了如何使用 googlefc.controlledMessagingFunction 仅针对订阅者禁止投放积分墙,而不禁止其他消息类型:

<head>
  <script>
    // Define googlefc and the controlled messaging function on the Window.
    window.googlefc = window.googlefc || {};
    googlefc.controlledMessagingFunction = async (message) => {
     // Determine if the Offerwall should display or not.
     const shouldDisplayOfferwall = await determineIfUserIsSubscriber();
     const applicableMessageTypes = [];

     if (!shouldDisplayOfferwall) {
       // Do not show the Offerwall, but allow other message types to display.
       applicableMessageTypes.push(window.googlefc.MessageTypeEnum.OFFERWALL);
       message.proceed(false, applicableMessageTypes);
     } else {
       // Otherwise, show messages as usual.
       message.proceed(true);
     }
    }
  </script>
</head>

googlefc.callbackQueue {!Array<!Object<string, function()>> | !Array<function()> | !googlefc.CallbackQueue}

对全局回调队列的引用,用于异步执行与消息传递相关的调用。调用任何函数的唯一支持方式是将其添加到 callbackQueue

由于不同类型的数据会在不同时间可用,因此应将函数添加为映射,其中以下字符串之一作为键,要执行的函数作为值。

支持的按键:

密钥名称 用法 相对延迟时间
CONSENT_API_READY 当定义并可调用受支持意见征求框架的 API 时,系统会执行使用 CONSENT_API_READY 键推送到回调队列的函数。从此时起,后续添加的任何 CONSENT_API_READY 键值函数的执行都是同步的。如需了解特定于框架的详细信息,请参阅 IAB 框架部分。
CONSENT_DATA_READY 当系统知道在受支持的意见征求框架下收集的用户意见(通过之前的执行或用户与意见征求消息互动后得知)时,系统会执行使用 CONSENT_DATA_READY 键推送到回调队列的函数。从此时起,后续添加的任何 CONSENT_DATA_READY 键值函数的执行都是同步的。
AD_BLOCK_DATA_READY 当广告拦截数据在流程中可用时,系统会执行使用 AD_BLOCK_DATA_READY 键推送到回调队列的函数。从此时起,后续添加的任何 AD_BLOCK_DATA_READY 键值函数的执行都是同步的。
CONSENT_MODE_DATA_READY 当 Google [意见征求模式](https://support.google.com/google-ads/answer/10000067) 数据(用于与 Google Ads 和 Google Analytics 代码搭配使用)在流程中可用时,系统会执行使用 CONSENT_MODE_DATA_READY 键推送到回调队列的函数。意见征求模式数据准备就绪后,您可以随时使用 googlefc.getGoogleConsentModeValues 访问意见征求模式值。
INITIAL_US_STATES_OPT_OUT_DATA_READY 当流程中有美国州级法规数据可用时,系统会执行使用 INITIAL_US_STATES_OPT_OUT_DATA_READY 键推送到回调队列的函数。请注意,您应通过直接调用 GPP API (__gpp) 来获取任何后续的美国州级法规数据请求。
INITIAL_CCPA_DATA_READY 美国州级法规的旧版键。首选 INITIAL_US_STATES_OPT_OUT_DATA_READY

当流程中有美国州级法规数据可用时,系统会执行使用 INITIAL_CCPA_DATA_READY 键推送到回调队列的函数。请注意,您应通过直接调用 GPP API (__gpp) 来获取任何后续的美国州级法规数据请求。

googlefc.CallbackQueue {!Object}

方法摘要:

名称 类型 参数 返回类型 角色
push(data) 编号 data:键值对,其中键为数据可用性类型之一,值为要执行的 JavaScript 函数。可接受的数据可用性键包括 CONSENT_API_READYCONSENT_DATA_READYAD_BLOCK_DATA_READYINITIAL_US_STATES_OPT_OUT_DATA_READYCONSENT_MODE_DATA_READY 和(旧版)INITIAL_CCPA_DATA_READY 到目前为止添加的命令数量。这会返回数组的当前长度。 按照数据可用性顺序(然后按照这些函数添加到队列的顺序)执行传入的函数。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  googlefc.callbackQueue.push({
    'AD_BLOCK_DATA_READY':
    () => {
      if (googlefc.getAdBlockerStatus() == googlefc.AdBlockerStatusEnum.NO_AD_BLOCKER) {
        // Handle a non-ad blocking user.
      }
    }
  });
</script>

googlefc.AdBlockerStatusEnum {!Object<string, number>}

表示用户的不同广告拦截状态。不同的状态包括:

googlefc.AdBlockerStatusEnum = {
  // Something failed, in an unknown state.
  UNKNOWN: 0,
  // The user was running an extension level ad blocker.
  EXTENSION_AD_BLOCKER: 1,
  // The user was running a network level ad blocker.
  NETWORK_LEVEL_AD_BLOCKER: 2,
  // The user was not blocking ads.
  NO_AD_BLOCKER: 3,
};

googlefc.AllowAdsStatusEnum {!Object<string, number>}

表示用户的不同广告拦截允许展示广告状态。不同的状态包括:

googlefc.AllowAdsStatusEnum = {
  // Something failed, in an unknown state.
  UNKNOWN: 0,
  // User is currently using an ad blocker, was never using an ad blocker, or
  // allowed ads, but not because they saw the Privacy & messaging message.
  ADS_NOT_ALLOWED: 1,
  // User is no longer using an ad blocker after seeing the ad blocking message.
  ADS_ALLOWED: 2,
};

googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum{!Object<string, number>}

表示用户选择不出售/分享个人信息的不同美国州级法规状态。不同的状态如下:

googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum = {
  // Something failed, status unknown.
  UNKNOWN: 0,
  // No US state regulation applies to this user.
  DOES_NOT_APPLY: 1,
  // A US state regulation applies to this user, and the user has not opted out yet.
  NOT_OPTED_OUT: 2,
  // A US state regulation applies to this user, and the user has opted out.
  OPTED_OUT: 3,
};

googlefc.GoogleFcConsentModeUserStatus{!Object}

googlefc.getGoogleConsentModeValues 返回的对象的类型。

interface GoogleFcConsentModeUserStatus {

  // End user consent decision value for the ad_storage consent mode purpose.
  adStoragePurposeConsentStatus: number;

  // End user consent decision value for the ad_user_data consent mode purpose.
  adUserDataPurposeConsentStatus: number;

  // End user consent decision value for the ad_personalization consent mode purpose.
  adPersonalizationPurposeConsentStatus: number;

  // End user consent decision value for the analytics_storage consent mode purpose.
  analyticsStoragePurposeConsentStatus: number;
}

每个字段的值都是一个数字,对应于 googlefc.ConsentModePurposeStatusEnum 枚举值。


googlefc.ConsentModePurposeStatusEnum{!Object<string, number>}

表示用于同意模式用途的不同可能的最终用户意见征求值。不同的值如下:

googlefc.ConsentModePurposeStatusEnum = {
  // Indicates either an error state, or that consent mode data is not ready
  // yet.
  UNKNOWN: 0,
  // Consent is granted for the given consent mode purpose.
  GRANTED: 1,
  // Consent is denied for the given consent mode purpose.
  DENIED: 2,
  // Consent is not applicable for the given consent mode purpose.
  NOT_APPLICABLE: 3,
  // The consent mode purpose has not been configured for use in the Privacy &
  // messaging UI.
  NOT_CONFIGURED: 4
};

googlefc.usstatesoptout.overrideDnsLink{undefined|boolean}

将此字段设置为 true 可隐藏默认的“不出售/分享我的个人信息”链接,并使用您自己的自定义“不出售/分享我的个人信息”链接。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  // Signals that the default DNS link will be overridden.
  googlefc.usstatesoptout.overrideDnsLink = true;
</script>

googlefc.ccpa.InitialCcpaStatusEnum{!Object<string, number>}

表示用户选择不出售/分享个人信息的不同美国州级法规状态。不同的状态如下:

googlefc.ccpa.InitialCcpaStatusEnum = {
  // Something failed, in an unknown state.
  UNKNOWN: 0,
  // No US state regulation applies to this user.
  CCPA_DOES_NOT_APPLY: 1,
  // A US state regulation applies to this user, and the user has not opted out yet.
  NOT_OPTED_OUT: 2,
  // A US state regulation applies to this user, and the user has opted out.
  OPTED_OUT: 3,
};

googlefc.ccpa.overrideDnsLink{undefined|boolean}

将此字段设置为 true 可隐藏默认的“不出售/分享我的个人信息”链接,并使用您自己的自定义“不出售/分享我的个人信息”链接。请注意,如果您将此字段设为 true,则有责任在您的网站上呈现“请勿出售或分享”链接。此字段应与 openConfirmationDialog 结合使用。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.ccpa = window.googlefc.ccpa || {};
  // Signals that the default DNS link will be overridden.
  googlefc.ccpa.overrideDnsLink = true;
</script>

方法:说明和示例

googlefc.getConsentStatus(): {number}


googlefc.getConsentedProviderIds(): {!Array<string>}

  1. 现在,调用此方法时,它始终会返回空列表。

googlefc.showRevocationMessage(): {undefined}

清除当前意见征求记录,并显示适用于此用户的意见征求消息。应为此函数指定的键为 CONSENT_DATA_READY

示例:

<button type="button" onclick="googlefc.callbackQueue.push({'CONSENT_DATA_READY': () => googlefc.showRevocationMessage()});">
  Click here to revoke
</button>

googlefc.getAdBlockerStatus(): {number}

根据用户的广告拦截状态,返回 AdBlockerStatusEnum 中的值。应为此函数指定的键为 AD_BLOCK_DATA_READY

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  googlefc.callbackQueue.push({
    'AD_BLOCK_DATA_READY':
    () => {
      switch (googlefc.getAdBlockerStatus()) {
          case googlefc.AdBlockerStatusEnum.EXTENSION_LEVEL_AD_BLOCKER:
          case googlefc.AdBlockerStatusEnum.NETWORK_LEVEL_AD_BLOCKER:
            // Insert handling for cases where the user is blocking ads.
            break;
          case googlefc.AdBlockerStatusEnum.NO_AD_BLOCKER:
            // Insert handling for cases where the user is not blocking ads.
            break;
          case googlefc.AdBlockerStatusEnum.UNKNOWN:
            // Insert handling for unknown cases.
            break;
      }
    }
  });
</script>

googlefc.getAllowAdsStatus(): {number}

根据用户的广告允许状态,在 AllowAdsStatusEnum 中返回一个值。应为此函数指定的键为 AD_BLOCK_DATA_READY

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  googlefc.callbackQueue.push({
    'AD_BLOCK_DATA_READY':
    () => {
      switch (googlefc.getAllowAdsStatus()) {
        case googlefc.AllowAdsStatusEnum.ADS_NOT_ALLOWED:
          // Insert handling for cases where the user has not allowed ads.
          // The user may have never been an ad blocker.
          break;
        case googlefc.AllowAdsStatusEnum.ADS_ALLOWED:
          // Insert handling for cases where the user saw the ad blocking
          // message and allowed ads on the site.
          break;
        case googlefc.AllowAdsStatusEnum.UNKNOWN:
          // Insert handling for unknown cases.
          break;
      }
    }
  });
</script>

googlefc.usstatesoptout.getInitialUsStatesOptOutStatus(): {number}

根据用户选择不出售/分享个人信息的美国州级法规状态,在 InitialUsStatesOptOutStatusEnum 中返回一个值。应为此函数指定的键为 INITIAL_US_STATES_OPT_OUT_DATA_READY。请注意,您应通过直接调用 GPP API (__gpp) 来获取任何后续的美国州级法规数据请求。

如果您要替换“不出售或分享”链接,可以使用此方法确定何时在您的网站上添加该链接。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {}
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  googlefc.callbackQueue.push({
    'INITIAL_US_STATES_OPT_OUT_DATA_READY':
    () => {
      switch (googlefc.usstatesoptout.getInitialUsStatesOptOutStatus()) {
        case googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum.DOES_NOT_APPLY:
          // Insert handling for cases where no US state regulation applies to
          // the user.
          break;
        case googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum.NOT_OPTED_OUT:
          // Insert handling for cases where a US state regulation applies to
          // the user, and the user has not opted out.
          break;
        case googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum.OPTED_OUT:
          // Insert handling for cases where a US state regulation applies to the
          // user, and the user has opted out.
          break;
      }
    }
  });
</script>

googlefc.usstatesoptout.openConfirmationDialog(function(boolean)): {undefined}

如果默认的“不出售”链接被替换,系统会打开美国州级法规选择停用确认对话框。用户与确认对话框互动后,如果用户决定停用,系统会使用 true 调用所提供的回调函数;否则,系统会使用 false 调用该函数。

示例:

<script>
// This callback will be called with the user's US state regulation opt-out
// decision.
const usStateRegCompletionCallback = (userOptedOut) => {
  // Insert handling for user opt-out status here.
}
// Invoke the US state regulations confirmation dialog when the user clicks the
// link.
document.getElementById("your-custom-do-not-sell-link").addEventListener(
  "click", () => googlefc.usstatesoptout.openConfirmationDialog(usStateRegCompletionCallback));
</script>

googlefc.getGoogleConsentModeValues(): {!Object}

根据用户的意见征求决定,返回一个 googlefc.GoogleFcConsentModeUserStatus 对象,其中包含每个意见征求模式用途的当前值。

如需了解预期用途,请参阅在遵守欧盟法规的前提下使用支持意见征求模式的 Google 意见征求管理解决方案


googlefc.ccpa.getInitialCcpaStatus(): {number}

根据用户的美国州级法规选择停用状态,在 InitialCcpaStatusEnum 中返回一个值。应为此函数指定的键为 INITIAL_CCPA_DATA_READY。请注意,如需获取任何后续的美国州级法规数据请求,应通过直接调用 GPP API (__gpp) 来获取。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.ccpa = window.googlefc.ccpa || {}
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  googlefc.callbackQueue.push({
    'INITIAL_CCPA_DATA_READY':
    () => {
      switch (googlefc.ccpa.getInitialCcpaStatus()) {
        case googlefc.ccpa.InitialCcpaStatusEnum.CCPA_DOES_NOT_APPLY:
          // Insert handling for cases where no US state regulation applies to
          // the user.
          break;
        case googlefc.ccpa.InitialCcpaStatusEnum.NOT_OPTED_OUT:
          // Insert handling for cases where a US state regulation applies to
          // the user, and the user has not opted out.
          break;
        case googlefc.ccpa.InitialCcpaStatusEnum.OPTED_OUT:
          // Insert handling for cases where a US state regulation applies to the
          // user, and the user has opted out.
          break;
      }
    }
  });
</script>

googlefc.ccpa.openConfirmationDialog(function(boolean)): {undefined}

如果默认的“不出售”链接被替换,系统会打开美国州级法规选择停用确认对话框。用户与确认对话框互动后,如果用户决定停用,系统会使用 true 调用所提供的回调函数;否则,系统会使用 false 调用该函数。

示例:

<script>
// This callback will be called with the user's US state regulation opt-out
// decision.
const usStateRegCompletionCallback = (userOptedOut) => {
  // Insert handling for user opt-out status here.
}
// Invoke the US state regulations confirmation dialog when the user clicks the
// link.
document.getElementById("your-custom-ccpa-do-not-sell-link").addEventListener(
  "click", () => googlefc.ccpa.openConfirmationDialog(ccpaCompletionCallback));
</script>

如果您使用 Google 意见征求管理解决方案在 IAB TCF v2 框架下征求 GDPR 用户意见,则应使用 IAB TCF v2 API

您可以使用 CONSENT_API_READY 回调队列键,确保仅在网页上定义 IAB TCF v2 API 时才调用相应的回调。此方法应与 IAB TCF v2 API 的 'addEventListener' 命令结合使用。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback using the CONSENT_API_READY key on the callbackQueue.
  window.googlefc.callbackQueue.push({
    'CONSENT_API_READY':
    () => __tcfapi('addEventListener', 2.2, (data, success) => {
      // Do something with consent data value; this callback may be invoked
      // multiple times as user completes consent flow.
    })
  });
</script>

您可以使用 CONSENT_DATA_READY 回调队列键,确保仅在使用 IAB TCF v2 API 收集用户意见并可访问时调用相应的回调。此方法可与 'addEventListener' 命令结合使用 - 在首次调用您提供的回调时提供的数据将包含用户的意见选择(前提是 TCF v2 适用于此用户)。请注意,随着 TCF v2.2 的发布,'getTCData' 命令现已废弃。

示例:

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback using the CONSENT_DATA_READY key on the callbackQueue.
  window.googlefc.callbackQueue.push({
    'CONSENT_DATA_READY':
    () => __tcfapi('addEventListener', 2.2, (data, success) => {
      // Do something with consent data value; this callback may be invoked
      // multiple times if user consent selections change.
    })
  });
</script>

Google 意见征求管理解决方案可解读用户在Google 意见征求模式中针对欧盟法规的意见征求选项(如需了解详情,请参阅帮助中心)。

您可以通过基本模式高级模式实现意见征求模式,如 Google Ads 和 Google Analytics 文档中所述。您应咨询法务部门,了解应采用哪种意见征求模式来满足您的法律要求。

系统默认支持高级意见征求模式;在“隐私权和消息”界面中启用意见征求模式后,您无需执行任何其他操作。

如需使用 Google 意见征求管理解决方案实现基本意见征求模式,您可以在意见征求模式数据可用后使用 CONSENT_MODE_DATA_READY 回调队列键有条件地加载 Google Ads 和 Google Analytics 代码。在资金选择工具确定意见征求模式不适用于此请求(例如,因为欧盟法规不适用于此请求)或用户做出欧盟法规意见征求决定后,系统才会提供意见征求模式数据。您应咨询法务部门,了解确定在意见征求模式可用后是否可以加载代码的标准。

例如,如需在意见征求模式数据可用后加载代码(无论最终用户的意见征求决定如何),请执行以下操作:

<script>
// Make sure that the properties exist on the window.
window.googlefc = window.googlefc || {};
window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

// Helper function to load Google Ads/Analytics tags once consent mode data is
// ready.
const loadGtagScript = () => {
  // Load gtag.js script - code taken from
  // https://developers.google.com/tag-platform/security/guides/consent?consentmode=basic#set_up_consent_mode
  var gtagScript = document.createElement('script');
  gtagScript.async = true;
  gtagScript.src = 'https://www.googletagmanager.com/gtag/js?id=<Google tag ID>';

  var firstScript = document.getElementsByTagName('script')[0];
  firstScript.parentNode.insertBefore(gtagScript,firstScript);
}

// Queue the callback using the CONSENT_MODE_DATA_READY key on the callbackQueue.
window.googlefc.callbackQueue.push({
  'CONSENT_MODE_DATA_READY':
  () => {
      loadGtagScript();
  },
});
</script>

当有意见征求模式数据可用时,您还可以使用 googlefc.getGoogleConsentModeValues() API 获取各个意见征求模式用途的值。此 API 会返回一个 GoogleFcConsentModeUserStatus 对象,其中包含每个受支持的意见征求模式用途对应的一个字段,每个字段的值都是一个枚举值,表示该意见征求模式用途的值。

例如,只有在以下任一情况下,您才能使用 googlefc.getGoogleConsentModeValues() 解除屏蔽 Google Ads 和 Google Analytics 代码:

  • 最终用户做出的欧盟法规意见征求决定导致其同意所有意见征求模式用途,或者
  • 所有意见征求模式用途均不适用于当前请求(如果欧盟法规不适用,或者未在“隐私权和消息”中针对一个或多个用途配置意见征求模式,就可能会出现这种情况)。
<script>
// Make sure that the properties exist on the window.
window.googlefc = window.googlefc || {};
window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

// Helper function to determine whether Google Ads and Analytics tags can be
// unblocked. Returns true if all consent mode purposes are set to GRANTED,
// NOT_APPLICABLE, or NOT_CONFIGURED.
const shouldUnblockConsentTags = (googleFcConsentModeStatus) => {
  const allConsentModeValues = [
    googleFcConsentModeStatus.adStoragePurposeConsentStatus,
    googleFcConsentModeStatus.adUserDataPurposeConsentStatus,
    googleFcConsentModeStatus.adPersonalizationPurposeConsentStatus,
    googleFcConsentModeStatus.analyticsStoragePurposeConsentStatus
  ];
  for (const consentModeValue of allConsentModeValues) {
    switch (consentModeValue) {
      case googlefc.ConsentModePurposeStatusEnum.CONSENT_MODE_PURPOSE_STATUS_UNKNOWN:
        // Indicates either an error case or that consent mode data is not
        // ready yet. Cannot unblock tags until consent data is ready and valid,
        // so return false.
        return false;
      case googlefc.ConsentModePurposeStatusEnum.CONSENT_MODE_PURPOSE_STATUS_GRANTED:
        // Consent is granted for this consent mode purpose.
        break;
      case googlefc.ConsentModePurposeStatusEnum.CONSENT_MODE_PURPOSE_STATUS_DENIED:
        // Consent is denied for this consent mode purpose. Do not unblock tags.
        return false;
      case googlefc.ConsentModePurposeStatusEnum.CONSENT_MODE_PURPOSE_STATUS_NOT_APPLICABLE:
        // Consent mode does not apply for this purpose.
        break;
      case googlefc.ConsentModePurposeStatusEnum.CONSENT_MODE_PURPOSE_STATUS_NOT_CONFIGURED:
        // Consent mode not configured for this purpose.
        // If you configured support for Ads purposes but not Analytics purposes in the
        // Privacy & messaging UI, the value of `analyticsStoragePurposeConsentStatus` will
        // always be set to NOT_CONFIGURED. If you do not enable any Consent Mode support
        // in the Privacy & messaging UI, the values of all purposes will always be set to
        // NOT_CONFIGURED.
        break;
      default:
        console.log("Unexpected consent mode value encountered");
    }
  }
  // If all prior checks pass, all consent mode values are either GRANTED,
  // NOT_APPLICABLE, or NOT_CONFIGURED.
  return true;
};

// Helper function to load Google Ads/Analytics tags.
const loadGtagScript = () => {
  // Load gtag.js script - code taken from
  // https://developers.google.com/tag-platform/security/guides/consent?consentmode=basic#set_up_consent_mode
  var gtagScript = document.createElement('script');
  gtagScript.async = true;
  gtagScript.src = 'https://www.googletagmanager.com/gtag/js?id=<Google tag ID>';

  var firstScript = document.getElementsByTagName('script')[0];
  firstScript.parentNode.insertBefore(gtagScript,firstScript);
}

googlefc.callbackQueue.push({
  CONSENT_MODE_DATA_READY: () => {
    if (shouldUnblockConsentTags(googlefc.getGoogleConsentModeValues())) {
      loadGtagScript();
    }
  },
});
</script>

如果您使用 Google 意见征求管理解决方案在 IAB GPP 框架下向最终用户提供“选择不出售/分享个人信息”的美国州级法规消息,则应使用 IAB GPP API

由于美国州级法规的选择停用性质,您可以使用 CONSENT_API_READYCONSENT_DATA_READY 回调队列键,以确保在调用回调时 IAB GPP API 可调用且会返回意见征求数据。

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Queue the callback on the callbackQueue.
  window.googlefc.callbackQueue.push({
    'CONSENT_DATA_READY':
    () => __gpp('ping', (data, success) => {
        // Do something with consent data value.
    })
  });
</script>

如果您使用 Google 意见征求管理解决方案在 IAB GPP 框架下向最终用户提供美国州级法规“选择不出售/分享个人信息”消息,则可以通过将 googlefc.usstatesoptout.overrideDnsLink 标志设置为 true 来提供您自己的自定义“不出售或分享个人信息”链接。

<script>
  // Make sure that the properties exist on the window.
  window.googlefc = window.googlefc || {};
  window.googlefc.usstatesoptout = window.googlefc.usstatesoptout || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];

  // Signals that the default DNS link will be overridden.
  window.googlefc.usstatesoptout.overrideDnsLink = true;

  // Register the callback for the initial US state regulations data.
  window.googlefc.callbackQueue.push({
      'INITIAL_US_STATES_OPT_OUT_DATA_READY': () => {
        if (googlefc.usstatesoptout.getInitialUsStatesOptOutStatus() ===
            googlefc.usstatesoptout.InitialUsStatesOptOutStatusEnum.NOT_OPTED_OUT) {
          // TODO: Display custom Do Not Sell or Share link here.
        }
      }
    });
</script>

这样可以确保系统不会呈现默认的“不出售或分享”链接。然后,您需要通过调用美国州级法规“不出售/分享”功能的停用确认对话框来处理用户与自定义“不出售/分享”链接的互动。

请注意,使用自定义“不出售/分享”链接时,您有责任确保该链接符合美国州级法规。

<script>
// This callback will be called when the user makes a US state regulations
// decision.
const usStateRegCompletionCallback = (userOptedOut) => {
  if (userOptedOut) {
    // TODO: Hide custom Do Not Sell or Share link here.
  }
}
// Invoke the US state regulations opt-out confirmation dialog when the user
// clicks the link.
document.getElementById("your-custom-do-not-sell-link").addEventListener(
  "click", () => googlefc.usstatesoptout.openConfirmationDialog(usStateRegCompletionCallback));
</script>