Config API の移行

このガイドでは、以前の構成方法から新しい Google パブリッシャー タグ(GPT)ライブラリの setConfig API と getConfig API に移行する例を示します。

setConfig API と getConfig API を使用すると、ページレベルとスロットレベルの両方の構成を一元的に管理できます。

ページレベルの構成を設定する

次の表に、以前の PubAdsService 構成方法と、その代替となる setConfig を示します。

機能 以前の方法 setConfig の代替
AdSense の属性 set(key, value) googletag.setConfig({ adsenseAttributes: { [key]: value } })
カテゴリの除外 clearCategoryExclusions() googletag.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) googletag.setConfig({ categoryExclusion: [label] })
中央揃え setCentering(centerAds) googletag.setConfig({ centering: centerAds })
空の div を折りたたむ collapseEmptyDivs(collapseBeforeFetch) googletag.setConfig({ collapseDiv: collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL' })
初期読み込みとシングル リクエスト アーキテクチャ(SRA) disableInitialLoad() googletag.setConfig({ disableInitialLoad: true })
enableSingleRequest() googletag.setConfig({ singleRequest: true })
遅延読み込み enableLazyLoad(config) googletag.setConfig({ lazyLoad: config })
ロケーション setLocation(address) googletag.setConfig({ location: address })
SafeFrame setForceSafeFrame(force) slot.setConfig({ safeFrame: { forceSafeFrame: force } })
setSafeFrameConfig(config) slot.setConfig({ safeFrame: config })
ターゲティング clearTargeting(key) googletag.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) googletag.setConfig({ targeting: { [key]: value } })
動画広告 enableVideoAds() googletag.setConfig({ videoAds: { enableVideoAds: true } })
setVideoContent(contentId, cmsId) googletag.setConfig({ videoAds: { videoContentId: contentId, videoCmsId: cmsId } })

AdSense の属性

以前:

googletag.pubads().set('document_language', 'en');

新規:

googletag.setConfig({
  adsenseAttributes: {
    document_language: 'en'
  }
});

カテゴリの除外

以前:

googletag.pubads().setCategoryExclusion('AirlineAd');
googletag.pubads().clearCategoryExclusions();

新規:

// Set category exclusion
googletag.setConfig({
  categoryExclusion: ['AirlineAd']
});

// Clear category exclusions
googletag.setConfig({
  categoryExclusion: null
});

中央揃え

以前:

googletag.pubads().setCentering(true);

新規:

googletag.setConfig({
  centering: true
});

空の div を折りたたむ

以前:

googletag.pubads().collapseEmptyDivs(true); // Collapse before fetch
googletag.pubads().collapseEmptyDivs(false); // Collapse on no fill

新規:

// Collapse before fetch
googletag.setConfig({
  collapseDiv: 'BEFORE_FETCH'
});

// Collapse on no fill
googletag.setConfig({
  collapseDiv: 'ON_NO_FILL'
});

// Don't collapse
googletag.setConfig({
  collapseDiv: 'DISABLED'
});

初期読み込みとシングル リクエスト アーキテクチャ(SRA)

以前:

googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();

新規:

googletag.setConfig({
  disableInitialLoad: true,
  singleRequest: true
});

遅延読み込み

以前:

googletag.pubads().enableLazyLoad({
  // Fetch slots within 5 viewports.
  fetchMarginPercent: 500,
  // Render slots within 2 viewports.
  renderMarginPercent: 200,
  // Double the above values on mobile.
  mobileScaling: 2.0,
});

新規:

googletag.setConfig({
  lazyLoad: {
    // Fetch slots within 5 viewports.
    fetchMarginPercent: 500,
    // Render slots within 2 viewports.
    renderMarginPercent: 200,
    // Double the above values on mobile.
    mobileScaling: 2.0,
  },
});

ロケーション

以前:

googletag.pubads().setLocation('10001,US');

新規:

googletag.setConfig({
  location: '10001,US'
});

SafeFrame

以前:

googletag.pubads().setForceSafeFrame(true);
googletag.pubads().setSafeFrameConfig({sandbox: true});

新規:

googletag.pubads().setConfig({
  safeFrame: {
    forceSafeFrame: true,
    sandbox: true
  }
});

ターゲティング

以前:

googletag.pubads().setTargeting('interests', 'sports');
googletag.pubads().setTargeting('interests', ['sports', 'music']);
googletag.pubads().clearTargeting('interests');
googletag.pubads().clearTargeting();

新規:

// Set targeting
googletag.setConfig({
  targeting: {
    interests: 'sports'
  }
});

// Set multiple values
googletag.setConfig({
  targeting: {
    interests: ['sports', 'music']
  }
});

// Clear a specific key
googletag.setConfig({
  targeting: {
    interests: null
  }
});

// Clear all targeting
googletag.setConfig({
  targeting: null
});

動画広告

以前:

googletag.pubads().enableVideoAds();
googletag.pubads().setVideoContent('video123', 'cms456');

新規:

googletag.setConfig({
  videoAds: {
    enableVideoAds: true,
    videoContentId: 'video123',
    videoCmsId: 'cms456'
  }
});

スロットレベルの構成を設定する

次の表に、以前の Slot 構成方法と、その代替となる setConfig を示します。

機能 以前の方法 setConfig の代替
AdSense の属性 set(key, value) Slot.setConfig({ adsenseAttributes: { [key]: value } })
カテゴリの除外 clearCategoryExclusions() Slot.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) Slot.setConfig({ categoryExclusion: [label] })
クリック URL setClickUrl(url) Slot.setConfig({ clickUrl: url })
空の div を折りたたむ setCollapseEmptyDiv(collapse, collapseBeforeFetch) Slot.setConfig({ collapseDiv: collapse ? (collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL') : 'DISABLED' })
SafeFrame setForceSafeFrame(force) Slot.setConfig({ safeFrame: { forceSafeFrame: force } })
setSafeFrameConfig(config) Slot.setConfig({ safeFrame: config })
ターゲティング clearTargeting(key) Slot.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) Slot.setConfig({ targeting: { [key]: value } })
updateTargetingFromMap(config) Slot.setConfig({ targeting: config })

AdSense の属性

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.set('adsense_background_color', '#FFFFFF');

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');

slot.setConfig({
  adsenseAttributes: {
    adsense_background_color: '#FFFFFF'
  }
});

カテゴリの除外

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setCategoryExclusion('AirlineAd');
slot.clearCategoryExclusions();

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');

// Set category exclusion
slot.setConfig({
  categoryExclusion: ['AirlineAd']
});

// Clear category exclusions
slot.setConfig({
  categoryExclusion: null
});

クリック URL

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setClickUrl('http://www.example.com?original_click_url=');

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');

slot.setConfig({
  clickUrl: 'http://www.example.com?original_click_url='
});

空の div の折りたたみ

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setCollapseEmptyDiv(true, true); // Collapse before fetch

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');

slot.setConfig({
  collapseDiv: 'BEFORE_FETCH'
});

SafeFrame

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setForceSafeFrame(true);
slot.setSafeFrameConfig({sandbox: true});

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setConfig({
  safeFrame: {
    forceSafeFrame: true,
    sandbox: true
  }
});

ターゲティング

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setTargeting('allow_expandable', 'true');
slot.clearTargeting('allow_expandable');
slot.updateTargetingFromMap({
  color: 'red',
  interests: ['sports', 'music', 'movies']
});

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');

// Set targeting
slot.setConfig({
  targeting: {
    allow_expandable: 'true'
  }
});

// Clear targeting
slot.setConfig({
  targeting: {
    allow_expandable: null
  }
});

// Update targeting (only specified KVs are set/modified).
slot.setConfig({
  targeting: {
    color: 'red',
    interests: ['sports', 'music', 'movies']
  }
})

ページレベルの構成を取得する

次の表に、以前の PubAdsService ゲッター メソッドと、その代替となる getConfig を示します。

機能 以前の方法 setConfig の代替
AdSense の属性 get(key) googletag.getConfig('adsenseAttributes')
getAttributeKeys() googletag.getConfig('adsenseAttributes')
初期読み込み isInitialLoadDisabled() googletag.getConfig('disableInitialLoad')
ターゲティング getTargeting(key) googletag.getConfig('targeting')
getTargetingKeys() googletag.getConfig('targeting')

AdSense の属性

以前:

const documentLangauage = googletag.pubads().get('document_language');
const adsenseAttributes = googletag.pubads().getAttributeKeys();

新規:

const adsenseConfig = googletag.getConfig('adsenseAttributes').adsenseAttributes;

// Get the value of a single AdSense attribute.
const documentLanguage = adsenseConfig.document_language || null;

// Get all configured AdSense attribute keys.
const adsenseAttributes = Object.keys(adsenseConfig);

初期読み込み

以前:

const isDisabled = googletag.pubads().isInitialLoadDisabled();

新規:

const isDisabled = googletag.getConfig('disableInitialLoad').disableInitialLoad;

ターゲティング

以前:

const targeting = googletag.pubads().getTargeting('interests');
const keys = googletag.pubads().getTargetingKeys();

新規:

const targetingConfig = googletag.getConfig('targeting').targeting;

// Get targeting for a specific key.
const targeting = targetingConfig.interests || [];

// Get all targeting keys.
const keys = Object.keys(targetingConfig);

スロットレベルの構成を取得する

次の表に、以前の Slot ゲッター メソッドと、その代替となる getConfig を示します。

機能 以前の方法 setConfig の代替
AdSense の属性 get(key) Slot.getConfig('adsenseAttributes')
getAttributeKeys() Slot.getConfig('adsenseAttributes')
カテゴリの除外 getCategoryExclusions() Slot.getConfig('categoryExclusion')
ターゲティング getTargeting(key) Slot.getConfig('targeting')
getTargetingKeys() Slot.getConfig('targeting')

AdSense の属性

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const bgColor = slot.get('adsense_background_color');
const adsenseAttributes = slot.getAttributeKeys();

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const adsenseConfig = slot.getConfig('adsenseAttributes').adsenseAttributes;

// Get the value of a single AdSense attribute.
const bgColor = adsenseConfig.adsense_background_color || null;

// Get all configured AdSense attribute.
const adsenseAttributes = Object.keys(adsenseConfig);

カテゴリの除外

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const exclusions = slot.getCategoryExclusions();

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const exclusions = slot.getConfig('categoryExclusion').categoryExclusion || [];

ターゲティング

以前:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const targeting = slot.getTargeting('allow_expandable');
const keys = slot.getTargetingKeys();

新規:

const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const targetingConfig = slot.getConfig('targeting').targeting;

// Get targeting for a specific key.
const targeting = targetingConfig.allow_expandable || [];

// Get all targeting keys.
const keys = Object.keys(targetingConfig);