Skip to content

Commit

Permalink
GPT Pre Auction: setting GPID (prebid#7671)
Browse files Browse the repository at this point in the history
* set gpid

* fix gptPreAuction tests

* clean logic + add test
  • Loading branch information
robertrmartinez authored and Chris Pabst committed Jan 10, 2022
1 parent 5c30156 commit b10139c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
16 changes: 11 additions & 5 deletions modules/gptPreAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ export const appendPbAdSlot = adUnit => {
const context = adUnit.ortb2Imp.ext.data;
const { customPbAdSlot } = _currentConfig;

if (customPbAdSlot) {
context.pbadslot = customPbAdSlot(adUnit.code, deepAccess(context, 'adserver.adslot'));
// use context.pbAdSlot if set (if someone set it already, it will take precedence over others)
if (context.pbadslot) {
return;
}

// use context.pbAdSlot if set
if (context.pbadslot) {
if (customPbAdSlot) {
context.pbadslot = customPbAdSlot(adUnit.code, deepAccess(context, 'adserver.adslot'));
return;
}

// use data attribute 'data-adslotid' if set
try {
const adUnitCodeDiv = document.getElementById(adUnit.code);
Expand All @@ -78,12 +79,17 @@ export const appendPbAdSlot = adUnit => {
return;
}
context.pbadslot = adUnit.code;
return true;
};

export const makeBidRequestsHook = (fn, adUnits, ...args) => {
appendGptSlots(adUnits);
adUnits.forEach(adUnit => {
appendPbAdSlot(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;
}
});
return fn.call(this, adUnits, ...args);
};
Expand Down
62 changes: 58 additions & 4 deletions test/spec/modules/gptPreAuction_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,69 @@ describe('GPT pre-auction module', () => {
code: 'slotCode3',
}];

// first two adUnits directly pass in pbadslot => gpid is same
const expectedAdUnits = [{
code: 'adUnit1',
ortb2Imp: { ext: { data: { pbadslot: '12345' } } }
}, {
ortb2Imp: {
ext: {
data: {
pbadslot: '12345'
},
gpid: '12345'
}
}
},
// second adunit
{
code: 'slotCode1',
ortb2Imp: { ext: { data: { pbadslot: '67890', adserver: { name: 'gam', adslot: 'slotCode1' } } } }
ortb2Imp: {
ext: {
data: {
pbadslot: '67890',
adserver: {
name: 'gam',
adslot: 'slotCode1'
}
},
gpid: '67890'
}
}
}, {
code: 'slotCode3',
ortb2Imp: { ext: { data: { pbadslot: 'slotCode3', adserver: { name: 'gam', adslot: 'slotCode3' } } } }
ortb2Imp: {
ext: {
data: {
pbadslot: 'slotCode3',
adserver: {
name: 'gam',
adslot: 'slotCode3'
}
},
gpid: 'slotCode3'
}
}
}];

window.googletag.pubads().setSlots(testSlots);
runMakeBidRequests(testAdUnits);
expect(returnedAdUnits).to.deep.equal(expectedAdUnits);
});

it('should not apply gpid if pbadslot was set by adUnitCode', () => {
const testAdUnits = [{
code: 'noMatchCode',
}];

// first two adUnits directly pass in pbadslot => gpid is same
const expectedAdUnits = [{
code: 'noMatchCode',
ortb2Imp: {
ext: {
data: {
pbadslot: 'noMatchCode'
},
}
}
}];

window.googletag.pubads().setSlots(testSlots);
Expand Down

0 comments on commit b10139c

Please sign in to comment.