Di chuyển Config API

Hướng dẫn này cung cấp các ví dụ về cách di chuyển từ các phương thức định cấu hình cũ sang thư viện Thẻ nhà xuất bản của Google (GPT) setConfig và API getConfig mới.

API setConfiggetConfig cung cấp một cách tập trung để quản lý cả cấu hình cấp trang và cấp vùng quảng cáo.

Đặt cấu hình cấp trang

Bảng sau đây ánh xạ các phương thức định cấu hình PubAdsService cũ với các phương thức thay thế setConfig.

Tính năng Phương pháp cũ Thay thế setConfig
Thuộc tính trong AdSense set(key, value) googletag.setConfig({ adsenseAttributes: { [key]: value } })
Loại trừ danh mục clearCategoryExclusions() googletag.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) googletag.setConfig({ categoryExclusion: [label] })
Căn giữa setCentering(centerAds) googletag.setConfig({ centering: centerAds })
Thu gọn div trống collapseEmptyDivs(collapseBeforeFetch) googletag.setConfig({ collapseDiv: collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL' })
Tải ban đầu và Cấu trúc yêu cầu duy nhất (SRA) disableInitialLoad() googletag.setConfig({ disableInitialLoad: true })
enableSingleRequest() googletag.setConfig({ singleRequest: true })
Tải từng phần enableLazyLoad(config) googletag.setConfig({ lazyLoad: config })
Vị trí setLocation(address) googletag.setConfig({ location: address })
SafeFrame setForceSafeFrame(force) slot.setConfig({ safeFrame: { forceSafeFrame: force } })
setSafeFrameConfig(config) slot.setConfig({ safeFrame: config })
Nhắm mục tiêu clearTargeting(key) googletag.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) googletag.setConfig({ targeting: { [key]: value } })
Quảng cáo dạng video enableVideoAds() googletag.setConfig({ videoAds: { enableVideoAds: true } })
setVideoContent(contentId, cmsId) googletag.setConfig({ videoAds: { videoContentId: contentId, videoCmsId: cmsId } })

Thuộc tính AdSense

Cũ:

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

Mới:

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

Loại trừ danh mục

Cũ:

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

Mới:

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

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

Căn giữa

Cũ:

googletag.pubads().setCentering(true);

Mới:

googletag.setConfig({
  centering: true
});

Thu gọn div trống

Cũ:

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

Mới:

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

Tải ban đầu và Cấu trúc yêu cầu duy nhất (SRA)

Cũ:

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

Mới:

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

Tải từng phần

Cũ:

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

Mới:

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

Thông tin vị trí

Cũ:

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

Mới:

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

SafeFrame

Cũ:

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

Mới:

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

Nhắm mục tiêu

Cũ:

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

Mới:

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

Quảng cáo dạng video

Cũ:

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

Mới:

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

Đặt cấu hình cấp vị trí

Bảng sau đây ánh xạ các phương thức định cấu hình Slot cũ với các phương thức thay thế setConfig.

Tính năng Phương pháp cũ Thay thế setConfig
Thuộc tính trong AdSense set(key, value) Slot.setConfig({ adsenseAttributes: { [key]: value } })
Loại trừ danh mục clearCategoryExclusions() Slot.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) Slot.setConfig({ categoryExclusion: [label] })
URL nhấp vào setClickUrl(url) Slot.setConfig({ clickUrl: url })
Thu gọn div trống 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 })
Nhắm mục tiêu clearTargeting(key) Slot.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) Slot.setConfig({ targeting: { [key]: value } })
updateTargetingFromMap(config) Slot.setConfig({ targeting: config })

Thuộc tính AdSense

Cũ:

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

Mới:

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

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

Loại trừ danh mục

Cũ:

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

Mới:

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

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

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

Bấm URL

Cũ:

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

Mới:

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

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

Thu gọn div trống

Cũ:

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

Mới:

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

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

SafeFrame

Cũ:

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

Mới:

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

Nhắm mục tiêu

Cũ:

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

Mới:

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

Nhận cấu hình cấp trang

Bảng sau đây liên kết các phương thức getter PubAdsService cũ với các phương thức thay thế getConfig.

Tính năng Phương pháp cũ Thay thế setConfig
Thuộc tính trong AdSense get(key) googletag.getConfig('adsenseAttributes')
getAttributeKeys() googletag.getConfig('adsenseAttributes')
Tải ban đầu isInitialLoadDisabled() googletag.getConfig('disableInitialLoad')
Nhắm mục tiêu getTargeting(key) googletag.getConfig('targeting')
getTargetingKeys() googletag.getConfig('targeting')

Thuộc tính AdSense

Cũ:

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

Mới:

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

Tải ban đầu

Cũ:

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

Mới:

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

Nhắm mục tiêu

Cũ:

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

Mới:

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

Nhận cấu hình cấp vị trí

Bảng sau đây liên kết các phương thức getter Slot cũ với các phương thức thay thế getConfig.

Tính năng Phương pháp cũ Thay thế setConfig
Thuộc tính trong AdSense get(key) Slot.getConfig('adsenseAttributes')
getAttributeKeys() Slot.getConfig('adsenseAttributes')
Loại trừ danh mục getCategoryExclusions() Slot.getConfig('categoryExclusion')
Nhắm mục tiêu getTargeting(key) Slot.getConfig('targeting')
getTargetingKeys() Slot.getConfig('targeting')

Thuộc tính AdSense

Cũ:

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

Mới:

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

Loại trừ danh mục

Cũ:

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

Mới:

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

Nhắm mục tiêu

Cũ:

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

Mới:

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