forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoedgeRtdProvider.js
293 lines (267 loc) · 7.56 KB
/
geoedgeRtdProvider.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* This module adds geoedge provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will fetch creative wrapper from geoedge server
* The module will place geoedge RUM client on bid responses markup
* @module modules/geoedgeProvider
* @requires module:modules/realTimeData
*/
/**
* @typedef {Object} ModuleParams
* @property {string} key
* @property {?Object} bidders
* @property {?boolean} wap
* @property {?string} keyName
*/
import { submodule } from '../src/hook.js';
import { ajax } from '../src/ajax.js';
import { generateUUID, createInvisibleIframe, insertElement, isEmpty, logError } from '../src/utils.js';
import * as events from '../src/events.js';
import { EVENTS } from '../src/constants.js';
import { loadExternalScript } from '../src/adloader.js';
import { auctionManager } from '../src/auctionManager.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
/** @type {string} */
const SUBMODULE_NAME = 'geoedge';
/** @type {string} */
export const WRAPPER_URL = 'https://wrappers.geoedge.be/wrapper.html';
/** @type {string} */
/* eslint-disable no-template-curly-in-string */
export const HTML_PLACEHOLDER = '${creative}';
/** @type {string} */
const PV_ID = generateUUID();
/** @type {string} */
const HOST_NAME = 'https://rumcdn.geoedge.be';
/** @type {string} */
const FILE_NAME_CLIENT = 'grumi.js';
/** @type {string} */
const FILE_NAME_INPAGE = 'grumi-ip.js';
/** @type {function} */
export let getClientUrl = (key) => `${HOST_NAME}/${key}/${FILE_NAME_CLIENT}`;
/** @type {function} */
export let getInPageUrl = (key) => `${HOST_NAME}/${key}/${FILE_NAME_INPAGE}`;
/** @type {string} */
export let wrapper
/** @type {boolean} */;
let wrapperReady;
/** @type {boolean} */;
let preloaded;
/** @type {object} */;
let refererInfo = getRefererInfo();
/** @type {object} */;
let overrides = window.grumi?.overrides;
/**
* fetches the creative wrapper
* @param {function} success - success callback
*/
export function fetchWrapper(success) {
if (wrapperReady) {
return success(wrapper);
}
ajax(WRAPPER_URL, success);
}
/**
* sets the wrapper and calls preload client
* @param {string} responseText
*/
export function setWrapper(responseText) {
wrapperReady = true;
wrapper = responseText;
}
export function getInitialParams(key) {
let params = {
wver: '1.1.1',
wtype: 'pbjs-module',
key,
meta: {
topUrl: refererInfo.page
},
site: refererInfo.domain,
pimp: PV_ID,
fsRan: true,
frameApi: true
};
return params;
}
export function markAsLoaded() {
preloaded = true;
}
/**
* preloads the client
* @param {string} key
*/
export function preloadClient(key) {
let iframe = createInvisibleIframe();
iframe.id = 'grumiFrame';
insertElement(iframe);
iframe.contentWindow.grumi = getInitialParams(key);
let url = getClientUrl(key);
loadExternalScript(url, MODULE_TYPE_RTD, SUBMODULE_NAME, markAsLoaded, iframe.contentDocument);
}
/**
* creates identity function for string replace without special replacement patterns
* @param {string} str
* @return {function}
*/
function replacer(str) {
return function () {
return str;
}
}
export function wrapHtml(wrapper, html) {
return wrapper.replace(HTML_PLACEHOLDER, replacer(html));
}
/**
* generate macros dictionary from bid response
* @param {Object} bid
* @param {string} key
* @return {Object}
*/
export function getMacros(bid, key) {
return {
'${key}': key,
'%%ADUNIT%%': bid.adUnitCode,
'%%WIDTH%%': bid.width,
'%%HEIGHT%%': bid.height,
'%%PATTERN:hb_adid%%': bid.adId,
'%%PATTERN:hb_bidder%%': bid.bidderCode,
'%_isHb!': true,
'%_hbcid!': bid.creativeId || '',
'%_hbadomains': bid.meta && bid.meta.advertiserDomains,
'%%PATTERN:hb_pb%%': bid.pbHg,
'%%SITE%%': overrides?.site || refererInfo.domain,
'%_pimp%': PV_ID,
'%_hbCpm!': bid.cpm,
'%_hbCurrency!': bid.currency
};
}
/**
* replace macro placeholders in a string with values from a dictionary
* @param {string} wrapper
* @param {Object} macros
* @return {string}
*/
function replaceMacros(wrapper, macros) {
var re = new RegExp('\\' + Object.keys(macros).join('|'), 'gi');
return wrapper.replace(re, function(matched) {
return macros[matched];
});
}
/**
* build final creative html with creative wrapper
* @param {Object} bid
* @param {string} wrapper
* @param {string} html
* @return {string}
*/
function buildHtml(bid, wrapper, html, key) {
let macros = getMacros(bid, key);
wrapper = replaceMacros(wrapper, macros);
return wrapHtml(wrapper, html);
}
/**
* muatates the bid ad property
* @param {Object} bid
* @param {string} ad
*/
function mutateBid(bid, ad) {
bid.ad = ad;
}
/**
* wraps a bid object with the creative wrapper
* @param {Object} bid
* @param {string} key
*/
export function wrapBidResponse(bid, key) {
let wrapped = buildHtml(bid, wrapper, bid.ad, key);
mutateBid(bid, wrapped);
}
/**
* checks if bidder's bids should be monitored
* @param {string} bidder
* @return {boolean}
*/
function isSupportedBidder(bidder, paramsBidders) {
return isEmpty(paramsBidders) || paramsBidders[bidder] === true;
}
/**
* checks if bid should be monitored
* @param {Object} bid
* @return {boolean}
*/
function shouldWrap(bid, params) {
let supportedBidder = isSupportedBidder(bid.bidderCode, params.bidders);
let donePreload = params.wap ? preloaded : true;
let isGPT = params.gpt;
return wrapperReady && supportedBidder && donePreload && !isGPT;
}
function conditionallyWrap(bidResponse, config, userConsent) {
let params = config.params;
if (shouldWrap(bidResponse, params)) {
wrapBidResponse(bidResponse, params.key);
}
}
function isBillingMessage(data, params) {
return data.key === params.key && data.impression;
}
/**
* Fire billable events when our client sends a message
* Messages will be sent only when:
* a. applicable bids are wrapped
* b. our code laoded and executed sucesfully
*/
function fireBillableEventsForApplicableBids(params) {
window.addEventListener('message', function (message) {
let data = message.data;
if (isBillingMessage(data, params)) {
let winningBid = auctionManager.findBidByAdId(data.adId);
events.emit(EVENTS.BILLABLE_EVENT, {
vendor: SUBMODULE_NAME,
billingId: data.impressionId,
type: winningBid ? 'impression' : data.type,
transactionId: winningBid?.transactionId || data.transactionId,
auctionId: winningBid?.auctionId || data.auctionId,
bidId: winningBid?.requestId || data.requestId
});
}
});
}
/**
* Loads Geoedge in page script that monitors all ad slots created by GPT
* @param {Object} params
*/
function setupInPage(params) {
window.grumi = params;
window.grumi.fromPrebid = true;
loadExternalScript(getInPageUrl(params.key), MODULE_TYPE_RTD, SUBMODULE_NAME);
}
function init(config, userConsent) {
let params = config.params;
if (!params || !params.key) {
logError('missing key for geoedge RTD module provider');
return false;
}
if (params.gpt) {
setupInPage(params);
} else {
fetchWrapper(setWrapper);
preloadClient(params.key);
}
fireBillableEventsForApplicableBids(params);
return true;
}
/** @type {RtdSubmodule} */
export const geoedgeSubmodule = {
/**
* used to link submodule with realTimeData
* @type {string}
*/
name: SUBMODULE_NAME,
init,
onBidResponseEvent: conditionallyWrap
};
submodule('realTimeData', geoedgeSubmodule);