-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtriple-lift-htb.js
415 lines (349 loc) · 14.8 KB
/
triple-lift-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
/**
* @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 Partner = require('partner.js');
var Size = require('size.js');
var SpaceCamp = require('space-camp.js');
var System = require('system.js');
var Network = require('network.js');
var ComplianceService;
var EventsService;
var RenderService;
//? if (DEBUG) {
var ConfigValidators = require('config-validators.js');
var PartnerSpecificValidator = require('triple-lift-htb-validator.js');
var Scribe = require('scribe.js');
var Whoopsie = require('whoopsie.js');
//? }
////////////////////////////////////////////////////////////////////////////////
// Main ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Triple Lift Header Tag Bidder module.
*
* @class
*/
function TripleLiftHtb(configs) {
/* TripleLift endpoint only works with AJAX */
if (!Network.isXhrSupported()) {
//? if (DEBUG) {
Scribe.warn('Partner TripleLift requires AJAX support. Aborting instantiation.');
//? }
return null;
}
/* =====================================
* Data
* ---------------------------------- */
/* Private
* ---------------------------------- */
/**
* Reference to the partner base class.
*
* @private {object}
*/
var __baseClass;
/**
* Profile for this partner.
*
* @private {object}
*/
var __profile;
/**
* Base url for bid requests.
*
* @private {object}
*/
var __baseUrl;
/* =====================================
* Functions
* ---------------------------------- */
/* Utilities
* ---------------------------------- */
/**
* Generates the request URL to the endpoint for the xSlots in the given
* returnParcels.
*
* @param {object[]} returnParcels
* @return {object} the request object
*/
function __generateRequestObj(returnParcels) {
/* =============================================================================
* STEP 2 | Generate Request URL
* -----------------------------------------------------------------------------
*
* Generate the URL to request demand from the partner endpoint using the provided
* returnParcels. The returnParcels is an array of objects each object containing
* an .xSlotRef which is a reference to the xSlot object from the partner configuration.
* Use this to retrieve the placements/xSlots you need to request for.
*
* If your partner is MRA, returnParcels will be an array of length one. If your
* partner is SRA, it will contain any number of entities. In any event, the full
* contents of the array should be able to fit into a single request and the
* return value of this function should similarly represent a single request to the
* endpoint.
*
* Return an object containing:
* queryUrl: the url for the request
* data: the query object containing a map of the query string paramaters
*
* callbackId:
*
* arbitrary id to match the request with the response in the callback function. If
* your endpoint supports passing in an arbitrary ID and returning as part of the response
* please use the callbackType: Partner.CallbackTypes.ID and fill out the adResponseCallback.
* Also please provide this adResponseCallback to your bid request here so that the JSONP
* response calls it once it has completed.
*
* If your endpoint does not support passing in an ID, simply use
* Partner.CallbackTypes.CALLBACK_NAME and the wrapper will take care of handling request
* matching by generating unique callbacks for each request using the callbackId.
*
* If your endpoint is ajax only, please set the appropriate values in your profile for this,
* i.e. Partner.CallbackTypes.NONE and Partner.Requesttypes.AJAX
*
* The return object should look something like this:
* {
* url: 'http://bidserver.com/api/bids' // base request url for a GET/POST request
* data: { // query string object that will be attached to the base url
* slots: [
* {
* placementId: 54321,
* sizes: [[300, 250]]
* },{
* placementId: 12345,
* sizes: [[300, 600]]
* },{
* placementId: 654321,
* sizes: [[728, 90]]
* }
* ],
* site: 'http://google.com'
* },
* callbackId: '_23sd2ij4i1' //unique id used for pairing requests and responses
* }
*/
var gdprStatus = ComplianceService.gdpr.getConsent();
var privacyEnabled = ComplianceService.isPrivacyEnabled();
/* MRA partners receive only one parcel in the array. */
var returnParcel = returnParcels[0];
var xSlot = returnParcel.xSlotRef;
/* request params */
var requestParams = {
inv_code: xSlot.inventoryCode, // jshint ignore:line
lib: 'ix',
fe: Browser.isFlashSupported() ? 1 : 0,
size: Size.arrayToString(xSlot.sizes),
referrer: Browser.getPageUrl(),
v: '2.1'
};
if (privacyEnabled) {
requestParams.gdpr = gdprStatus.applies;
requestParams.cmp_cs = gdprStatus.consentString;
}
if (xSlot.floor) {
requestParams.floor = xSlot.floor;
}
return {
url: __baseUrl,
data: requestParams
};
}
/* Helpers
* ---------------------------------- */
/* Parse adResponse, put demand into outParcels.
* Triple-Lift response contains a single result object.
*/
function __parseResponse(sessionId, responseObj, returnParcels) {
/* =============================================================================
* STEP 4 | Parse & store demand response
* -----------------------------------------------------------------------------
*
* Fill the below variables with information about the bid from the partner, using
* the adResponse variable that contains your module adResponse.
*/
/* This an array of all the bids in your response that will be iterated over below. Each of
* these will be mapped back to a returnParcel object using some criteria explained below.
* The following variables will also be parsed and attached to that returnParcel object as
* returned demand.
*
* Use the adResponse variable to extract your bid information and insert it into the
* bids array. Each element in the bids array should represent a single bid and should
* match up to a single element from the returnParcel array.
*
*/
/* ---------- Process adResponse and extract the bids into the bids array ------------*/
/* there is only one bid because mra */
var bid = responseObj;
/* --------------------------------------------------------------------------------- */
/* MRA partners receive only one parcel in the array. */
var returnParcel = returnParcels[0];
/* header stats information */
var headerStatsInfo = {
sessionId: sessionId,
statsId: __profile.statsId,
htSlotId: returnParcel.htSlot.getId(),
requestId: returnParcel.requestId,
xSlotNames: [returnParcel.xSlotName]
};
/* If not a pass */
if (!bid.status && bid.cpm && bid.cpm > 0) {
if (__profile.enabledAnalytics.requestTime) {
EventsService.emit('hs_slot_bid', headerStatsInfo);
}
/* ---------- Fill the bid variables with data from the bid response here. ------------*/
/* Using the above variable, curBid, extract various information about the bid and assign it to
* these local variables */
var bidPrice = bid.cpm; /* the bid price for the given slot */
var bidCreative = bid.ad; /* the creative/adm for the given slot that will be rendered if is the winner. */
var bidSize = [Number(bid.width), Number(bid.height)]; /* the size of the given slot */
/* the dealId if applicable for this slot. */
var bidDealId = bid.deal_id; // jshint ignore:line
/* ---------------------------------------------------------------------------------------*/
returnParcel.targetingType = 'slot';
returnParcel.targeting = {};
returnParcel.size = bidSize;
var targetingCpm = '';
//? if(FEATURES.GPT_LINE_ITEMS) {
targetingCpm = __baseClass._bidTransformers.targeting.apply(bidPrice);
var sizeKey = Size.arrayToString(bidSize);
if (bidDealId) {
returnParcel.targeting[__baseClass._configs.targetingKeys.pmid] = [bidDealId];
returnParcel.targeting[__baseClass._configs.targetingKeys.pm] = [sizeKey + '_' + targetingCpm];
} else {
returnParcel.targeting[__baseClass._configs.targetingKeys.om] = [sizeKey + '_' + targetingCpm];
}
returnParcel.targeting[__baseClass._configs.targetingKeys.id] = [returnParcel.requestId];
//? }
//? if(FEATURES.RETURN_CREATIVE) {
returnParcel.adm = bidCreative;
//? }
//? if(FEATURES.RETURN_PRICE) {
returnParcel.price = Number(__baseClass._bidTransformers.price.apply(bidPrice));
//? }
var pubKitAdId = RenderService.registerAd({
sessionId: sessionId,
partnerId: __profile.partnerId,
adm: bidCreative,
requestId: returnParcel.requestId,
size: returnParcel.size,
price: targetingCpm ? targetingCpm : undefined,
dealId: bidDealId ? bidDealId : undefined,
timeOfExpiry: __profile.features.demandExpiry.enabled ? (__profile.features.demandExpiry.value + System.now()) : 0
});
//? if(FEATURES.INTERNAL_RENDER) {
returnParcel.targeting.pubKitAdId = pubKitAdId;
//? }
} else {
//? if (DEBUG) {
Scribe.info(__profile.partnerId + ' no bid response for { id: ' + returnParcel.xSlotRef.inventoryCode + ' }.');
//? }
if (__profile.enabledAnalytics.requestTime) {
EventsService.emit('hs_slot_pass', headerStatsInfo);
}
returnParcel.pass = true;
}
}
/* =====================================
* Constructors
* ---------------------------------- */
(function __constructor() {
ComplianceService = SpaceCamp.services.ComplianceService;
EventsService = SpaceCamp.services.EventsService;
RenderService = SpaceCamp.services.RenderService;
/* =============================================================================
* STEP 1 | Partner Configuration
* -----------------------------------------------------------------------------
*
* Please review below partner profile according to the steps in the README doc.
*/
__profile = {
partnerId: 'TripleLiftHtb',
namespace: 'TripleLiftHtb',
statsId: 'TPL',
version: '2.1.1',
targetingType: 'slot',
enabledAnalytics: {
requestTime: true
},
features: {
demandExpiry: {
enabled: false,
value: 0
},
rateLimiting: {
enabled: false,
value: 0
}
},
targetingKeys: {
om: 'ix_tpl_cpm',
pm: 'ix_tpl_cpm',
pmid: 'ix_tpl_dealid',
id: 'ix_tpl_id'
},
bidUnitInCents: 100,
lineItemType: Constants.LineItemTypes.ID_AND_SIZE,
callbackType: Partner.CallbackTypes.NONE,
architecture: Partner.Architectures.MRA,
requestType: Partner.RequestTypes.AJAX
};
/* ---------------------------------------------------------------------------------------*/
//? if (DEBUG) {
var results = ConfigValidators.partnerBaseConfig(configs) || PartnerSpecificValidator(configs);
if (results) {
throw Whoopsie('INVALID_CONFIG', results);
}
//? }
/* build base bid request url */
__baseUrl = Browser.getProtocol() + '//tlx.3lift.com/header/auction';
__baseClass = Partner(__profile, configs, null, {
parseResponse: __parseResponse,
generateRequestObj: __generateRequestObj
});
})();
/* =====================================
* Public Interface
* ---------------------------------- */
var derivedClass = {
/* Class Information
* ---------------------------------- */
//? if (DEBUG) {
__type__: 'TripleLiftHtb',
//? }
//? if (TEST) {
__baseClass: __baseClass,
//? }
/* Data
* ---------------------------------- */
//? if (TEST) {
__profile: __profile,
__baseUrl: __baseUrl,
//? }
/* Functions
* ---------------------------------- */
//? if (TEST) {
__generateRequestObj: __generateRequestObj,
__parseResponse: __parseResponse
//? }
};
return Classify.derive(__baseClass, derivedClass);
}
////////////////////////////////////////////////////////////////////////////////
// Exports /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
module.exports = TripleLiftHtb;