forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequativBidAdapter.js
More file actions
248 lines (205 loc) · 7.47 KB
/
Copy pathequativBidAdapter.js
File metadata and controls
248 lines (205 loc) · 7.47 KB
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
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
import { prepareSplitImps } from '../libraries/equativUtils/equativUtils.js';
import { tryAppendQueryString } from '../libraries/urlUtils/urlUtils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
import { deepAccess, deepSetValue, logError, logWarn, mergeDeep } from '../src/utils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest
*/
const BIDDER_CODE = 'equativ';
const COOKIE_SYNC_ORIGIN = 'https://apps.smartadserver.com';
const COOKIE_SYNC_URL = `${COOKIE_SYNC_ORIGIN}/diff/templates/asset/csync.html`;
const DEFAULT_TTL = 300;
const LOG_PREFIX = 'Equativ:';
const PID_STORAGE_NAME = 'eqt_pid';
let feedbackArray = [];
let impIdMap = {};
let nwid = 0;
let tokens = {};
/**
* Gets value of the local variable impIdMap
* @returns {*} Value of impIdMap
*/
export function getImpIdMap() {
return impIdMap;
}
/**
* Evaluates impressions for validity. The entry evaluated is considered valid if NEITHER of these conditions are met:
* 1) it has a `video` property defined for `mediaTypes.video` which is an empty object
* 2) it has a `native` property defined for `mediaTypes.native` which is an empty object
* @param {*} bidReq A bid request object to evaluate
* @returns boolean
*/
function isValid(bidReq) {
return !(bidReq.mediaTypes.video && JSON.stringify(bidReq.mediaTypes.video) === '{}') && !(bidReq.mediaTypes.native && JSON.stringify(bidReq.mediaTypes.native) === '{}');
}
/**
* Updates bid request with data from previous auction
* @param {*} req A bid request object to be updated
* @returns {*} Updated bid request object
*/
function updateFeedbackData(req) {
if (req?.ext?.prebid?.previousauctioninfo) {
req.ext.prebid.previousauctioninfo.forEach(info => {
if (tokens[info?.bidId]) {
feedbackArray.push({
feedback_token: tokens[info.bidId],
loss: info.bidderCpm == info.highestBidCpm ? 0 : 102,
price: info.highestBidCpm
});
delete tokens[info.bidId];
}
});
delete req.ext.prebid;
}
if (feedbackArray.length) {
deepSetValue(req, 'ext.bid_feedback', feedbackArray[0]);
feedbackArray.shift();
}
return req;
}
export const storage = getStorageManager({ bidderCode: BIDDER_CODE });
export const spec = {
code: BIDDER_CODE,
gvlid: 45,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
/**
* @param bidRequests
* @param bidderRequest
* @returns {ServerRequest[]}
*/
buildRequests: (bidRequests, bidderRequest) => {
if (bidRequests.filter(isValid).length === 0) {
logError(`${LOG_PREFIX} No useful bid requests to process. No requests will be sent.`, bidRequests);
return undefined;
}
const requests = [];
bidRequests.forEach(bid => {
const data = converter.toORTB({bidRequests: [bid], bidderRequest});
requests.push({
data,
method: 'POST',
url: 'https://ssb-global.smartadserver.com/api/bid?callerId=169',
})
});
return requests;
},
/**
* @param serverResponse
* @param bidRequest
* @returns {Bid[]}
*/
interpretResponse: (serverResponse, bidRequest) => {
if (bidRequest.data?.imp?.length) {
bidRequest.data.imp.forEach(imp => imp.id = impIdMap[imp.id]);
}
if (serverResponse.body?.seatbid?.length) {
serverResponse.body.seatbid
.filter(seat => seat?.bid?.length)
.forEach(seat =>
seat.bid.forEach(bid => {
bid.impid = impIdMap[bid.impid];
if (deepAccess(bid, 'ext.feedback_token')) {
tokens[bid.impid] = bid.ext.feedback_token;
}
bid.ttl = typeof bid.exp === 'number' && bid.exp > 0 ? bid.exp : DEFAULT_TTL;
})
);
}
return converter.fromORTB({
request: bidRequest.data,
response: serverResponse.body,
});
},
/**
* @param bidRequest
* @returns {boolean}
*/
isBidRequestValid: (bidRequest) => {
return !!(
deepAccess(bidRequest, 'params.networkId') ||
deepAccess(bidRequest, 'ortb2.site.publisher.id') ||
deepAccess(bidRequest, 'ortb2.app.publisher.id') ||
deepAccess(bidRequest, 'ortb2.dooh.publisher.id')
);
},
/**
* @param syncOptions
* @returns {{type: string, url: string}[]}
*/
getUserSyncs: (syncOptions, serverResponses, gdprConsent) => {
if (syncOptions.iframeEnabled) {
window.addEventListener('message', function handler(event) {
if (event.origin === COOKIE_SYNC_ORIGIN && event.data.action === 'getConsent') {
event.source.postMessage({
action: 'consentResponse',
id: event.data.id,
consents: gdprConsent.vendorData.vendor.consents
}, event.origin);
if (event.data.pid) {
storage.setDataInLocalStorage(PID_STORAGE_NAME, event.data.pid);
}
this.removeEventListener('message', handler);
}
});
let url = tryAppendQueryString(COOKIE_SYNC_URL + '?', 'nwid', nwid);
url = tryAppendQueryString(url, 'gdpr', (gdprConsent?.gdprApplies ? '1' : '0'));
return [{ type: 'iframe', url }];
}
return [];
}
};
export const converter = ortbConverter({
context: {
netRevenue: true,
ttl: DEFAULT_TTL
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
const { siteId, pageId, formatId } = bidRequest.params;
delete imp.dt;
imp.secure = 1;
imp.tagid = bidRequest.adUnitCode;
if (!deepAccess(bidRequest, 'ortb2Imp.rwdd') && deepAccess(bidRequest, 'mediaTypes.video.ext.rewarded')) {
mergeDeep(imp, { rwdd: bidRequest.mediaTypes.video.ext.rewarded });
}
const bidder = { ...(siteId && { siteId }), ...(pageId && { pageId }), ...(formatId && { formatId }) };
if (Object.keys(bidder).length) {
mergeDeep(imp.ext, { bidder });
}
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const bid = context.bidRequests[0];
const currency = config.getConfig('currency.adServerCurrency') || 'USD';
const splitImps = prepareSplitImps(imps, bid, currency, impIdMap, 'eqtv');
let req = buildRequest(splitImps, bidderRequest, context);
let env = ['ortb2.site.publisher', 'ortb2.app.publisher', 'ortb2.dooh.publisher'].find(propPath => deepAccess(bid, propPath)) || 'ortb2.site.publisher';
nwid = deepAccess(bid, env + '.id') || bid.params.networkId;
deepSetValue(req, env.replace('ortb2.', '') + '.id', nwid);
[
{ path: 'mediaTypes.video', props: ['mimes', 'placement'] },
{ path: 'ortb2Imp.audio', props: ['mimes'] },
{ path: 'mediaTypes.native.ortb', props: ['privacy', 'plcmttype', 'eventtrackers'] },
].forEach(({ path, props }) => {
if (deepAccess(bid, path)) {
props.forEach(prop => {
if (!deepAccess(bid, `${path}.${prop}`)) {
logWarn(`${LOG_PREFIX} Property "${path}.${prop}" is missing from request. Request will proceed, but the use of "${prop}" is strongly encouraged.`, bid);
}
});
}
});
const pid = storage.getDataFromLocalStorage(PID_STORAGE_NAME);
if (pid) {
deepSetValue(req, 'user.buyeruid', pid);
}
req = updateFeedbackData(req);
return req;
}
});
registerBidder(spec);