Ce guide fournit des exemples de migration des anciennes méthodes de configuration vers les nouvelles API de la bibliothèque Google Publisher Tag (GPT) setConfig et getConfig.
Les API setConfig et getConfig offrent un moyen centralisé de gérer 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 à leurs remplacements setConfig.
Attributs AdSense
Ancienne :
googletag.pubads().set('document_language', 'en');
Nouvelle réponse :
googletag.setConfig({
adsenseAttributes: {
document_language: 'en'
}
});
Exclusion de catégories
Ancienne :
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 :
googletag.pubads().setCentering(true);
Nouvelle réponse :
googletag.setConfig({
centering: true
});
Réduire les divs vides
Ancienne :
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 :
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
Nouvelle réponse :
googletag.setConfig({
disableInitialLoad: true,
singleRequest: true
});
Chargement différé
Ancienne :
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 :
googletag.pubads().setLocation('10001,US');
Nouvelle réponse :
googletag.setConfig({
location: '10001,US'
});
SafeFrame
Ancienne :
googletag.pubads().setForceSafeFrame(true);
googletag.pubads().setSafeFrameConfig({sandbox: true});
Nouvelle réponse :
googletag.pubads().setConfig({
safeFrame: {
forceSafeFrame: true,
sandbox: true
}
});
Ciblage
Ancienne :
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 :
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 à 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 suivi des clics | 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 :
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 :
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 :
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 un élément div vide
Ancienne :
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 :
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 :
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 setConfig |
|---|---|---|
| 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 :
const documentLangauage = 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 :
const isDisabled = googletag.pubads().isInitialLoadDisabled();
Nouvelle réponse :
const isDisabled = googletag.getConfig('disableInitialLoad').disableInitialLoad;
Ciblage
Ancienne :
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 setConfig |
|---|---|---|
| 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 :
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.
const adsenseAttributes = Object.keys(adsenseConfig);
Exclusion de catégories
Ancienne :
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 :
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);