forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopscoBidAdapter.js
133 lines (114 loc) · 4.04 KB
/
opscoBidAdapter.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
import {deepAccess, deepSetValue, isArray, logInfo} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
const ENDPOINT = 'https://exchange.ops.co/openrtb2/auction';
const BIDDER_CODE = 'opsco';
const DEFAULT_BID_TTL = 300;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_NET_REVENUE = true;
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: (bid) => !!(bid.params &&
bid.params.placementId &&
bid.params.publisherId &&
bid.mediaTypes?.banner?.sizes &&
Array.isArray(bid.mediaTypes?.banner?.sizes)),
buildRequests: (validBidRequests, bidderRequest) => {
if (!validBidRequests || !bidderRequest) {
return;
}
const {publisherId, siteId} = validBidRequests[0].params;
const payload = {
id: bidderRequest.bidderRequestId,
imp: validBidRequests.map(bidRequest => ({
id: bidRequest.bidId,
banner: {format: extractSizes(bidRequest)},
ext: {
opsco: {
placementId: bidRequest.params.placementId,
publisherId: publisherId,
}
}
})),
site: {
id: siteId,
publisher: {id: publisherId},
domain: bidderRequest.refererInfo?.domain,
page: bidderRequest.refererInfo?.page,
ref: bidderRequest.refererInfo?.ref,
},
};
if (isTest(validBidRequests[0])) {
payload.test = 1;
}
if (bidderRequest.gdprConsent) {
deepSetValue(payload, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(payload, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
const eids = deepAccess(validBidRequests[0], 'userIdAsEids');
if (eids && eids.length !== 0) {
deepSetValue(payload, 'user.ext.eids', eids);
}
const schainData = deepAccess(validBidRequests[0], 'schain.nodes');
if (isArray(schainData) && schainData.length > 0) {
deepSetValue(payload, 'source.ext.schain', validBidRequests[0].schain);
}
if (bidderRequest.uspConsent) {
deepSetValue(payload, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}
return {
method: 'POST',
url: ENDPOINT,
data: JSON.stringify(payload),
};
},
interpretResponse: (serverResponse) => {
const response = (serverResponse || {}).body;
const bidResponses = response?.seatbid?.[0]?.bid?.map(bid => ({
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
ad: bid.adm,
ttl: typeof bid.exp === 'number' ? bid.exp : DEFAULT_BID_TTL,
creativeId: bid.crid,
netRevenue: DEFAULT_NET_REVENUE,
currency: DEFAULT_CURRENCY,
meta: {advertiserDomains: bid?.adomain || []},
mediaType: bid.mediaType || bid.mtype
})) || [];
if (!bidResponses.length) {
logInfo('opsco.interpretResponse :: No valid responses');
}
return bidResponses;
},
getUserSyncs: (syncOptions, serverResponses) => {
logInfo('opsco.getUserSyncs', 'syncOptions', syncOptions, 'serverResponses', serverResponses);
if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
return [];
}
let syncs = [];
serverResponses.forEach(resp => {
const userSync = deepAccess(resp, 'body.ext.usersync');
if (userSync) {
const syncDetails = Object.values(userSync).flatMap(value => value.syncs || []);
syncDetails.forEach(syncDetail => {
const type = syncDetail.type === 'iframe' ? 'iframe' : 'image';
if ((type === 'iframe' && syncOptions.iframeEnabled) || (type === 'image' && syncOptions.pixelEnabled)) {
syncs.push({type, url: syncDetail.url});
}
});
}
});
logInfo('opsco.getUserSyncs result=%o', syncs);
return syncs;
}
};
function extractSizes(bidRequest) {
return (bidRequest.mediaTypes?.banner?.sizes || []).map(([width, height]) => ({w: width, h: height}));
}
function isTest(validBidRequest) {
return validBidRequest.params?.test === true;
}
registerBidder(spec);