Migration de l'API Config

Ce guide fournit des exemples de migration des anciennes méthodes de configuration vers les nouvelles API setConfig et getConfig de la bibliothèque Google Publisher Tag (GPT).

Les API setConfig et getConfig permettent de gérer de manière centralisée la configuration au niveau de la page et de l'emplacement.

Définir la configuration au niveau de la page

Le tableau suivant mappe les anciennes méthodes de configuration PubAdsService avec leurs remplacements setConfig.

Fonctionnalité Ancienne méthode Remplacement de setConfig
Attributs AdSense set(key, value) googletag.setConfig({ adsenseAttributes: { [key]: value } })
Exclusion de catégories clearCategoryExclusions() googletag.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) googletag.setConfig({ categoryExclusion: [label] })
Centrage setCentering(centerAds) googletag.setConfig({ centering: centerAds })
Réduire les éléments div vides collapseEmptyDivs(collapseBeforeFetch) googletag.setConfig({ collapseDiv: collapseBeforeFetch ? 'BEFORE_FETCH' : 'ON_NO_FILL' })
Chargement initial et architecture de demande simple (SRA) disableInitialLoad() googletag.setConfig({ disableInitialLoad: true })
enableSingleRequest() googletag.setConfig({ singleRequest: true })
Chargement différé enableLazyLoad(config) googletag.setConfig({ lazyLoad: config })
Emplacement setLocation(address) googletag.setConfig({ location: address })
SafeFrame setForceSafeFrame(force) googletag.setConfig({ safeFrame: { forceSafeFrame: force } })
setSafeFrameConfig(config) googletag.setConfig({ safeFrame: config })
Ciblage clearTargeting(key) googletag.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) googletag.setConfig({ targeting: { [key]: value } })
Annonces vidéo enableVideoAds() googletag.setConfig({ videoAds: { enableVideoAds: true } })
setVideoContent(contentId, cmsId) googletag.setConfig({ videoAds: { enableVideoAds: true, videoContentId: contentId, videoCmsId: cmsId } })

Attributs AdSense

Ancienne version :

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

Nouvelle réponse :

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

Exclusion de catégories

Ancienne version :

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

Nouvelle réponse :

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

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

Centrage

Ancienne version :

googletag.pubads().setCentering(true);

Nouvelle réponse :

googletag.setConfig({
  centering: true
});

Réduire les éléments div vides

Ancienne version :

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

Nouvelle réponse :

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

Chargement initial et architecture de demande simple (SRA)

Ancienne version :

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

Nouvelle réponse :

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

Chargement différé

Ancienne version :

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

Nouvelle réponse :

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

Emplacement

Ancienne version :

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

Nouvelle réponse :

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

SafeFrame

Ancienne version :

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

Nouvelle réponse :

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

Ciblage

Ancienne version :

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

Nouvelle réponse :

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

Annonces vidéo

Ancienne version :

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

Nouvelle réponse :

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

Définir la configuration au niveau de l'emplacement

Le tableau suivant mappe les anciennes méthodes de configuration Slot avec leurs remplacements setConfig.

Fonctionnalité Ancienne méthode Remplacement de setConfig
Attributs AdSense set(key, value) Slot.setConfig({ adsenseAttributes: { [key]: value } })
Exclusion de catégories clearCategoryExclusions() Slot.setConfig({ categoryExclusion: null })
setCategoryExclusion(label) Slot.setConfig({ categoryExclusion: [label] })
URL de clic setClickUrl(url) Slot.setConfig({ clickUrl: url })
Réduire les éléments div vides 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 })
Ciblage clearTargeting(key) Slot.setConfig({ targeting: { [key]: null } })
setTargeting(key, value) Slot.setConfig({ targeting: { [key]: value } })
updateTargetingFromMap(config) Slot.setConfig({ targeting: config })

Attributs AdSense

Ancienne version :

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

Nouvelle réponse :

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

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

Exclusion de catégories

Ancienne version :

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

Nouvelle réponse :

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

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

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

Cliquez sur l'URL

Ancienne version :

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

Nouvelle réponse :

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

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

Réduire les éléments div vides

Ancienne version :

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

Nouvelle réponse :

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

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

SafeFrame

Ancienne version :

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

Nouvelle réponse :

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

Ciblage

Ancienne version :

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

Nouvelle réponse :

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

Obtenir la configuration au niveau de la page

Le tableau suivant mappe les anciennes méthodes getter PubAdsService à leurs remplacements getConfig.

Fonctionnalité Ancienne méthode Remplacement de getConfig
Attributs AdSense get(key) googletag.getConfig('adsenseAttributes')
getAttributeKeys() googletag.getConfig('adsenseAttributes')
Chargement initial isInitialLoadDisabled() googletag.getConfig('disableInitialLoad')
Ciblage getTargeting(key) googletag.getConfig('targeting')
getTargetingKeys() googletag.getConfig('targeting')

Attributs AdSense

Ancienne version :

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

Nouvelle réponse :

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

Chargement initial

Ancienne version :

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

Nouvelle réponse :

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

Ciblage

Ancienne version :

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

Nouvelle réponse :

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

Obtenir la configuration au niveau de l'emplacement

Le tableau suivant mappe les anciennes méthodes getter Slot à leurs remplacements getConfig.

Fonctionnalité Ancienne méthode Remplacement de getConfig
Attributs AdSense get(key) Slot.getConfig('adsenseAttributes')
getAttributeKeys() Slot.getConfig('adsenseAttributes')
Exclusion de catégories getCategoryExclusions() Slot.getConfig('categoryExclusion')
Ciblage getTargeting(key) Slot.getConfig('targeting')
getTargetingKeys() Slot.getConfig('targeting')

Attributs AdSense

Ancienne version :

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

Nouvelle réponse :

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 keys.
const adsenseAttributes = Object.keys(adsenseConfig);

Exclusion de catégories

Ancienne version :

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

Nouvelle réponse :

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

Ciblage

Ancienne version :

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

Nouvelle réponse :

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