Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions modules/gptPreAuction.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
import {
deepAccess,
isAdUnitCodeMatchingSlot,
isGptPubadsDefined,
logInfo,
pick,
deepSetValue
} from '../src/utils.js';
import {config} from '../src/config.js';
import {getHook} from '../src/hook.js';
import {find} from '../src/polyfill.js';

const MODULE_NAME = 'GPT Pre-Auction';
export let _currentConfig = {};
let hooksAdded = false;

export const appendGptSlots = adUnits => {
const { customGptSlotMatching } = _currentConfig;
window.adpushup = window.adpushup || {};
const adp = window.adpushup;
const adpConfig = adp.config || {};

if (!isGptPubadsDefined()) {
return;
window.adpTags = window.adpTags || {};
const adpTags = window.adpTags;
const adpSlots = adpTags.adpSlots || {};

const getAdUnitPath = function(code) {
const adpSlot = adpSlots[code];
if (!adpSlot) return null;
const {childPublisherId,isMcmEnabled=false } = adpConfig.mcm;

let dfpNetwork = adpSlot.activeDFPNetwork;
if (isMcmEnabled) {
dfpNetwork += `,${childPublisherId}`;
}
const dfpAdUnitCode = adpSlot.currentGptSlotData && adpSlot.currentGptSlotData.dfpAdunitCode;
return `/${dfpNetwork}/${dfpAdUnitCode}`;
}

export const appendGptSlots = adUnits => {
if (!adpSlots) {
return;
}
const adUnitMap = adUnits.reduce((acc, adUnit) => {
acc[adUnit.code] = acc[adUnit.code] || [];
acc[adUnit.code].push(adUnit);
return acc;
}, {});

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

for (adUnit in adpSlots) {
const matchingAdUnitCode = Object.keys(adUnitMap).find((key) => key === adUnit);
if (matchingAdUnitCode) {
const adserver = {
name: 'gam',
adslot: sanitizeSlotPath(slot.getAdUnitPath())
name: "gam",
adslot: sanitizeSlotPath(getAdUnitPath(adUnit)),
};
adUnitMap[matchingAdUnitCode].forEach((adUnit) => {
deepSetValue(adUnit, 'ortb2Imp.ext.data.adserver', Object.assign({}, adUnit.ortb2Imp?.ext?.data?.adserver, adserver));
deepSetValue(
adUnit,
"ortb2Imp.ext.data.adserver",
Object.assign({}, adUnit.ortb2Imp?.ext?.data?.adserver, adserver)
);
});
}
});
}
};

const sanitizeSlotPath = (path) => {
Expand All @@ -62,13 +78,11 @@ const defaultPreAuction = (adUnit, adServerAdSlot) => {
return context.pbadslot;
}

// confirm that GPT is set up
if (!isGptPubadsDefined()) {
return;
if (!adpSlots) {
return
}

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

if (gptSlots.length === 0) {
return; // should never happen
Expand Down