diff --git a/modules/gptPreAuction.js b/modules/gptPreAuction.js index 2dc5a6983b9..88f90419aeb 100644 --- a/modules/gptPreAuction.js +++ b/modules/gptPreAuction.js @@ -48,10 +48,35 @@ const sanitizeSlotPath = (path) => { return path; } +const defaultPreAuction = (adUnit, adServerAdSlot) => { + const context = adUnit.ortb2Imp.ext.data; + + // use pbadslot if supplied + if (context.pbadslot) { + return context.pbadslot; + } + + // confirm that GPT is set up + if (!isGptPubadsDefined()) { + return; + } + + // find all GPT slots with this name + var gptSlots = window.googletag.pubads().getSlots().filter(slot => slot.getAdUnitPath() === adServerAdSlot); + + if (gptSlots.length === 0) { + return; // should never happen + } + + if (gptSlots.length === 1) { + return adServerAdSlot; + } + + // else the adunit code must be div id. append it. + return `${adServerAdSlot}#${adUnit.code}`; +} + export const appendPbAdSlot = adUnit => { - adUnit.ortb2Imp = adUnit.ortb2Imp || {}; - adUnit.ortb2Imp.ext = adUnit.ortb2Imp.ext || {}; - adUnit.ortb2Imp.ext.data = adUnit.ortb2Imp.ext.data || {}; const context = adUnit.ortb2Imp.ext.data; const { customPbAdSlot } = _currentConfig; @@ -84,11 +109,32 @@ export const appendPbAdSlot = adUnit => { export const makeBidRequestsHook = (fn, adUnits, ...args) => { appendGptSlots(adUnits); + const { useDefaultPreAuction, customPreAuction } = _currentConfig; adUnits.forEach(adUnit => { - const usedAdUnitCode = appendPbAdSlot(adUnit); - // gpid should be set to itself if already set, or to what pbadslot was (as long as it was not adUnit code) - if (!adUnit.ortb2Imp.ext.gpid && !usedAdUnitCode) { - adUnit.ortb2Imp.ext.gpid = adUnit.ortb2Imp.ext.data.pbadslot; + // init the ortb2Imp if not done yet + adUnit.ortb2Imp = adUnit.ortb2Imp || {}; + adUnit.ortb2Imp.ext = adUnit.ortb2Imp.ext || {}; + adUnit.ortb2Imp.ext.data = adUnit.ortb2Imp.ext.data || {}; + const context = adUnit.ortb2Imp.ext; + + // if neither new confs set do old stuff + if (!customPreAuction && !useDefaultPreAuction) { + const usedAdUnitCode = appendPbAdSlot(adUnit); + // gpid should be set to itself if already set, or to what pbadslot was (as long as it was not adUnit code) + if (!context.gpid && !usedAdUnitCode) { + context.gpid = context.data.pbadslot; + } + } else { + let adserverSlot = deepAccess(context, 'data.adserver.adslot'); + let result; + if (customPreAuction) { + result = customPreAuction(adUnit, adserverSlot); + } else if (useDefaultPreAuction) { + result = defaultPreAuction(adUnit, adserverSlot); + } + if (result) { + context.gpid = context.data.pbadslot = result; + } } }); return fn.call(this, adUnits, ...args); @@ -100,6 +146,8 @@ const handleSetGptConfig = moduleConfig => { 'customGptSlotMatching', customGptSlotMatching => typeof customGptSlotMatching === 'function' && customGptSlotMatching, 'customPbAdSlot', customPbAdSlot => typeof customPbAdSlot === 'function' && customPbAdSlot, + 'customPreAuction', customPreAuction => typeof customPreAuction === 'function' && customPreAuction, + 'useDefaultPreAuction', useDefaultPreAuction => useDefaultPreAuction === true, ]); if (_currentConfig.enabled) { diff --git a/test/spec/modules/gptPreAuction_spec.js b/test/spec/modules/gptPreAuction_spec.js index 9e81aea80d1..2bfce71806e 100644 --- a/test/spec/modules/gptPreAuction_spec.js +++ b/test/spec/modules/gptPreAuction_spec.js @@ -20,7 +20,9 @@ describe('GPT pre-auction module', () => { const testSlots = [ makeSlot({ code: 'slotCode1', divId: 'div1' }), makeSlot({ code: 'slotCode2', divId: 'div2' }), - makeSlot({ code: 'slotCode3', divId: 'div3' }) + makeSlot({ code: 'slotCode3', divId: 'div3' }), + makeSlot({ code: 'slotCode4', divId: 'div4' }), + makeSlot({ code: 'slotCode4', divId: 'div5' }) ]; describe('appendPbAdSlot', () => { @@ -172,7 +174,9 @@ describe('GPT pre-auction module', () => { expect(_currentConfig).to.deep.equal({ enabled: true, customGptSlotMatching: false, - customPbAdSlot: false + customPbAdSlot: false, + customPreAuction: false, + useDefaultPreAuction: false }); }); }); @@ -266,5 +270,176 @@ describe('GPT pre-auction module', () => { runMakeBidRequests(testAdUnits); expect(returnedAdUnits).to.deep.equal(expectedAdUnits); }); + + it('should use the passed customPreAuction logic', () => { + let counter = 0; + config.setConfig({ + gptPreAuction: { + enabled: true, + customPreAuction: (adUnit, slotName) => { + counter += 1; + return `${adUnit.code}-${slotName || counter}`; + } + } + }); + const testAdUnits = [ + { + code: 'adUnit1', + ortb2Imp: { ext: { data: { pbadslot: '12345' } } } + }, + { + code: 'adUnit2', + }, + { + code: 'slotCode3', + }, + { + code: 'div4', + } + ]; + + // all slots should be passed in same time and have slot-${index} + const expectedAdUnits = [{ + code: 'adUnit1', + ortb2Imp: { + ext: { + // no slotname match so uses adUnit.code-counter + data: { + pbadslot: 'adUnit1-1' + }, + gpid: 'adUnit1-1' + } + } + }, + // second adunit + { + code: 'adUnit2', + ortb2Imp: { + ext: { + // no slotname match so uses adUnit.code-counter + data: { + pbadslot: 'adUnit2-2' + }, + gpid: 'adUnit2-2' + } + } + }, { + code: 'slotCode3', + ortb2Imp: { + ext: { + // slotname found, so uses code + slotname (which is same) + data: { + pbadslot: 'slotCode3-slotCode3', + adserver: { + name: 'gam', + adslot: 'slotCode3' + } + }, + gpid: 'slotCode3-slotCode3' + } + } + }, { + code: 'div4', + ortb2Imp: { + ext: { + // slotname found, so uses code + slotname + data: { + pbadslot: 'div4-slotCode4', + adserver: { + name: 'gam', + adslot: 'slotCode4' + } + }, + gpid: 'div4-slotCode4' + } + } + }]; + + window.googletag.pubads().setSlots(testSlots); + runMakeBidRequests(testAdUnits); + expect(returnedAdUnits).to.deep.equal(expectedAdUnits); + }); + + it('should use useDefaultPreAuction logic', () => { + config.setConfig({ + gptPreAuction: { + enabled: true, + useDefaultPreAuction: true + } + }); + const testAdUnits = [ + // First adUnit should use the preset pbadslot + { + code: 'adUnit1', + ortb2Imp: { ext: { data: { pbadslot: '12345' } } } + }, + // Second adUnit should not match a gam slot, so no slot set + { + code: 'adUnit2', + }, + // third adunit matches a single slot so uses it + { + code: 'slotCode3', + }, + // fourth adunit matches multiple slots so combination + { + code: 'div4', + } + ]; + + const expectedAdUnits = [{ + code: 'adUnit1', + ortb2Imp: { + ext: { + data: { + pbadslot: '12345' + }, + gpid: '12345' + } + } + }, + // second adunit + { + code: 'adUnit2', + ortb2Imp: { + ext: { + data: { + }, + } + } + }, { + code: 'slotCode3', + ortb2Imp: { + ext: { + data: { + pbadslot: 'slotCode3', + adserver: { + name: 'gam', + adslot: 'slotCode3' + } + }, + gpid: 'slotCode3' + } + } + }, { + code: 'div4', + ortb2Imp: { + ext: { + data: { + pbadslot: 'slotCode4#div4', + adserver: { + name: 'gam', + adslot: 'slotCode4' + } + }, + gpid: 'slotCode4#div4' + } + } + }]; + + window.googletag.pubads().setSlots(testSlots); + runMakeBidRequests(testAdUnits); + expect(returnedAdUnits).to.deep.equal(expectedAdUnits); + }); }); });