forked from indexexchange/open-x-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-x-htb.js
420 lines (340 loc) · 14.1 KB
/
open-x-htb.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* @author: Partner
* @license: UNLICENSED
*
* @copyright: Copyright (c) 2017 by Index Exchange. All rights reserved.
*
* The information contained within this document is confidential, copyrighted
* and or a trade secret. No part of this document may be reproduced or
* distributed in any form or by any means, in whole or in part, without the
* prior written permission of Index Exchange.
*/
'use strict';
////////////////////////////////////////////////////////////////////////////////
// Dependencies ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var Browser = require('browser.js');
var Classify = require('classify.js');
var Constants = require('constants.js');
var Network = require('network.js');
var Partner = require('partner.js');
var Size = require('size.js');
var SpaceCamp = require('space-camp.js');
var System = require('system.js');
var Utilities = require('utilities.js');
var Whoopsie = require('whoopsie.js');
var EventsService;
var RenderService;
//? if (DEBUG) {
var ConfigValidators = require('config-validators.js');
var PartnerSpecificValidator = require('open-x-htb-validator.js');
var Scribe = require('scribe.js');
//? }
////////////////////////////////////////////////////////////////////////////////
// Main ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Partner module template
*
* @class
*/
function OpenXHtb(configs) {
/* =====================================
* Data
* ---------------------------------- */
/* Private
* ---------------------------------- */
/**
* Reference to the partner base class.
*
* @private {object}
*/
var __baseClass;
/**
* Profile for this partner.
*
* @private {object}
*/
var __profile;
var __baseAdRequestUrl;
var __baseBeaconUrl;
/* =====================================
* Functions
* ---------------------------------- */
/* Utilities
* ---------------------------------- */
function __fireBeacon(sessionId, data) {
var networkParams = {
url: __baseBeaconUrl,
data: data,
method: 'GET',
sessionId: sessionId,
//? if (DEBUG) {
initiatorId: __profile.partnerId
//? }
};
if (Network.isXhrSupported()) {
Network.ajax(networkParams);
} else {
Network.img(networkParams);
}
}
/**
* Generates the request URL and query data to the endpoint for the xSlots
* in the given returnParcels.
*
* @param {object[]} returnParcels
*
* @return {object}
*/
function __generateRequestObj(returnParcels) {
var callbackId = '_' + System.generateUniqueId();
var auidString = '';
var ausString = '';
for (var i = 0; i < returnParcels.length; i++) {
auidString += returnParcels[i].xSlotRef.adUnitId.toString() + ',';
ausString += Size.arrayToString(returnParcels[i].xSlotRef.sizes, ',') + '|';
}
auidString = auidString.slice(0, -1);
ausString = ausString.slice(0, -1);
var queryObj = {
auid: auidString,
aus: ausString,
ju: Browser.getPageUrl(),
jr: Browser.getPageUrl(),
ch: configs.charset,
tz: System.getTimezoneOffset(),
bc: configs.bidderCode,
be: configs.trackingEnabled ? 1 : 0,
res: Size.arrayToString([
[Browser.getScreenWidth(), Browser.getScreenHeight()]
]),
tws: Size.arrayToString([
[Browser.getViewportWidth(), Browser.getViewportHeight()]
]),
ifr: Browser.isTopFrame() ? 0 : 1,
callback: 'window.' + SpaceCamp.NAMESPACE + '.OpenXHtb.adResponseCallbacks.' + callbackId,
cache: new Date().getTime()
};
return {
url: __baseAdRequestUrl,
data: queryObj,
callbackId: callbackId
};
}
/* Helpers
* ---------------------------------- */
/* Parses and extracts demand from adResponse according to the adapter and then attaches it
* to the corresponding bid's returnParcel in the correct format using targeting keys.
*/
function __parseResponse(sessionId, adResponse, returnParcels, outstandingXSlotNames, startTime, endTime, timedOut) {
var unusedReturnParcels = returnParcels.slice();
var ads = adResponse.ads;
if (!ads || !ads.ad || !Utilities.isArray(ads.ad)) {
EventsService.emit('internal_error', __profile.partnerId + ' invalid ad response');
if (__profile.enabledAnalytics.requestTime && !timedOut) {
__baseClass._emitStatsEvent(sessionId, 'hs_slot_error', outstandingXSlotNames);
}
return;
}
if (ads.pixels) {
Browser.createHiddenIFrame(ads.pixels);
}
var bids = ads.ad;
for (var i = 0; i < bids.length; i++) {
var curBid = bids[i];
var curReturnParcel;
/* adUnitId are strings in the configuration, but the openX endpoint return them as numbers */
bids[i].adunitid = String(bids[i].adunitid);
for (var j = unusedReturnParcels.length - 1; j >= 0; j--) {
if (unusedReturnParcels[j].xSlotRef.adUnitId === bids[i].adunitid) {
curReturnParcel = unusedReturnParcels[j];
unusedReturnParcels.splice(j, 1);
break;
}
}
if (!curReturnParcel) {
continue;
}
var curHtSlotId = curReturnParcel.htSlot.getId();
var beaconData = {};
if (curBid.ts) {
beaconData.ts = curBid.ts;
}
beaconData.bt = configs.timeout || 0;
beaconData.bd = endTime - startTime;
beaconData.br = timedOut ? 't' : 'p';
beaconData.bs = Browser.getHostname();
if (!curBid.pub_rev || !curBid.html || !curBid.creative || !curBid.creative.length) { //jshint ignore:line
EventsService.emit('internal_error', __profile.partnerId + ' invalid ad response');
if (__profile.enabledAnalytics.requestTime && !timedOut) {
EventsService.emit('hs_slot_error', {
sessionId: sessionId,
statsId: __profile.statsId,
htSlotId: curHtSlotId,
requestId: curReturnParcel.requestId,
xSlotNames: [curReturnParcel.xSlotName]
});
if (outstandingXSlotNames[curHtSlotId] && outstandingXSlotNames[curHtSlotId][curReturnParcel.requestId]) {
Utilities.arrayDelete(outstandingXSlotNames[curHtSlotId][curReturnParcel.requestId], curReturnParcel.xSlotName);
}
}
continue;
}
var bidPrice = curBid.pub_rev; //jshint ignore:line
beaconData.bp = bidPrice;
__fireBeacon(sessionId, beaconData);
if (timedOut) {
continue;
}
var bidWidth = Number(curBid.creative[0].width);
var bidHeight = Number(curBid.creative[0].height);
var bidCreative = curBid.html;
var bidDealId = curBid.deal_id ? String(curBid.deal_id) : ''; //jshint ignore:line
if (bidPrice <= 0 && bidDealId === '') {
//? if (DEBUG) {
Scribe.info(__profile.partnerId + ' returned pass for { id: ' + adResponse.id + ' }.');
//? }
curReturnParcel.pass = true;
continue;
}
if (__profile.enabledAnalytics.requestTime) {
EventsService.emit('hs_slot_bid', {
sessionId: sessionId,
statsId: __profile.statsId,
htSlotId: curHtSlotId,
requestId: curReturnParcel.requestId,
xSlotNames: [curReturnParcel.xSlotName]
});
if (outstandingXSlotNames[curHtSlotId] && outstandingXSlotNames[curHtSlotId][curReturnParcel.requestId]) {
Utilities.arrayDelete(outstandingXSlotNames[curHtSlotId][curReturnParcel.requestId], curReturnParcel.xSlotName);
}
}
curReturnParcel.size = [bidWidth, bidHeight];
curReturnParcel.targetingType = 'slot';
curReturnParcel.targeting = {};
var targetingCpm = '';
//? if (FEATURES.GPT_LINE_ITEMS) {
targetingCpm = __baseClass._bidTransformers.targeting.apply(bidPrice);
var sizeKey = Size.arrayToString(curReturnParcel.size);
if (bidDealId !== '') {
curReturnParcel.targeting[__baseClass._configs.targetingKeys.pm] = [sizeKey + '_' + bidDealId];
}
curReturnParcel.targeting[__baseClass._configs.targetingKeys.om] = [sizeKey + '_' + targetingCpm];
curReturnParcel.targeting[__baseClass._configs.targetingKeys.id] = [curReturnParcel.requestId];
//? }
//? if (FEATURES.RETURN_CREATIVE) {
curReturnParcel.adm = bidCreative;
//? }
//? if (FEATURES.RETURN_PRICE) {
curReturnParcel.price = Number(__baseClass._bidTransformers.price.apply(bidPrice));
//? }
var pubKitAdId = RenderService.registerAd({
sessionId: sessionId,
partnerId: __profile.partnerId,
adm: bidCreative,
requestId: curReturnParcel.requestId,
size: curReturnParcel.size,
price: targetingCpm,
dealId: bidDealId,
timeOfExpiry: __profile.features.demandExpiry.enabled ? (__profile.features.demandExpiry.value + System.now()) : 0
});
//? if (FEATURES.INTERNAL_RENDER) {
curReturnParcel.targeting.pubKitAdId = pubKitAdId;
//? }
}
/* any requests that didn't get a response above are passes */
if (__profile.enabledAnalytics.requestTime && !timedOut) {
__baseClass._emitStatsEvent(sessionId, 'hs_slot_pass', outstandingXSlotNames);
}
}
/* =====================================
* Constructors
* ---------------------------------- */
(function __constructor() {
EventsService = SpaceCamp.services.EventsService;
RenderService = SpaceCamp.services.RenderService;
__profile = {
partnerId: 'OpenXHtb',
namespace: 'OpenXHtb',
statsId: 'OPNX',
version: '2.1.1',
targetingType: 'slot',
enabledAnalytics: {
requestTime: true
},
features: {
demandExpiry: {
enabled: false,
value: 0
},
rateLimiting: {
enabled: false,
value: 0
}
},
targetingKeys: {
id: 'ix_ox_id',
om: 'ix_ox_om',
pm: 'ix_ox_pm'
},
bidUnitInCents: 0.1,
lineItemType: Constants.LineItemTypes.ID_AND_SIZE,
callbackType: Partner.CallbackTypes.CALLBACK_NAME,
architecture: Partner.Architectures.SRA,
requestType: Partner.RequestTypes.ANY,
parseAfterTimeout: true
};
//? if (DEBUG) {
var results = ConfigValidators.partnerBaseConfig(configs) || PartnerSpecificValidator(configs);
if (results) {
throw Whoopsie('INVALID_CONFIG', results);
}
//? }
configs.medium = configs.medium || 'w';
configs.version = configs.version || '1.0';
configs.endPointName = configs.endPointName || 'arj';
configs.charset = configs.charset || 'UTF-8';
configs.bidderCode = configs.bidderCode || 'hb_ix';
__baseAdRequestUrl = Network.buildUrl(Browser.getProtocol() + '//' + configs.host, [configs.medium, configs.version, configs.endPointName]);
__baseBeaconUrl = Network.buildUrl(Browser.getProtocol() + '//' + configs.host, [configs.medium, configs.version, 'bo']);
__baseClass = Partner(__profile, configs, null, {
parseResponse: __parseResponse,
generateRequestObj: __generateRequestObj
});
/* If wrapper is already active, we might be instantiated late so need to add our callback
since the shell potentially missed its chance */
if (window[SpaceCamp.NAMESPACE]) {
window[SpaceCamp.NAMESPACE][__profile.namespace] = window[SpaceCamp.NAMESPACE][__profile.namespace] || {};
window[SpaceCamp.NAMESPACE][__profile.namespace].adResponseCallbacks = __baseClass.getDirectInterface()[__profile.namespace].adResponseCallbacks;
}
})();
/* =====================================
* Public Interface
* ---------------------------------- */
var derivedClass = {
/* Class Information
* ---------------------------------- */
//? if (DEBUG) {
__type__: 'OpenXHtb',
//? }
//? if (TEST) {
__baseClass: __baseClass,
//? }
/* Data
* ---------------------------------- */
//? if (TEST) {
__profile: __profile,
//? }
/* Functions
* ---------------------------------- */
//? if (TEST) {
__parseResponse: __parseResponse
//? }
};
return Classify.derive(__baseClass, derivedClass);
}
////////////////////////////////////////////////////////////////////////////////
// Exports /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
module.exports = OpenXHtb;