forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtealBidAdapter.js
145 lines (137 loc) · 4.65 KB
/
tealBidAdapter.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
import {deepSetValue, deepAccess, triggerPixel, deepClone, isEmpty, logError, shuffle} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js'
import {BANNER} from '../src/mediaTypes.js';
import {pbsExtensions} from '../libraries/pbsExtensions/pbsExtensions.js'
const BIDDER_CODE = 'teal';
const GVLID = 1378;
const DEFAULT_ENDPOINT = 'https://a.bids.ws/openrtb2/auction';
const COOKIE_SYNC_ENDPOINT = 'https://a.bids.ws/cookie_sync';
const COOKIE_SYNC_IFRAME = 'https://bids.ws/load-cookie.html';
const MAX_SYNC_COUNT = 10;
const converter = ortbConverter({
processors: pbsExtensions,
context: {
netRevenue: true,
ttl: 30
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
const { placement, testMode } = bidRequest.params;
if (placement) {
deepSetValue(imp, 'ext.prebid.storedrequest.id', placement);
}
if (testMode) {
deepSetValue(imp, 'ext.prebid.storedauctionresponse.id', placement);
}
delete imp.ext.prebid.bidder;
return imp;
},
overrides: {
bidResponse: {
bidderCode(orig, bidResponse, bid, { bidRequest }) {
let useSourceBidderCode = deepAccess(bidRequest, 'params.useSourceBidderCode', false);
if (useSourceBidderCode) {
orig.apply(this, [...arguments].slice(1));
}
},
},
}
});
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER],
aliases: [],
isBidRequestValid: function(bid) {
return Boolean(bid.params?.account);
},
buildRequests: function(bidRequests, bidderRequest) {
const { bidder } = bidRequests[0];
const data = converter.toORTB({bidRequests, bidderRequest});
const account = deepAccess(bidRequests[0], 'params.account', null);
const subAccount = deepAccess(bidRequests[0], 'params.subAccount', null);
deepSetValue(data, 'site.publisher.id', account);
deepSetValue(data, 'ext.prebid.storedrequest.id', subAccount || account);
data.ext.prebid.passthrough = {
...data.ext.prebid.passthrough,
teal: { bidder },
};
data.tmax = (bidderRequest.timeout || 1500) - 100;
return {
method: 'POST',
url: deepAccess(bidRequests[0], 'params.endpoint', DEFAULT_ENDPOINT),
data
};
},
interpretResponse: function(response, request) {
const resp = deepClone(response.body);
const { bidder } = request.data.ext.prebid.passthrough.teal;
const modifiers = {
responsetimemillis: (values) => Math.max(...values),
errors: (values) => [].concat(...values),
};
Object.entries(modifiers).forEach(([field, combineFn]) => {
const obj = resp.ext?.[field];
if (!isEmpty(obj)) {
resp.ext[field] = {[bidder]: combineFn(Object.values(obj))};
}
});
const bids = converter.fromORTB({response: resp, request: request.data}).bids;
return bids;
},
getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
if (!syncOptions.iframeEnabled) {
return [];
}
const syncs = [];
const { gdprApplies, consentString } = gdprConsent || {};
let bidders = [];
serverResponses.forEach(({ body }) => {
const newBidders = Object.keys(body.ext?.responsetimemillis || {});
newBidders.forEach(s => {
if (bidders.indexOf(s) === -1) {
bidders.push(s);
}
});
});
bidders = shuffle(bidders).slice(0, MAX_SYNC_COUNT);
if (!bidders.length) {
return;
}
const params = {
endpoint: COOKIE_SYNC_ENDPOINT,
max_sync_count: MAX_SYNC_COUNT,
gdpr: gdprApplies ? 1 : 0,
gdpr_consent: consentString,
us_privacy: uspConsent,
bidders: bidders.join(','),
coop_sync: 0
};
const qs = Object.entries(params)
.filter(([k, v]) => ![null, undefined, ''].includes(v))
.map(([k, v]) => `${k}=${encodeURIComponent(v.toString())}`)
.join('&');
syncs.push({ type: 'iframe', url: `${COOKIE_SYNC_IFRAME}?${qs}` });
return syncs;
},
onBidWon: function(bid) {
if (bid.pbsWurl) {
triggerPixel(bid.pbsWurl);
}
if (bid.burl) {
triggerPixel(bid.burl);
}
},
onBidderError: function({ error, bidderRequest }) {
if (error.responseText && error.status) {
let id = error.responseText.match(/found for id: (.*)/);
if (Array.isArray(id) && id.length > 1 && error.status == 400) {
logError(`Placement: ${id[1]} not found on ${BIDDER_CODE} server. Please contact your account manager or email prebid@teal.works`, error);
return;
}
}
logError(`${BIDDER_CODE} bidder error`, error);
}
}
registerBidder(spec);