Skip to content

Commit

Permalink
GPT Pre Auction: New logic options - customPreAuction & useDefaultPre…
Browse files Browse the repository at this point in the history
…Auction (#7784)

* updated new logic

* no need to export

* missed a test update
  • Loading branch information
robertrmartinez committed Dec 9, 2021
1 parent 5998842 commit df54d4b
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 9 deletions.
62 changes: 55 additions & 7 deletions modules/gptPreAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
179 changes: 177 additions & 2 deletions test/spec/modules/gptPreAuction_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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
});
});
});
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit df54d4b

Please sign in to comment.