миграция API конфигурации

В этом руководстве приведены примеры перехода от устаревших методов настройки к новым API-интерфейсам setConfig и getConfig библиотеки Google Publisher Tag (GPT).

API-интерфейсы setConfig и getConfig предоставляют централизованный способ управления конфигурацией как на уровне страниц, так и на уровне слотов.

Настройка конфигурации на уровне страницы

В приведенной ниже таблице показано соответствие устаревших методов конфигурации 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);