Migración de la API de Config

En esta guía, se proporcionan ejemplos para migrar de los métodos de configuración heredados a las nuevas APIs de setConfig y getConfig de la biblioteca de Google Publisher Tag (GPT).

Las APIs de setConfig y getConfig proporcionan una forma centralizada de administrar la configuración a nivel de la página y de la ranura.

Cómo establecer la configuración a nivel de la página

En la siguiente tabla, se asignan los métodos de configuración heredados de PubAdsService a sus reemplazos de setConfig.

Función Método heredado Reemplazo de setConfig
Atributos de AdSense set(key, value) googletag.setConfig({ adsenseAttributes: { [key]: value } })
Exclusión de categorías clearCategoryExclusions() googletag.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) googletag.setConfig({ categoryExclusion: [label] })
Centrado setCentering(centerAds) googletag.setConfig({ centering: centerAds })
Contraer elementos div vacíos collapseEmptyDivs(collapseBeforeFetch) googletag.setConfig({ collapseDiv: collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL' })
Carga inicial y arquitectura de solicitud única (SRA) disableInitialLoad() googletag.setConfig({ disableInitialLoad: true })
enableSingleRequest() googletag.setConfig({ singleRequest: true })
Carga diferida enableLazyLoad(config) googletag.setConfig({ lazyLoad: config })
Ubicación setLocation(address) googletag.setConfig({ location: address })
SafeFrame setForceSafeFrame(force) slot.setConfig({ safeFrame: { forceSafeFrame: force } })
setSafeFrameConfig(config) slot.setConfig({ safeFrame: config })
Orientación clearTargeting(key) googletag.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) googletag.setConfig({ targeting: { [key]: value } })
Anuncios de video enableVideoAds() googletag.setConfig({ videoAds: { enableVideoAds: true } })
setVideoContent(contentId, cmsId) googletag.setConfig({ videoAds: { videoContentId: contentId, videoCmsId: cmsId } })

Atributos de AdSense

Heredado:

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

Nuevo:

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

Exclusión de categorías

Heredado:

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

Nuevo:

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

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

Centrado

Heredado:

googletag.pubads().setCentering(true);

Nuevo:

googletag.setConfig({
  centering: true
});

Contraer elementos div vacíos

Heredado:

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

Nuevo:

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

Carga inicial y arquitectura de solicitud única (SRA)

Heredado:

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

Nuevo:

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

Carga diferida

Heredado:

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

Nuevo:

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

Ubicación

Heredado:

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

Nuevo:

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

SafeFrame

Heredado:

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

Nuevo:

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

Segmentación

Heredado:

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

Nuevo:

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

Anuncios de video

Heredado:

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

Nuevo:

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

Cómo establecer la configuración a nivel del espacio

En la siguiente tabla, se asignan los métodos de configuración heredados de Slot a sus reemplazos de setConfig.

Función Método heredado Reemplazo de setConfig
Atributos de AdSense set(key, value) Slot.setConfig({ adsenseAttributes: { [key]: value } })
Exclusión de categorías clearCategoryExclusions() Slot.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) Slot.setConfig({ categoryExclusion: [label] })
URL de clic setClickUrl(url) Slot.setConfig({ clickUrl: url })
Contraer elementos div vacíos 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 })
Orientación clearTargeting(key) Slot.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) Slot.setConfig({ targeting: { [key]: value } })
updateTargetingFromMap(config) Slot.setConfig({ targeting: config })

Atributos de AdSense

Heredado:

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

Nuevo:

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

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

Exclusión de categorías

Heredado:

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

Nuevo:

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

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

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

Haga clic en la dirección URL

Heredado:

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

Nuevo:

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

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

Contraer div vacío

Heredado:

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

Nuevo:

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

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

SafeFrame

Heredado:

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

Nuevo:

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

Segmentación

Heredado:

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

Nuevo:

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

Obtén la configuración a nivel de la página

En la siguiente tabla, se asignan los métodos get heredados de PubAdsService a sus reemplazos de getConfig.

Función Método heredado Reemplazo de setConfig
Atributos de AdSense get(key) googletag.getConfig('adsenseAttributes')
getAttributeKeys() googletag.getConfig('adsenseAttributes')
Carga inicial isInitialLoadDisabled() googletag.getConfig('disableInitialLoad')
Orientación getTargeting(key) googletag.getConfig('targeting')
getTargetingKeys() googletag.getConfig('targeting')

Atributos de AdSense

Heredado:

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

Nuevo:

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

Carga inicial

Heredado:

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

Nuevo:

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

Segmentación

Heredado:

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

Nuevo:

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

Obtén la configuración a nivel del espacio publicitario

En la siguiente tabla, se asignan los métodos get heredados de Slot a sus reemplazos de getConfig.

Función Método heredado Reemplazo de setConfig
Atributos de AdSense get(key) Slot.getConfig('adsenseAttributes')
getAttributeKeys() Slot.getConfig('adsenseAttributes')
Exclusión de categorías getCategoryExclusions() Slot.getConfig('categoryExclusion')
Orientación getTargeting(key) Slot.getConfig('targeting')
getTargetingKeys() Slot.getConfig('targeting')

Atributos de AdSense

Heredado:

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

Nuevo:

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

Exclusión de categorías

Heredado:

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

Nuevo:

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

Segmentación

Heredado:

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

Nuevo:

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