forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpmstarBidAdapter.js
executable file
·210 lines (179 loc) · 7.17 KB
/
cpmstarBidAdapter.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
import * as utils from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { VIDEO, BANNER } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
const BIDDER_CODE = 'cpmstar';
const ENDPOINT_DEV = 'https://dev.server.cpmstar.com/view.aspx';
const ENDPOINT_STAGING = 'https://staging.server.cpmstar.com/view.aspx';
const ENDPOINT_PRODUCTION = 'https://server.cpmstar.com/view.aspx';
const DEFAULT_TTL = 300;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_NET_REVENUE = true;
export const converter = ortbConverter({
context: {
ttl: DEFAULT_TTL,
netRevenue: DEFAULT_NET_REVENUE
}
});
export const spec = {
code: BIDDER_CODE,
gvlid: 1317,
supportedMediaTypes: [BANNER, VIDEO],
pageID: Math.floor(Math.random() * 10e6),
getMediaType: function (bidRequest) {
if (bidRequest == null) return BANNER;
return !utils.deepAccess(bidRequest, 'mediaTypes.video') ? BANNER : VIDEO;
},
getPlayerSize: function (bidRequest) {
var playerSize = utils.deepAccess(bidRequest, 'mediaTypes.video.playerSize');
if (playerSize == null) return [640, 440];
if (playerSize[0] != null) playerSize = playerSize[0];
if (playerSize == null || playerSize[0] == null || playerSize[1] == null) return [640, 440];
return playerSize;
},
isBidRequestValid: function (bid) {
return ((typeof bid.params.placementId === 'string') && !!bid.params.placementId.length) || (typeof bid.params.placementId === 'number');
},
buildRequests: function (validBidRequests, bidderRequest) {
var requests = [];
for (var i = 0; i < validBidRequests.length; i++) {
var bidRequest = validBidRequests[i];
const referer = bidderRequest.refererInfo.page ? bidderRequest.refererInfo.page : bidderRequest.refererInfo.domain;
const e = utils.getBidIdParameter('endpoint', bidRequest.params);
const ENDPOINT = e == 'dev' ? ENDPOINT_DEV : e == 'staging' ? ENDPOINT_STAGING : ENDPOINT_PRODUCTION;
const url = new URL(ENDPOINT);
const body = {};
const mediaType = spec.getMediaType(bidRequest);
const playerSize = spec.getPlayerSize(bidRequest);
url.searchParams.set('media', mediaType);
if (mediaType == VIDEO) {
url.searchParams.set('fv', 0);
if (playerSize) {
url.searchParams.set('w', playerSize?.[0]);
url.searchParams.set('h', playerSize?.[1]);
}
}
url.searchParams.set('json', 'c_b');
url.searchParams.set('mv', 1);
url.searchParams.set('poolid', utils.getBidIdParameter('placementId', bidRequest.params));
url.searchParams.set('reachedTop', bidderRequest.refererInfo.reachedTop);
url.searchParams.set('requestid', bidRequest.bidId);
url.searchParams.set('referer', referer);
if (bidRequest.schain && bidRequest.schain.nodes) {
var schain = bidRequest.schain;
var schainString = '';
schainString += schain.ver + ',' + schain.complete;
for (var i2 = 0; i2 < schain.nodes.length; i2++) {
var node = schain.nodes[i2];
schainString += '!' +
(node.asi || '') + ',' +
(node.sid || '') + ',' +
(node.hp || '') + ',' +
(node.rid || '') + ',' +
(node.name || '') + ',' +
(node.domain || '');
}
url.searchParams.set('schain', schainString);
}
if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.consentString != null) {
url.searchParams.set('gdpr_consent', bidderRequest.gdprConsent.consentString);
}
if (bidderRequest.gdprConsent.gdprApplies != null) {
url.searchParams.set('gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
}
if (bidderRequest.uspConsent != null) {
url.searchParams.set('us_privacy', bidderRequest.uspConsent);
}
if (config.getConfig('coppa')) {
url.searchParams.set('tfcd', (config.getConfig('coppa') ? 1 : 0));
}
let adUnitCode = bidRequest.adUnitCode;
if (adUnitCode) {
body.adUnitCode = adUnitCode;
}
if (mediaType == VIDEO) {
body.video = utils.deepAccess(bidRequest, 'mediaTypes.video');
} else if (mediaType == BANNER) {
body.banner = utils.deepAccess(bidRequest, 'mediaTypes.banner');
}
const ortb = converter.toORTB({ bidderRequest, bidRequests: [bidRequest] });
Object.assign(body, ortb);
requests.push({
method: 'POST',
url: url.toString(),
bidRequest: bidRequest,
data: body
});
}
return requests;
},
interpretResponse: function (serverResponse, request) {
var bidRequest = request.bidRequest;
var mediaType = spec.getMediaType(bidRequest);
var bidResponses = [];
if (!Array.isArray(serverResponse.body)) {
serverResponse.body = [serverResponse.body];
}
for (var i = 0; i < serverResponse.body.length; i++) {
var raw = serverResponse.body[i];
var rawBid = raw.creatives[0];
if (!rawBid) {
utils.logWarn('cpmstarBidAdapter: server response failed check');
return;
}
var cpm = (parseFloat(rawBid.cpm) || 0);
if (!cpm) {
utils.logWarn('cpmstarBidAdapter: server response failed check. Missing cpm');
return;
}
var bidResponse = {
requestId: rawBid.requestid,
cpm: cpm,
width: rawBid.width || 0,
height: rawBid.height || 0,
currency: rawBid.currency ? rawBid.currency : DEFAULT_CURRENCY,
netRevenue: rawBid.netRevenue ? rawBid.netRevenue : DEFAULT_NET_REVENUE,
ttl: rawBid.ttl ? rawBid.ttl : DEFAULT_TTL,
creativeId: rawBid.creativeid || 0,
meta: {
advertiserDomains: rawBid.adomain ? rawBid.adomain : []
}
};
if (rawBid.hasOwnProperty('dealId')) {
bidResponse.dealId = rawBid.dealId;
}
if (mediaType == BANNER && rawBid.code) {
bidResponse.ad = rawBid.code + (rawBid.px_cr ? "\n<img width=0 height=0 src='" + rawBid.px_cr + "' />" : '');
} else if (mediaType == VIDEO && rawBid.creativemacros && rawBid.creativemacros.HTML5VID_VASTSTRING) {
var playerSize = spec.getPlayerSize(bidRequest);
if (playerSize != null) {
bidResponse.width = playerSize[0];
bidResponse.height = playerSize[1];
}
bidResponse.mediaType = VIDEO;
bidResponse.vastXml = rawBid.creativemacros.HTML5VID_VASTSTRING;
} else {
return utils.logError('bad response', rawBid);
}
bidResponses.push(bidResponse);
}
return bidResponses;
},
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = [];
if (serverResponses.length == 0 || !serverResponses[0].body) return syncs;
var usersyncs = serverResponses[0].body[0].syncs;
if (!usersyncs || usersyncs.length < 0) return syncs;
for (var i = 0; i < usersyncs.length; i++) {
var us = usersyncs[i];
if ((us.type === 'image' && syncOptions.pixelEnabled) || (us.type == 'iframe' && syncOptions.iframeEnabled)) {
syncs.push(us);
}
}
return syncs;
}
};
registerBidder(spec);