forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptPreAuction.js
167 lines (141 loc) · 5.14 KB
/
gptPreAuction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { isGptPubadsDefined, isAdUnitCodeMatchingSlot, deepAccess, pick, logInfo } from '../src/utils.js';
import { config } from '../src/config.js';
import { getHook } from '../src/hook.js';
import find from 'core-js-pure/features/array/find.js';
const MODULE_NAME = 'GPT Pre-Auction';
export let _currentConfig = {};
let hooksAdded = false;
export const appendGptSlots = adUnits => {
const { customGptSlotMatching } = _currentConfig;
if (!isGptPubadsDefined()) {
return;
}
const adUnitMap = adUnits.reduce((acc, adUnit) => {
acc[adUnit.code] = adUnit;
return acc;
}, {});
window.googletag.pubads().getSlots().forEach(slot => {
const matchingAdUnitCode = find(Object.keys(adUnitMap), customGptSlotMatching
? customGptSlotMatching(slot)
: isAdUnitCodeMatchingSlot(slot));
if (matchingAdUnitCode) {
const adUnit = adUnitMap[matchingAdUnitCode];
adUnit.ortb2Imp = adUnit.ortb2Imp || {};
adUnit.ortb2Imp.ext = adUnit.ortb2Imp.ext || {};
adUnit.ortb2Imp.ext.data = adUnit.ortb2Imp.ext.data || {};
const context = adUnit.ortb2Imp.ext.data;
context.adserver = context.adserver || {};
context.adserver.name = 'gam';
context.adserver.adslot = sanitizeSlotPath(slot.getAdUnitPath());
}
});
};
const sanitizeSlotPath = (path) => {
const gptConfig = config.getConfig('gptPreAuction') || {};
if (gptConfig.mcmEnabled) {
return path.replace(/(^\/\d*),\d*\//, '$1/');
}
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 => {
const context = adUnit.ortb2Imp.ext.data;
const { customPbAdSlot } = _currentConfig;
// use context.pbAdSlot if set (if someone set it already, it will take precedence over others)
if (context.pbadslot) {
return;
}
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);
if (adUnitCodeDiv.dataset.adslotid) {
context.pbadslot = adUnitCodeDiv.dataset.adslotid;
return;
}
} catch (e) {}
// banner adUnit, use GPT adunit if defined
if (deepAccess(context, 'adserver.adslot')) {
context.pbadslot = context.adserver.adslot;
return;
}
context.pbadslot = adUnit.code;
return true;
};
export const makeBidRequestsHook = (fn, adUnits, ...args) => {
appendGptSlots(adUnits);
const { useDefaultPreAuction, customPreAuction } = _currentConfig;
adUnits.forEach(adUnit => {
// 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);
};
const handleSetGptConfig = moduleConfig => {
_currentConfig = pick(moduleConfig, [
'enabled', enabled => enabled !== false,
'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) {
if (!hooksAdded) {
getHook('makeBidRequests').before(makeBidRequestsHook);
hooksAdded = true;
}
} else {
logInfo(`${MODULE_NAME}: Turning off module`);
_currentConfig = {};
getHook('makeBidRequests').getHooks({hook: makeBidRequestsHook}).remove();
hooksAdded = false;
}
};
config.getConfig('gptPreAuction', config => handleSetGptConfig(config.gptPreAuction));
handleSetGptConfig({});