forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalltvBidAdapter.js
143 lines (134 loc) · 5.42 KB
/
malltvBidAdapter.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
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 = 'malltv';
const ENDPOINT_URL = 'https://central.mall.tv/bid';
const DIMENSION_SEPARATOR = 'x';
const SIZE_SEPARATOR = ';';
const BISKO_ID = 'biskoId';
const STORAGE_ID = 'bisko-sid';
const SEGMENTS = 'biskoSegments';
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.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) {
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 = {};
let auctionId = bidderRequest ? bidderRequest.auctionId : '';
let gdrpApplies = true;
let gdprConsent = '';
let placements = validBidRequests.map(bidRequest => {
if (!propertyId) { propertyId = bidRequest.params.propertyId; }
if (!pageViewGuid && bidRequest.params) { pageViewGuid = bidRequest.params.pageViewGuid || ''; }
if (!bidderRequestId) { bidderRequestId = bidRequest.bidderRequestId; }
// TODO: is 'page' the right value here?
if (!url && bidderRequest) { url = bidderRequest.refererInfo.page; }
if (!contents.length && bidRequest.params.contents && bidRequest.params.contents.length) { contents = bidRequest.params.contents; }
if (Object.keys(data).length === 0 && bidRequest.params.data && Object.keys(bidRequest.params.data).length !== 0) { data = bidRequest.params.data; }
if (bidderRequest && bidRequest.gdprConsent) { gdrpApplies = bidderRequest.gdprConsent && bidderRequest.gdprConsent.gdprApplies ? bidderRequest.gdprConsent.gdprApplies : true; }
if (bidderRequest && bidRequest.gdprConsent) { gdprConsent = bidderRequest.gdprConsent && bidderRequest.gdprConsent.consentString ? bidderRequest.gdprConsent.consentString : ''; }
let adUnitId = bidRequest.adUnitCode;
let placementId = bidRequest.params.placementId;
let sizes = generateSizeParam(bidRequest.sizes);
return {
sizes: sizes,
adUnitId: adUnitId,
placementId: placementId,
bidid: bidRequest.bidId,
count: bidRequest.params.count,
skipTime: bidRequest.params.skipTime
};
});
let body = {
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: auctionId,
propertyId: propertyId,
pageViewGuid: pageViewGuid,
storageId: storageId,
biskoId: biskoId,
segments: segments,
url: url,
requestid: bidderRequestId,
placements: placements,
contents: contents,
data: data,
gdpr_applies: gdrpApplies,
gdpr_consent: gdprConsent,
};
return [{
method: 'POST',
url: ENDPOINT_URL,
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);
}
registerBidder(spec);