forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yieldliftBidAdapter.js
151 lines (135 loc) · 4.68 KB
/
yieldliftBidAdapter.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
import {deepAccess, deepSetValue, logInfo} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
const ENDPOINT_URL = 'https://x.yieldlift.com/pbjs';
const DEFAULT_BID_TTL = 300;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_NET_REVENUE = true;
export const spec = {
code: 'yieldlift',
aliases: ['yl'],
supportedMediaTypes: [BANNER],
isBidRequestValid: function (bid) {
return (!!bid.params.unitId && typeof bid.params.unitId === 'string') ||
(!!bid.params.networkId && typeof bid.params.networkId === 'string') ||
(!!bid.params.publisherId && typeof bid.params.publisherId === 'string');
},
buildRequests: function (validBidRequests, bidderRequest) {
if (!validBidRequests || !bidderRequest) {
return;
}
const publisherId = validBidRequests[0].params.publisherId;
const networkId = validBidRequests[0].params.networkId;
const impressions = validBidRequests.map(bidRequest => ({
id: bidRequest.bidId,
banner: {
format: bidRequest.mediaTypes.banner.sizes.map(sizeArr => ({
w: sizeArr[0],
h: sizeArr[1]
}))
},
ext: {
exchange: {
unitId: bidRequest.params.unitId
}
}
}));
const openrtbRequest = {
id: bidderRequest.bidderRequestId,
imp: impressions,
site: {
domain: bidderRequest.refererInfo?.domain,
page: bidderRequest.refererInfo?.page,
ref: bidderRequest.refererInfo?.ref,
},
ext: {
exchange: {
publisherId: publisherId,
networkId: networkId,
}
}
};
// adding schain object
if (validBidRequests[0].schain) {
deepSetValue(openrtbRequest, 'source.ext.schain', validBidRequests[0].schain);
}
// Attaching GDPR Consent Params
if (bidderRequest.gdprConsent) {
deepSetValue(openrtbRequest, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(openrtbRequest, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
// CCPA
if (bidderRequest.uspConsent) {
deepSetValue(openrtbRequest, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}
// EIDS
const eids = deepAccess(validBidRequests[0], 'userIdAsEids');
if (Array.isArray(eids) && eids.length > 0) {
deepSetValue(openrtbRequest, 'user.ext.eids', eids);
}
const payloadString = JSON.stringify(openrtbRequest);
return {
method: 'POST',
url: ENDPOINT_URL,
data: payloadString,
};
},
interpretResponse: function (serverResponse) {
const bidResponses = [];
const response = (serverResponse || {}).body;
// response is always one seat (exchange) with (optional) bids for each impression
if (response && response.seatbid && response.seatbid.length === 1 && response.seatbid[0].bid && response.seatbid[0].bid.length) {
response.seatbid[0].bid.forEach(bid => {
bidResponses.push({
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 && bid.advertiserDomains ? bid.advertiserDomains : [] }
})
})
} else {
logInfo('yieldlift.interpretResponse :: no valid responses to interpret');
}
return bidResponses;
},
getUserSyncs: function (syncOptions, serverResponses) {
logInfo('yieldlift.getUserSyncs', 'syncOptions', syncOptions, 'serverResponses', serverResponses);
let syncs = [];
if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
return syncs;
}
serverResponses.forEach(resp => {
const userSync = deepAccess(resp, 'body.ext.usersync');
if (userSync) {
let syncDetails = [];
Object.keys(userSync).forEach(key => {
const value = userSync[key];
if (value.syncs && value.syncs.length) {
syncDetails = syncDetails.concat(value.syncs);
}
});
syncDetails.forEach(syncDetails => {
syncs.push({
type: syncDetails.type === 'iframe' ? 'iframe' : 'image',
url: syncDetails.url
});
});
if (!syncOptions.iframeEnabled) {
syncs = syncs.filter(s => s.type !== 'iframe')
}
if (!syncOptions.pixelEnabled) {
syncs = syncs.filter(s => s.type !== 'image')
}
}
});
logInfo('yieldlift.getUserSyncs result=%o', syncs);
return syncs;
},
};
registerBidder(spec);