注: このデベロッパー サイトは developers.google.com/tag-platform に移行中です。2021 年 9 月 30 日までに、リダイレクトが開始されます。

同意モードを CMP として実装する

Google タグ マネージャーには、コミュニティ テンプレート ギャラリーでデプロイ可能なタグ マネージャーのテンプレートを使って同意管理ソリューションを構築できる機能がいくつか用意されています。これらの機能には、タグの動作をより細かく制御できるよう一連の API と同意タイプが含まれています。詳しくは、タグ マネージャーの同意設定と構成をご覧ください。

デフォルトの同意設定を構成するには、setDefaultConsentState API を使用します。ユーザーの初期設定の際に、希望のデフォルト設定をユーザーに確認します。次の例は、ad_storage のデフォルト設定を 'denied'、そしてanalytics_storagefunctionality_storagepersonalization_storagesecurity_storage のデフォルト設定を 'granted' とすることを希望したユーザーの setDefaultConsentState 呼び出しを示しています。

const setDefaultConsentState = require('setDefaultConsentState');

setDefaultConsentState({
  'ad_storage': 'denied',
  'analytics_storage': 'granted',
  'functionality_storage': 'granted',
  'personalization_storage': 'granted',
  'security_storage': 'granted'
});

動作を更新する

ウェブサイトのユーザーが同意バナーを操作するなどして同意ステータスを示したら、updateConsentState API を使用して同意ステータスを更新します。次の例は、ad_storageanalytics_storagefunctionality_storagepersonalization_storagesecurity_storage に同意したユーザーの updateConsentState 呼び出しを示しています。

const updateConsentState = require('updateConsentState');

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

実装例

次の実装例では、テンプレート エディタで 3 つのフィールドを作成する必要があります。

command フィールド

  1. [フィールド] タブで [フィールドを追加] をクリックします
  2. [プルダウン メニュー] を選択します
  3. 名前を「command」に変更します
  4. [メニュー項目を追加] をクリックして、[アイテム名] に「default」、[] に「default」と入力します
  5. [メニュー項目を追加] をクリックして、[アイテム名] に「update」、[] に「update」と入力します

defaultSettings フィールド

  1. [フィールド] タブで [フィールドを追加] をクリックします
  2. [パラメータ表] を選択します
  3. 名前を「defaultSettings」に変更し、フィールドを展開します
  4. [表示名] を「Default settings」に変更します
  5. [列を追加] をクリックし、[テキスト入力] を選択して名前を「region」に変更し、[列の値は一意である必要があります] にチェックを入れます。列を展開し、表示名を「Region (leave black for all regions)」に変更します
  6. [列を追加] をクリックし、[テキスト入力] を選択して、名前を「granted」に変更します。列を展開し、表示名を「Granted (comma separated)」に変更します
  7. [列を追加] をクリックし、[テキスト入力] を選択して、名前を「granted」に変更します。列を展開し、表示名を「Granted (comma separated)」に変更します

updateSettings フィールド

同じ手順を defaultSettings フィールドに繰り返します。ただし、フィールド名を defaultSettings ではなく updateSettings にします。

これら 3 つのフィールドを作成したら、[コード] タブに移動して次のコードを入力します。

const setDefaultConsentState = require('setDefaultConsentState');
const updateConsentState = require('updateConsentState');

const splitInput = (input) => {
  return input.split(',')
    .map(entry => entry.trim())
    .filter(entry => entry.length !== 0);
};

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;
};

if (data.command === 'default') {
  data.defaultSettings.forEach(settings => {
    const commandData = parseCommandData(settings);
    setDefaultConsentState(commandData);
  });
}
if (data.command === 'update') {
  data.updateSettings.forEach(settings => {
    const commandData = parseCommandData(settings);
    updateConsentState(commandData);
  });
}

data.gtmOnSuccess();

同意タイプ

同意タイプ
ad_storage 必須
analytics_storage 必須
functionality_storage 推奨
personalization_storage 推奨
security_storage 推奨

特定の地域に限定される動作

特定の地域のユーザーに対するタグのデフォルトの動作を変更するには、同意コマンドで地域を指定します。この値を指定することで、ユーザーの地理的位置に基づいてデフォルト設定を微調整できます。たとえば、スペインとアラスカのユーザーに対して analytics_storage'denied' に設定し、すべてのユーザーに対して 'granted'analytics_storage に設定するには、次のように指定します。

const setDefaultConsentState = require('setDefaultConsentState');

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

絞り込まれた地域を優先

デフォルトの同意コマンドが同じページに 2 つ存在し、それぞれのコマンドで特定の地域とそれよりも狭い地域に対する値が設定されている場合は、より狭い地域の値が優先されます。たとえば、US 地域のユーザーに対して ad_storage'granted' に設定し、US-CA 地域のユーザーに対して ad_storage'denied' に設定した場合、カリフォルニア州のユーザーにはより限定的な US-CA の設定が適用されます。この例では、US-CA からアクセスするユーザーに対して ad_storage'denied' に設定されます。

地域 ad_storage 動作
US 'granted' カリフォルニア州以外の米国のユーザーに適用されます
US-CA 'denied' US-CA(米国カリフォルニア州)のユーザーに適用されます
指定なし 'granted' デフォルト値の 'granted' が使用されます。US または US-CA 以外の地域のユーザーに適用されます