隱私權與隱私權;訊息 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 (積分牆訊息)、usfl (美國州級法規選擇退出訊息,僅限佛羅里達州)、usnat (美國州級法規選擇退出訊息,適用於佛羅里達州以外的所有支援州別;等同於 ccpa)

以下列舉幾個範例,說明如何使用這項功能在網站 (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>

這項功能也提供擴充功能,發布商可指定只隱藏 Offerwall。使用這項功能擴充,即可禁止顯示 Offerwall,但不會禁止顯示其他類型的訊息。

如要使用 Offerwall 專屬的受控訊息,請將額外參數傳遞至 message.proceed(),也就是 googlefc.MessageTypeEnum 類型的 Array

範例:以下範例說明如何使用 googlefc.controlledMessagingFunction,只禁止向訂閱者放送 Offerwall,而不禁止其他類型的訊息:

<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 如果使用 CONSENT_API_READY 鍵將函式推送至回呼佇列,系統會在定義並可呼叫支援的同意聲明架構 API 時執行函式。從這一刻起,後續新增的任何 CONSENT_API_READY 鍵控函式都會同步執行。如需架構專屬詳細資料,請參閱「 IAB 架構」一節。 Low
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 如果使用 CONSENT_MODE_DATA_READY 鍵將函式推送至回呼佇列,當 Google[同意聲明模式](https://support.google.com/google-ads/answer/10000067) 資料 (用於 Google Ads 和 Analytics 代碼) 在流程中可用時,系統就會執行這些函式。同意聲明模式資料準備就緒後,您隨時可以使用 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}

清除目前的歐盟法規同意聲明記錄 (如有),並再次顯示歐盟法規訊息,讓使用者變更同意聲明決策。

範例 1:簡單範例,說明如何設定連結,點選後會顯示撤銷訊息:

<a href="javascript:window.googlefc.showRevocationMessage();">Privacy and cookie settings</a>
<a href="javascript:window.googlefc.showRevocationMessage();" style="display: none;" id="revocation-link">Privacy and cookie settings</a>
<script>
  window.googlefc = window.googlefc || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];
  window.googlefc.callbackQueue.push({
    'CONSENT_API_READY':
    () => {
      // Update the revocation link so that it shows on the page.
      const revocationLink = document.getElementById('revocation-link');
      revocationLink.style.display = 'block';
    }
  });
</script>

範例 2:如果只想在歐盟法規適用於目前使用者時顯示連結,可以使用 googlefc 回呼佇列和 TCF API,根據判斷出的 gdprApplies 值,有條件地更新按鈕的顯示狀態。請使用 CONSENT_API_READY API 金鑰。

<a href="javascript:window.googlefc.showRevocationMessage();" style="display: none;" id="revocation-link">Privacy and cookie settings</a>
<script>
  window.googlefc = window.googlefc || {};
  window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];
  window.googlefc.callbackQueue.push({
    'CONSENT_API_READY':
    // Specifying "0" for the version parameter will result in the API call
    // using the latest version of the TCF spec.
    () => __tcfapi('addEventListener', 0, (tcdata, success) => {
      const revocationLink = document.getElementById('revocation-link');
      if (!success || !tcdata) {
        // Something went wrong, don't show the revocation link.
        revocationLink.style.display = 'none';
      }
      else if (tcdata.gdprApplies) {
        revocationLink.style.display = 'block';
      } else {
        // GDPR does not apply so don't show the revocation link.
        revocationLink.style.display = 'none';
      }
    })
  });
</script>

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 資訊公開和同意聲明架構第 2 版收集《GDPR》同意聲明,請使用 IAB 資訊公開和同意聲明架構第 2 版 API

您可以使用 CONSENT_API_READY 回呼佇列金鑰,確保只有在網頁上定義 IAB TCF 第 2 版 API 時,才會叫用對應的回呼。這項功能應與 IAB TCF 第 2 版 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' 指令搭配使用,只要資訊公開和同意聲明架構第 2 版適用於該使用者,您在第一次叫用所提供的回呼時,所提供的資料就會包含使用者的同意聲明選項。請注意,隨著 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 和 Analytics 說明文件。建議您諮詢法務部門,瞭解應導入哪種同意聲明模式,才能符合法律規定。

系統預設支援進階同意聲明模式,您在隱私權與訊息工具使用者介面中啟用同意聲明模式後,就不需要進行其他作業。

如要使用 Google 同意聲明管理解決方案導入基本同意聲明模式,您可以使用 CONSENT_MODE_DATA_READY 回呼佇列鍵,在同意聲明模式資料可用時,有條件地載入 Google Ads 和 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 和 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>