انتقال پیکربندی API

این راهنما مثال‌هایی برای مهاجرت از روش‌های پیکربندی قدیمی به کتابخانه جدید Google Publisher Tag (GPT) یعنی APIهای setConfig و getConfig ارائه می‌دهد.

رابط‌های برنامه‌نویسی setConfig و getConfig روشی متمرکز برای مدیریت پیکربندی در سطح صفحه و اسلات ارائه می‌دهند.

تنظیم پیکربندی سطح صفحه

جدول زیر، متدهای پیکربندی قدیمی PubAdsService را به جایگزین‌های setConfig آنها نگاشت می‌کند.

ویژگی روش قدیمی جایگزینی setConfig
ویژگی‌های ادسنس 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 })
قاب امن 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 } })

ویژگی‌های ادسنس

میراث:

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

قاب امن

میراث:

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
ویژگی‌های ادسنس set(key, value) Slot.setConfig({ adsenseAttributes: { [key]: value } })
استثنای دسته بندی clearCategoryExclusions() Slot.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) Slot.setConfig({ categoryExclusion: [label] })
روی آدرس کلیک کنید setClickUrl(url) Slot.setConfig({ clickUrl: url })
جمع کردن div های خالی setCollapseEmptyDiv(collapse, collapseBeforeFetch) Slot.setConfig({ collapseDiv: collapse ? (collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL') : 'DISABLED' })
قاب امن 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 })

ویژگی‌های ادسنس

میراث:

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

روی آدرس کلیک کنید

میراث:

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

قاب امن

میراث:

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
ویژگی‌های ادسنس get(key) googletag.getConfig('adsenseAttributes')
getAttributeKeys() googletag.getConfig('adsenseAttributes')
بار اولیه isInitialLoadDisabled() googletag.getConfig('disableInitialLoad')
هدف‌گیری getTargeting(key) googletag.getConfig('targeting')
getTargetingKeys() googletag.getConfig('targeting')

ویژگی‌های ادسنس

میراث:

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

پیکربندی سطح اسلات را دریافت کنید

جدول زیر متدهای getter قدیمی Slot را به جایگزین‌های getConfig آنها نگاشت می‌کند.

ویژگی روش قدیمی جایگزینی setConfig
ویژگی‌های ادسنس get(key) Slot.getConfig('adsenseAttributes')
getAttributeKeys() Slot.getConfig('adsenseAttributes')
استثنای دسته بندی getCategoryExclusions() Slot.getConfig('categoryExclusion')
هدف‌گیری getTargeting(key) Slot.getConfig('targeting')
getTargetingKeys() Slot.getConfig('targeting')

ویژگی‌های ادسنس

میراث:

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