W tym przewodniku znajdziesz przykłady migracji ze starszych metod konfiguracji do nowej biblioteki tagów wydawcy Google (GPT) setConfig i getConfig interfejsów API.
Interfejsy API setConfig i getConfig zapewniają scentralizowany sposób zarządzania konfiguracją na poziomie strony i miejsca na reklamę.
Ustawianie konfiguracji na poziomie strony
W tabeli poniżej znajdziesz mapowanie starszych metod konfiguracji PubAdsService na ich zamienniki setConfig.
Atrybuty AdSense
Starsza wersja:
googletag.pubads().set('document_language', 'en');
Nowość:
googletag.setConfig({
adsenseAttributes: {
document_language: 'en'
}
});
Wykluczanie kategorii
Starsza wersja:
googletag.pubads().setCategoryExclusion('AirlineAd');
googletag.pubads().clearCategoryExclusions();
Nowość:
// Set category exclusion
googletag.setConfig({
categoryExclusion: ['AirlineAd']
});
// Clear category exclusions
googletag.setConfig({
categoryExclusion: null
});
Wyśrodkowanie
Starsza wersja:
googletag.pubads().setCentering(true);
Nowość:
googletag.setConfig({
centering: true
});
Zwijanie pustych elementów div
Starsza wersja:
googletag.pubads().collapseEmptyDivs(true); // Collapse before fetch
googletag.pubads().collapseEmptyDivs(false); // Collapse on no fill
Nowość:
// 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'
});
Wczytywanie początkowe i architektura z pojedynczym żądaniem (SRA)
Starsza wersja:
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
Nowość:
googletag.setConfig({
disableInitialLoad: true,
singleRequest: true
});
Leniwe ładowanie
Starsza wersja:
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,
});
Nowość:
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,
},
});
Lokalizacja
Starsza wersja:
googletag.pubads().setLocation('10001,US');
Nowość:
googletag.setConfig({
location: '10001,US'
});
SafeFrame
Starsza wersja:
googletag.pubads().setForceSafeFrame(true);
googletag.pubads().setSafeFrameConfig({sandbox: true});
Nowość:
googletag.pubads().setConfig({
safeFrame: {
forceSafeFrame: true,
sandbox: true
}
});
Kierowanie
Starsza wersja:
googletag.pubads().setTargeting('interests', 'sports');
googletag.pubads().setTargeting('interests', ['sports', 'music']);
googletag.pubads().clearTargeting('interests');
googletag.pubads().clearTargeting();
Nowość:
// 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
});
Reklamy wideo
Starsza wersja:
googletag.pubads().enableVideoAds();
googletag.pubads().setVideoContent('video123', 'cms456');
Nowość:
googletag.setConfig({
videoAds: {
enableVideoAds: true,
videoContentId: 'video123',
videoCmsId: 'cms456'
}
});
Ustawianie konfiguracji na poziomie boksu
W tabeli poniżej znajdziesz mapowanie starszych metod konfiguracji Slot na ich zamienniki setConfig.
| Funkcja | Starsza metoda | setConfig – zamiennik |
|---|---|---|
| Atrybuty AdSense | set(key, value) |
Slot.setConfig({ adsenseAttributes: { [key]: value } })
|
| Wykluczanie kategorii | clearCategoryExclusions() |
Slot.setConfig({ categoryExclusion: null })
|
setCategoryExclusion(label) |
Slot.setConfig({ categoryExclusion: [label] })
|
|
| Klikany URL | setClickUrl(url) |
Slot.setConfig({ clickUrl: url })
|
| Zwijanie pustych elementów div | 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 })
|
|
| Kierowanie | clearTargeting(key) |
Slot.setConfig({ targeting: { [key]: null } })
|
setTargeting(key, value) |
Slot.setConfig({ targeting: { [key]: value } })
|
|
updateTargetingFromMap(config) |
Slot.setConfig({ targeting: config })
|
Atrybuty AdSense
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.set('adsense_background_color', '#FFFFFF');
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setConfig({
adsenseAttributes: {
adsense_background_color: '#FFFFFF'
}
});
Wykluczanie kategorii
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setCategoryExclusion('AirlineAd');
slot.clearCategoryExclusions();
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
// Set category exclusion
slot.setConfig({
categoryExclusion: ['AirlineAd']
});
// Clear category exclusions
slot.setConfig({
categoryExclusion: null
});
Kliknij adres URL
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setClickUrl('http://www.example.com?original_click_url=');
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setConfig({
clickUrl: 'http://www.example.com?original_click_url='
});
Zwijanie pustego elementu div
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setCollapseEmptyDiv(true, true); // Collapse before fetch
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setConfig({
collapseDiv: 'BEFORE_FETCH'
});
SafeFrame
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setForceSafeFrame(true);
slot.setSafeFrameConfig({sandbox: true});
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
slot.setConfig({
safeFrame: {
forceSafeFrame: true,
sandbox: true
}
});
Kierowanie
Starsza wersja:
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']
});
Nowość:
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']
}
})
Pobieranie konfiguracji na poziomie strony
W tabeli poniżej znajdziesz mapowanie starszych metod pobierania PubAdsService na ich zamienniki getConfig.
| Funkcja | Starsza metoda | setConfig – zamiennik |
|---|---|---|
| Atrybuty AdSense | get(key) |
googletag.getConfig('adsenseAttributes')
|
getAttributeKeys() |
googletag.getConfig('adsenseAttributes')
|
|
| Wczytywanie początkowe | isInitialLoadDisabled() |
googletag.getConfig('disableInitialLoad')
|
| Kierowanie | getTargeting(key) |
googletag.getConfig('targeting')
|
getTargetingKeys() |
googletag.getConfig('targeting')
|
Atrybuty AdSense
Starsza wersja:
const documentLangauage = googletag.pubads().get('document_language');
const adsenseAttributes = googletag.pubads().getAttributeKeys();
Nowość:
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);
Wczytywanie początkowe
Starsza wersja:
const isDisabled = googletag.pubads().isInitialLoadDisabled();
Nowość:
const isDisabled = googletag.getConfig('disableInitialLoad').disableInitialLoad;
Kierowanie
Starsza wersja:
const targeting = googletag.pubads().getTargeting('interests');
const keys = googletag.pubads().getTargetingKeys();
Nowość:
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);
Pobieranie konfiguracji na poziomie boksu
W tabeli poniżej znajdziesz mapowanie starszych metod pobierania Slot na ich zamienniki getConfig.
| Funkcja | Starsza metoda | setConfig – zamiennik |
|---|---|---|
| Atrybuty AdSense | get(key) |
Slot.getConfig('adsenseAttributes')
|
getAttributeKeys() |
Slot.getConfig('adsenseAttributes')
|
|
| Wykluczanie kategorii | getCategoryExclusions() |
Slot.getConfig('categoryExclusion')
|
| Kierowanie | getTargeting(key) |
Slot.getConfig('targeting')
|
getTargetingKeys() |
Slot.getConfig('targeting')
|
Atrybuty AdSense
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const bgColor = slot.get('adsense_background_color');
const adsenseAttributes = slot.getAttributeKeys();
Nowość:
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);
Wykluczanie kategorii
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const exclusions = slot.getCategoryExclusions();
Nowość:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const exclusions = slot.getConfig('categoryExclusion').categoryExclusion || [];
Kierowanie
Starsza wersja:
const slot = googletag.defineSlot('/1234567/sports', [160, 600], 'div');
const targeting = slot.getTargeting('allow_expandable');
const keys = slot.getTargetingKeys();
Nowość:
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);