Skip to content

Commit

Permalink
gptPreAuction: fix missing gpid when using mcmEnabled (#12361)
Browse files Browse the repository at this point in the history
* gptPreAuction: fix missing gpid when using mcmEnabled

* Fix npe when gpt is not present
  • Loading branch information
dgirardi authored Oct 23, 2024
1 parent 91587a1 commit 0c310c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
16 changes: 10 additions & 6 deletions modules/gptPreAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ export const appendGptSlots = adUnits => {
return acc;
}, {});

const adUnitPaths = {};

window.googletag.pubads().getSlots().forEach(slot => {
const matchingAdUnitCode = find(Object.keys(adUnitMap), customGptSlotMatching
? customGptSlotMatching(slot)
: isAdUnitCodeMatchingSlot(slot));

if (matchingAdUnitCode) {
const path = adUnitPaths[matchingAdUnitCode] = slot.getAdUnitPath();
const adserver = {
name: 'gam',
adslot: sanitizeSlotPath(slot.getAdUnitPath())
adslot: sanitizeSlotPath(path)
};
adUnitMap[matchingAdUnitCode].forEach((adUnit) => {
deepSetValue(adUnit, 'ortb2Imp.ext.data.adserver', Object.assign({}, adUnit.ortb2Imp?.ext?.data?.adserver, adserver));
});
}
});
return adUnitPaths;
};

const sanitizeSlotPath = (path) => {
Expand All @@ -103,7 +107,7 @@ const sanitizeSlotPath = (path) => {
return path;
}

const defaultPreAuction = (adUnit, adServerAdSlot) => {
const defaultPreAuction = (adUnit, adServerAdSlot, adUnitPath) => {
const context = adUnit.ortb2Imp.ext.data;

// use pbadslot if supplied
Expand All @@ -117,7 +121,7 @@ const defaultPreAuction = (adUnit, adServerAdSlot) => {
}

// find all GPT slots with this name
var gptSlots = window.googletag.pubads().getSlots().filter(slot => slot.getAdUnitPath() === adServerAdSlot);
var gptSlots = window.googletag.pubads().getSlots().filter(slot => slot.getAdUnitPath() === adUnitPath);

if (gptSlots.length === 0) {
return; // should never happen
Expand Down Expand Up @@ -167,7 +171,7 @@ function warnDeprecation(adUnit) {
}

export const makeBidRequestsHook = (fn, adUnits, ...args) => {
appendGptSlots(adUnits);
const adUnitPaths = appendGptSlots(adUnits);
const { useDefaultPreAuction, customPreAuction } = _currentConfig;
adUnits.forEach(adUnit => {
// init the ortb2Imp if not done yet
Expand All @@ -190,9 +194,9 @@ export const makeBidRequestsHook = (fn, adUnits, ...args) => {
let adserverSlot = deepAccess(context, 'data.adserver.adslot');
let result;
if (customPreAuction) {
result = customPreAuction(adUnit, adserverSlot);
result = customPreAuction(adUnit, adserverSlot, adUnitPaths?.[adUnit.code]);
} else if (useDefaultPreAuction) {
result = defaultPreAuction(adUnit, adserverSlot);
result = defaultPreAuction(adUnit, adserverSlot, adUnitPaths?.[adUnit.code]);
}
if (result) {
context.gpid = context.data.pbadslot = result;
Expand Down
48 changes: 48 additions & 0 deletions test/spec/modules/gptPreAuction_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ describe('GPT pre-auction module', () => {
expect(adUnit.ortb2Imp.ext.data.adserver).to.deep.equal({ name: 'gam', adslot: '/12345/slotCode2' });
});

it('returns full ad unit path even if mcmEnabled is true', () => {
config.setConfig({ gptPreAuction: { enabled: true, mcmEnabled: true } });
window.googletag.pubads().setSlots([
makeSlot({ code: '/12345,21212/slot', divId: 'div1' }),
]);
const adUnit = {code: '/12345,21212/slot'};
expect(appendGptSlots([adUnit])).to.eql({
'/12345,21212/slot': '/12345,21212/slot'
})
})

it('will not trim child id if mcmEnabled is not set to true', () => {
window.googletag.pubads().setSlots([
makeSlot({ code: '/12345,21212/slotCode1', divId: 'div1' }),
Expand Down Expand Up @@ -459,6 +470,33 @@ describe('GPT pre-auction module', () => {
expect(returnedAdUnits).to.deep.equal(expectedAdUnits);
});

it('should pass full slot path to customPreAuction when mcmEnabled is true', () => {
const customPreAuction = sinon.stub();
config.setConfig({
gptPreAuction: {
enabled: true,
mcmEnabled: true,
customPreAuction
}
});
window.googletag.pubads().setSlots([
makeSlot({ code: '/12345,21212/slot', divId: 'div1' }),
]);
const adUnit = {code: '/12345,21212/slot'};
makeBidRequestsHook(sinon.stub(), [adUnit]);
sinon.assert.calledWith(customPreAuction, adUnit, '/12345/slot', adUnit.code);
});

it('should not choke if gpt is not available', () => {
config.setConfig({
gptPreAuction: {
enabled: true
}
});
sandbox.stub(window, 'googletag').value(null);
makeBidRequestsHook(sinon.stub(), [{}]);
})

it('should use useDefaultPreAuction logic', () => {
config.setConfig({
gptPreAuction: {
Expand Down Expand Up @@ -540,6 +578,16 @@ describe('GPT pre-auction module', () => {
runMakeBidRequests(testAdUnits);
expect(returnedAdUnits).to.deep.equal(expectedAdUnits);
});

it('sets gpid when mcmEnabled: true', () => {
config.setConfig({ gptPreAuction: { enabled: true, mcmEnabled: true } });
window.googletag.pubads().setSlots([
makeSlot({ code: '/12345,21212/slot', divId: 'div1' }),
]);
const adUnit = {code: '/12345,21212/slot'};
makeBidRequestsHook(sinon.stub(), [adUnit]);
expect(adUnit.ortb2Imp.ext.gpid).to.eql('/12345/slot');
});
});

describe('pps gpt config', () => {
Expand Down

0 comments on commit 0c310c3

Please sign in to comment.