Skip to content

Commit

Permalink
AdagioRtdProvider: ensure fallback when adUnit.ortb2Imp is missing (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
osazos authored Jun 28, 2024
1 parent 795e92b commit 67774de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 9 additions & 7 deletions modules/adagioRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,18 @@ function onGetBidRequestData(bidReqConfig, callback, config) {

const adUnits = bidReqConfig.adUnits || getGlobal().adUnits || [];
adUnits.forEach(adUnit => {
adUnit.ortb2Imp = adUnit.ortb2Imp || {};
const ortb2Imp = deepAccess(adUnit, 'ortb2Imp');

// A divId is required to compute the slot position and later to track viewability.
// If nothing has been explicitly set, we try to get the divId from the GPT slot and fallback to the adUnit code in last resort.
if (!deepAccess(ortb2Imp, 'ext.data.divId')) {
const divId = getGptSlotInfoForAdUnitCode(adUnit.code).divId;
let divId = deepAccess(ortb2Imp, 'ext.data.divId')
if (!divId) {
divId = getGptSlotInfoForAdUnitCode(adUnit.code).divId;
deepSetValue(ortb2Imp, `ext.data.divId`, divId || adUnit.code);
}

const slotPosition = getSlotPosition(adUnit);
const slotPosition = getSlotPosition(divId);
deepSetValue(ortb2Imp, `ext.data.adg_rtd.adunit_position`, slotPosition);

// It is expected that the publisher set a `adUnits[].ortb2Imp.ext.data.placement` value.
Expand Down Expand Up @@ -430,7 +433,7 @@ function getElementFromTopWindow(element, currentWindow) {
}
};

function getSlotPosition(adUnit) {
function getSlotPosition(divId) {
if (!isSafeFrameWindow() && !canAccessWindowTop()) {
return '';
}
Expand All @@ -451,16 +454,15 @@ function getSlotPosition(adUnit) {
// window.top based computing
const wt = getWindowTop();
const d = wt.document;
const adUnitElementId = deepAccess(adUnit, 'ortb2Imp.ext.data.divId');

let domElement;

if (inIframe() === true) {
const ws = getWindowSelf();
const currentElement = ws.document.getElementById(adUnitElementId);
const currentElement = ws.document.getElementById(divId);
domElement = getElementFromTopWindow(currentElement, ws);
} else {
domElement = wt.document.getElementById(adUnitElementId);
domElement = wt.document.getElementById(divId);
}

if (!domElement) {
Expand Down
12 changes: 12 additions & 0 deletions test/spec/modules/adagioRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,18 @@ describe('Adagio Rtd Provider', function () {
expect(bidRequest.adUnits[0]).to.have.property('ortb2Imp');
expect(bidRequest.adUnits[0].ortb2Imp.ext.data.placement).to.not.exist;
});

it('ensure we create the `ortb2Imp` object if it does not exist', function() {
const configCopy = utils.deepClone(config);
configCopy.params.placementSource = PLACEMENT_SOURCES.ADUNITCODE;

const bidRequest = utils.deepClone(bidReqConfig);
delete bidRequest.adUnits[0].ortb2Imp;

adagioRtdSubmodule.getBidRequestData(bidRequest, cb, configCopy);
expect(bidRequest.adUnits[0]).to.have.property('ortb2Imp');
expect(bidRequest.adUnits[0].ortb2Imp.ext.data.placement).to.equal('div-gpt-ad-1460505748561-0');
});
});
});

Expand Down

0 comments on commit 67774de

Please sign in to comment.