-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
integr8BidAdapter.js
165 lines (148 loc) · 5.38 KB
/
integr8BidAdapter.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
import { deepAccess, isFn, isPlainObject } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
const BIDDER_CODE = 'integr8';
const DEFAULT_ENDPOINT_URL = 'https://central.sea.integr8.digital/bid';
const DIMENSION_SEPARATOR = 'x';
const SIZE_SEPARATOR = ';';
const BISKO_ID = 'integr8Id';
const STORAGE_ID = 'bisko-sid';
const SEGMENTS = 'integr8Segments';
const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
return !!(bid.params && bid.params.propertyId && bid.params.placementId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
let deliveryUrl = '';
const storageId = storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(STORAGE_ID) || '' : '';
const biskoId = storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(BISKO_ID) || '' : '';
const segments = storage.localStorageIsEnabled() ? JSON.parse(storage.getDataFromLocalStorage(SEGMENTS)) || [] : [];
let propertyId = '';
let pageViewGuid = '';
let bidderRequestId = '';
let url = '';
let contents = [];
let data = {};
if (bidderRequest) {
bidderRequestId = bidderRequest.bidderRequestId;
if (bidderRequest.refererInfo) {
url = bidderRequest.refererInfo.page;
}
}
let placements = validBidRequests.map(bidRequest => {
if (!propertyId) { propertyId = bidRequest.params.propertyId; }
if (!pageViewGuid) { pageViewGuid = bidRequest.params.pageViewGuid || ''; }
if (!contents.length && bidRequest.params.contents && bidRequest.params.contents.length) { contents = bidRequest.params.contents; }
if (!Object.keys(data).length && bidRequest.params.data && Object.keys(bidRequest.params.data).length) { data = bidRequest.params.data; }
if (!deliveryUrl && bidRequest.params && typeof bidRequest.params.deliveryUrl === 'string') {
deliveryUrl = bidRequest.params.deliveryUrl;
}
return {
sizes: generateSizeParam(bidRequest.sizes),
adUnitId: bidRequest.adUnitCode,
placementId: bidRequest.params.placementId,
bidid: bidRequest.bidId,
count: bidRequest.params.count,
skipTime: deepAccess(bidRequest, 'mediaTypes.video.skipafter', bidRequest.params.skipTime),
floor: getBidFloor(bidRequest)
};
});
if (!deliveryUrl) {
deliveryUrl = DEFAULT_ENDPOINT_URL;
}
let body = {
propertyId: propertyId,
pageViewGuid: pageViewGuid,
storageId: storageId,
biskoId: biskoId,
segments: segments,
url: url,
requestid: bidderRequestId,
placements: placements,
contents: contents,
data: data
};
return [{
method: 'POST',
url: deliveryUrl,
data: body
}];
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse) {
const responses = serverResponse.body;
const bidResponses = [];
for (var i = 0; i < responses.length; i++) {
const bidResponse = {
requestId: responses[i].BidId,
cpm: responses[i].CPM,
width: responses[i].Width,
height: responses[i].Height,
creativeId: responses[i].CreativeId,
currency: responses[i].Currency,
netRevenue: responses[i].NetRevenue,
ttl: responses[i].TTL,
referrer: responses[i].Referrer,
ad: responses[i].Ad,
vastUrl: responses[i].VastUrl,
mediaType: responses[i].MediaType,
meta: {
advertiserDomains: Array.isArray(responses[i].ADomain) ? responses[i].ADomain : []
}
};
bidResponses.push(bidResponse);
}
return bidResponses;
}
};
/**
* Generate size param for bid request using sizes array
*
* @param {Array} sizes Possible sizes for the ad unit.
* @return {string} Processed sizes param to be used for the bid request.
*/
function generateSizeParam(sizes) {
return sizes.map(size => size.join(DIMENSION_SEPARATOR)).join(SIZE_SEPARATOR);
}
export function getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return null;
}
let floor = bid.getFloor({
currency: 'EUR',
mediaType: '*',
size: '*'
});
if (isPlainObject(floor) && !isNaN(floor.floor) && floor.currency === 'EUR') {
return floor.floor;
}
return null;
}
registerBidder(spec);