建立同意聲明模式範本

本文適用於負責維護同意聲明管理解決方案的開發人員 使用 Google 代碼管理工具 (GTM) 的網站。

本頁面介紹 Google 代碼管理工具中的同意聲明類型,並說明如何 與同意聲明管理解決方案整合

提供代碼範本後,使用者就可以整合您的同意聲明 ,省下大量時間和精力

使用者可使用同意聲明模式範本,並 將訪客同意聲明選項傳送至 Google 代碼管理工具。這麼做能確保 可正常運作的 Google 代碼和第三方代碼 (支援同意聲明) 模式。

範本建立者可以導入同意聲明模式範本供內部使用 或發布到社群範本庫 可以公開取得提供同意聲明的同意聲明管理平台 (CMP) 供應商 同意聲明模式範本有機會在同意聲明模式中列出 文件,並讓範本庫挑選器呈現範本。

,瞭解如何調查及移除這項存取權。

Google 和第三方代碼會根據同意聲明狀態調整儲存行為 granteddenied 狀態。內建同意聲明檢查 下列任何同意聲明類型

同意聲明類型 說明
ad_storage 啟用與廣告相關的 Cookie (例如 Cookie)。
ad_user_data 設定基於線上廣告用途,向 Google 傳送使用者資料的同意聲明。
ad_personalization 設定個人化廣告的同意聲明。
analytics_storage 啟用與數據分析相關的儲存功能,例如 Cookie (例如, 。
functionality_storage 啟用支援網站或應用程式功能的儲存功能 例如語言設定
personalization_storage 啟用與個人化等個人化功能相關的儲存功能 最佳化建議。
security_storage 啟用與安全性 (例如驗證) 相關的儲存功能 功能、防範詐欺和其他使用者防護措施

同意聲明模式會追蹤訪客同意聲明選項和代碼同意聲明檢查 確保代碼行為能據此調整建立新的同意聲明時 範本,請遵循以下最佳做法:

  • 使用代碼管理工具同意聲明模式 API setDefaultConsentStateupdateConsentState,而不是 gtag consent

  • 使用 同意聲明初始化 - 所有網頁觸發條件。

  • CMP 必須盡快提示訪客授予或拒絕同意聲明 。

  • 如果訪客指明瞭同意聲明選項,CMP 必須傳送 同意聲明狀態。

1. 建立新範本

此實作方法會使用範本中的一個欄位來保存 預設的同意聲明狀態。實作程式碼會讀取該欄位,設定 會在執行階段預設為同意聲明狀態若是更新指令,程式碼會嘗試 讀取同意聲明解決方案所設定的 Cookie,以儲存訪客同意聲明選項。 您也會設定回呼,讓 updateConsentState 處理案件 訪客尚未選取同意聲明選項或決定變更 取得使用者同意。

  1. 登入 Google 代碼管理工具帳戶。
  2. 選取左側導覽面板中的「範本」
  3. 在「Tag Templates」窗格中,按一下「New」
  1. 選取「欄位」標籤,然後按一下「新增欄位」參數表
  2. 將名稱變更為 defaultSettings
  3. 展開欄位。
  4. 將「顯示名稱」更新為 Default settings
  5. 按一下「新增資料欄」,選擇「文字輸入」,將名稱變更為 region,然後 勾選「資料欄值不得重複」方塊。
  6. 展開資料欄,將顯示名稱變更為 Region (leave blank to have consent apply to all regions)。括號中的陳述式為 說明文件。進一步瞭解 為不同區域設定同意聲明預設狀態
  7. 按一下「新增資料欄」,選擇「文字輸入」,將名稱變更為 granted
  8. 展開資料欄,將顯示名稱變更為 Granted Consent Types (comma separated)
  9. 按一下「新增資料欄」,選擇「文字輸入」,將名稱變更為 denied
  10. 展開資料欄,將顯示名稱變更為 Denied Consent Types (comma separated)

選用:如何新增廣告資料遮蓋支援功能:

  1. 按一下「新增欄位」,選擇「核取方塊」,然後將欄位名稱變更為 ads_data_redaction
  2. 將顯示名稱更新為 Redact Ads Data

透過廣告資料遮蓋功能,進一步瞭解 Cookie 行為

選用:如何新增透過網址參數傳遞的支援:

  1. 按一下「新增欄位」,選擇「核取方塊」,然後將欄位名稱變更為 url_passthrough
  2. 將顯示名稱更新為 Pass through URL parameters

進一步瞭解如何透過網址參數傳遞

新增實作程式碼的步驟如下:

  1. 在範本編輯器中開啟「Code」分頁標籤。
  2. 在下方程式碼範例中,編輯預留位置欄位。
  3. 複製程式碼,並將範本編輯器中的樣板程式碼替換成該程式碼。
  4. 按一下「儲存」即可儲存範本。
,瞭解如何調查及移除這項存取權。
// The first two lines are optional, use if you want to enable logging
const log = require('logToConsole');
log('data =', data);
const setDefaultConsentState = require('setDefaultConsentState');
const updateConsentState = require('updateConsentState');
const getCookieValues = require('getCookieValues');
const callInWindow = require('callInWindow');
const gtagSet = require('gtagSet');
const COOKIE_NAME = 'Your_cookie_name';
/*
 *   Splits the input string using comma as a delimiter, returning an array of
 *   strings
 */
const splitInput = (input) => {
  return input.split(',')
      .map(entry => entry.trim())
      .filter(entry => entry.length !== 0);
};
/*
 *   Processes a row of input from the default settings table, returning an object
 *   which can be passed as an argument to setDefaultConsentState
 */
const parseCommandData = (settings) => {
  const regions = splitInput(settings['region']);
  const granted = splitInput(settings['granted']);
  const denied = splitInput(settings['denied']);
  const commandData = {};
  if (regions.length > 0) {
    commandData.region = regions;
  }
  granted.forEach(entry => {
    commandData[entry] = 'granted';
  });
  denied.forEach(entry => {
    commandData[entry] = 'denied';
  });
  return commandData;
};
/*
 *   Called when consent changes. Assumes that consent object contains keys which
 *   directly correspond to Google consent types.
 */
const onUserConsent = (consent) => {
  const consentModeStates = {
    ad_storage: consent['adConsentGranted'] ? 'granted' : 'denied',
    ad_user_data: consent['adUserDataConsentGranted'] ? 'granted' : 'denied',
    ad_personalization: consent['adPersonalizationConsentGranted'] ? 'granted' : 'denied',
    analytics_storage: consent['analyticsConsentGranted'] ? 'granted' : 'denied',
    functionality_storage: consent['functionalityConsentGranted'] ? 'granted' : 'denied',
    personalization_storage: consent['personalizationConsentGranted'] ? 'granted' : 'denied',
    security_storage: consent['securityConsentGranted'] ? 'granted' : 'denied',
  };
  updateConsentState(consentModeStates);
};
/*
 *   Executes the default command, sets the developer ID, and sets up the consent
 *   update callback
 */
const main = (data) => {
  /*
   * Optional settings using gtagSet
   */
  gtagSet('ads_data_redaction', data.ads_data_redaction);
  gtagSet('url_passthrough', data.url_passthrough);
  gtagSet('developer_id.your_developer_id', true);
  // Set default consent state(s)
  data.defaultSettings.forEach(settings => {
    const defaultData = parseCommandData(settings);
  // wait_for_update (ms) allows for time to receive visitor choices from the CMP
    defaultData.wait_for_update = 500;
    setDefaultConsentState(defaultData);
  });

  // Check if cookie is set and has values that correspond to Google consent
  // types. If it does, run onUserConsent().
  const settings = getCookieValues(COOKIE_NAME);
  if (typeof settings !== 'undefined') {
    onUserConsent(settings);
  }
  /**
   *   Add event listener to trigger update when consent changes
   *
   *   References an external method on the window object which accepts a
   *   function as an argument. If you do not have such a method, you will need
   *   to create one before continuing. This method should add the function
   *   that is passed as an argument as a callback for an event emitted when
   *   the user updates their consent. The callback should be called with an
   *   object containing fields that correspond to the five built-in Google
   *   consent types.
   */
  callInWindow('addConsentListenerExample', onUserConsent);
};
main(data);
data.gtmOnSuccess();

接著設定權限,以存取同意聲明狀態和存取 Cookie。

  1. 選取「權限」分頁標籤,然後按一下「存取同意聲明狀態」
  2. 按一下「新增同意聲明類型」
  3. 按一下方塊,然後在下拉式選單中選取「ad_storage」。
  4. 勾選「Write」
  5. 按一下「新增」
  6. 針對 ad_user_dataad_personalizationanalytics_storage。如需額外的同意聲明類型,請在 使用這個層即可
  7. 按一下 [儲存]

如何新增存取 Cookie 的權限:

  1. 選取「Permissions」(權限) 分頁標籤,然後按一下「讀取 Cookie 值」
  2. 在「特定」下方,輸入程式碼所需的各個 Cookie 名稱 ,以判斷使用者的同意聲明選項,每行輸入一個名稱。
  3. 按一下 [儲存]

2. 建立單元測試

如要瞭解如何為範本建立測試,請參閱「測試」。

以下程式碼範例顯示如何整合這個範本 新增事件監聽器,以便呼叫同意聲明管理解決方案的程式碼:

// Array of callbacks to be executed when consent changes
const consentListeners = [];

/**
 *   Called from GTM template to set callback to be executed when user consent is provided.
 *   @param {function} Callback to execute on user consent
 */
window.addConsentListenerExample = (callback) => {
  consentListeners.push(callback);
};

/**
 *   Called when user grants/denies consent.
 *   @param {Object} Object containing user consent settings.
 */
const onConsentChange = (consent) => {
  consentListeners.forEach((callback) => {
    callback(consent);
  });
};

網站訪客表示同意聲明選項後 (通常是 使用者與同意橫幅互動,範本程式碼應更新同意聲明 updateConsentState API 相應狀態。

以下範例為訪客符合以下條件的 updateConsentState 呼叫: 表示同意所有儲存空間類型。同樣地,這個範例也採用硬式編碼 但實際上,應該在執行階段決定是 granted 的值 並使用 CMP 收集的訪客同意聲明。

const updateConsentState = require('updateConsentState');

updateConsentState({
  'ad_storage': 'granted',
  'ad_user_data': 'granted',
  'ad_personalization': 'granted',
  'analytics_storage': 'granted',
  'functionality_storage': 'granted',
  'personalization_storage': 'granted',
  'security_storage': 'granted'
});

關於特定區域的行為

如要針對來自特定區域的訪客設定預設適用的同意聲明狀態,請按照下列步驟操作: 請根據 ISO 和 3166-2)。 範本。使用區域值可讓範本使用者遵循區域性值 而不會失去來自這些地區之訪客的資訊。時間 未在 setDefaultConsentState 指令中指定區域,那麼值 此規則適用於其他所有區域

舉例來說,下列指令會將 analytics_storage 的預設狀態設為 針對來自西班牙和阿拉斯加的訪客,denied,將 analytics_storage 設為 granted 適用於所有他人:

const setDefaultConsentState = require('setDefaultConsentState');

setDefaultConsentState({
  'analytics_storage': 'denied',
  'region': ['ES', 'US-AK']
});
setDefaultConsentState({
  'analytics_storage': 'granted'
});

最明確的優先順序

如果相同網頁出現兩個預設同意聲明指令,且該指令含有特定區域的值 目標區域適用的政策則會生效適用對象 舉例來說,如果您將 US 區域的 ad_storage 設為 'granted',且 某位來自加州的訪客,US-CA 區域的ad_storage已設為 'denied' ,特定的 US-CA 設定就會生效。

區域 ad_storage 行為
美國 'granted' 適用於位於美國「非」加拿大的使用者
美國 (加拿大) 'denied' 適用於美國加州使用者
未指定 'granted' 使用預設值 'granted'。在這個例子中 適用於美國境外或加拿大以外的使用者

其他中繼資料

您可以使用 gtagSet API 設定下列選用參數:

這些 API 只能在 Google 代碼管理工具範本沙箱環境中使用。

傳遞網址中的廣告點擊、用戶端 ID 和工作階段 ID 資訊

當訪客按下廣告進入廣告客戶的網站時, 系統可能會在到達網頁網址中加入廣告相關資訊做為查詢 參數。為提高轉換準確度,Google 代碼通常會儲存這項資料 位於廣告客戶網域上第一方 Cookie 的資訊

不過,如果 ad_storagedenied,Google 代碼不會儲存這項資訊 本機儲存空間在此情況下,廣告主可以改善廣告點擊評估品質, 可選擇使用 「網址傳輸」功能

同樣地,如果將 analytics_storage 設為拒絕,則可使用網址傳輸功能 傳送事件和以工作階段為基礎的分析 (包括轉換), 跨網頁的 Cookie。

您必須符合下列條件,才能使用網址直通功能:

  • 網頁上有同意聲明感知的 Google 代碼。
  • 網站已選擇使用網址傳遞功能。
  • 已在網頁上導入同意聲明模式。
  • 出站連結是指與目前網頁網域相同的網域。
  • 網址中有 gclid/DCLID (僅限 Google Ads 和 Floodlight 代碼)

您的範本應允許範本使用者設定 想要啟用這項設定。以下範本程式碼的用途是設定 url_passthrough 為 true:

gtagSet('url_passthrough', true);

遮蓋廣告資料

如果 ad_storage 遭拒,系統不會設定新的廣告 Cookie 用途。此外,系統也會在 google.com 和 就不會使用 doubleclick.net傳送給 Google 的資料仍會包含 完整網頁網址,包括網址參數中的所有廣告點擊資訊。

如要在 ad_storage 遭拒時進一步遮蓋廣告資料,請設定 ads_data_redaction 到 true。

如果 ads_data_redaction 為 true 且 ad_storage 遭拒,廣告點擊就會 Google Ads 和 Floodlight 代碼在網路要求中傳送的 ID,將會是 已遮蓋。

gtagSet('ads_data_redaction', true);

開發人員 ID

如果您是有 Google 核發開發人員 ID 的 CMP 供應商,請使用以下資料 方法,在範本中盡早完成此設定。

您只需要提供開發人員 ID,您導入的功能將用於 由不相關的公司或實體多個網站。 如果要讓某個網站或實體使用導入方式,請不要 申請開發人員 ID。

gtagSet('developer_id.<your_developer_id>', true);

為使用者提供文件

使用者會使用您的同意範本設定代碼收集使用者 同意。提供使用者相關說明文件,解釋下列最佳做法 做法:

  • 如何在「設定」表格中設定同意聲明預設值。
  • 如何新增其他區域,為不同區域設定同意聲明預設狀態 表格列。
  • 觸發「Consent Initialization - All Pages」(同意聲明初始化 - 所有網頁) 觸發條件上的代碼。

後續步驟

如果想將範本提供給所有代碼管理工具使用者,請將範本上傳到 社群範本庫