forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfanAdapter.js
176 lines (151 loc) · 4.42 KB
/
fanAdapter.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
import * as utils from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { BANNER, NATIVE } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidderRequest} BidderRequest
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
*/
const BIDDER_CODE = 'freedomadnetwork';
const BASE_URL = 'https://srv.freedomadnetwork.com';
/**
* Build OpenRTB request from bidRequest and bidderRequest
*
* @param {BidRequest} bid
* @param {BidderRequest} bidderRequest
* @returns {Request}
*/
function buildBidRequest(bid, bidderRequest) {
const payload = {
id: bid.bidId,
tmax: bidderRequest.timeout,
placements: [bid.params.placementId],
at: 1,
user: {}
}
const gdprConsent = utils.deepAccess(bidderRequest, 'gdprConsent');
if (!!gdprConsent && gdprConsent.gdprApplies) {
payload.user.gdpr = 1;
payload.user.consent = gdprConsent.consentString;
}
const uspConsent = utils.deepAccess(bidderRequest, 'uspConsent');
if (uspConsent) {
payload.user.usp = uspConsent;
}
return {
method: 'POST',
url: BASE_URL + '/pb/req',
data: JSON.stringify(payload),
options: {
contentType: 'application/json',
withCredentials: false,
customHeaders: {
'Accept-Language': 'en;q=10',
},
},
originalBidRequest: bid
}
}
export const spec = {
code: BIDDER_CODE,
isBidRequestValid: function(bid) {
if (!bid) {
utils.logWarn(BIDDER_CODE, 'Invalid bid', bid);
return false;
}
if (!bid.params) {
utils.logWarn(BIDDER_CODE, 'bid.params is required');
return false;
}
if (!bid.params.placementId) {
utils.logWarn(BIDDER_CODE, 'bid.params.placementId is required');
return false;
}
var banner = utils.deepAccess(bid, 'mediaTypes.banner');
if (banner === undefined) {
return false;
}
return true;
},
buildRequests: function(validBidRequests, bidderRequest) {
return validBidRequests.map(bid => buildBidRequest(bid, bidderRequest));
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
const serverBody = serverResponse.body;
let bidResponses = [];
if (!serverBody) {
return bidResponses;
}
serverBody.forEach((response) => {
const bidResponse = {
requestId: response.id,
bidid: response.bidid,
impid: response.impid,
userId: response.userId,
cpm: response.cpm,
currency: response.currency,
width: response.width,
height: response.height,
ad: response.payload,
ttl: response.ttl,
creativeId: response.crid,
netRevenue: response.netRevenue,
trackers: response.trackers,
meta: {
mediaType: response.mediaType,
advertiserDomains: response.domains,
}
};
bidResponses.push(bidResponse);
});
return bidResponses;
},
/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
*
* @param {Bid} bid The bid that won the auction
*/
onBidWon: function (bid) {
if (!bid) {
return;
}
const payload = {
id: bid.bidid,
impid: bid.impid,
t: bid.cpm,
u: bid.userId,
}
ajax(BASE_URL + '/pb/imp', null, JSON.stringify(payload), {
method: 'POST',
customHeaders: {
'Accept-Language': 'en;q=10',
},
});
if (bid.trackers && bid.trackers.length > 0) {
for (var i = 0; i < bid.trackers.length; i++) {
if (bid.trackers[i].type == 0) {
utils.triggerPixel(bid.trackers[i].url);
}
}
}
},
onSetTargeting: function(bid) {},
onBidderError: function(error) {
utils.logError(`${BIDDER_CODE} bidder error`, error);
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = [];
return syncs;
},
onTimeout: function(timeoutData) {},
supportedMediaTypes: [BANNER, NATIVE]
}
registerBidder(spec);