forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractiveOffersBidAdapter.js
182 lines (166 loc) · 5.6 KB
/
interactiveOffersBidAdapter.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
177
178
179
180
181
182
import {deepClone, isNumber, logWarn} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
const BIDDER_CODE = 'interactiveOffers';
const ENDPOINT = 'https://prebid.ioadx.com/bidRequest/?partnerId=';
const DEFAULT = {
'OpenRTBBidRequest': {},
'OpenRTBBidRequestSite': {},
'OpenRTBBidRequestSitePublisher': {},
'OpenRTBBidRequestSiteContent': {
language: navigator.language,
},
'OpenRTBBidRequestSource': {},
'OpenRTBBidRequestDevice': {
ua: navigator.userAgent,
language: navigator.language
},
'OpenRTBBidRequestUser': {},
'OpenRTBBidRequestImp': {},
'OpenRTBBidRequestImpBanner': {},
'PrebidBid': {
currency: 'USD',
ttl: 60,
netRevenue: false
}
};
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: function(bid) {
let ret = true;
if (bid && bid.params) {
if (!bid.params.partnerId) {
logWarn('partnerId must be a valid ID');
ret = false;
}
if (bid.params.tmax && !isNumber(bid.params.tmax)) {
logWarn('tmax must be a valid numeric ID');
ret = false;
}
} else {
logWarn('invalid request');
ret = false;
}
return ret;
},
buildRequests: function(validBidRequests, bidderRequest) {
let aux = parseRequestPrebidjsToOpenRTB(bidderRequest, bidderRequest);
let payload = aux.payload;
return {
method: 'POST',
url: ENDPOINT + aux.partnerId,
data: JSON.stringify(payload),
bidderRequest: bidderRequest
};
},
interpretResponse: function(response, request) {
let bidResponses = [];
if (response.body) {
if (!response.body.length) {
response.body = [response.body];
}
bidResponses = parseResponseOpenRTBToPrebidjs(response.body);
}
return bidResponses;
}
};
function parseRequestPrebidjsToOpenRTB(prebidRequest, bidderRequest) {
let ret = {
payload: {},
partnerId: null
};
// TODO: these should probably look at refererInfo
let pageURL = window.location.href;
let domain = window.location.hostname;
let openRTBRequest = deepClone(DEFAULT['OpenRTBBidRequest']);
openRTBRequest.id = bidderRequest.bidderRequestId;
openRTBRequest.ext = {
// TODO: please do not send internal data structures over the network
refererInfo: prebidRequest.refererInfo.legacy,
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: prebidRequest.auctionId
};
openRTBRequest.site = deepClone(DEFAULT['OpenRTBBidRequestSite']);
openRTBRequest.site.id = domain;
openRTBRequest.site.name = domain;
openRTBRequest.site.domain = domain;
openRTBRequest.site.page = pageURL;
openRTBRequest.site.ref = prebidRequest.refererInfo.ref;
openRTBRequest.site.publisher = deepClone(DEFAULT['OpenRTBBidRequestSitePublisher']);
openRTBRequest.site.publisher.id = 0;
openRTBRequest.site.publisher.name = prebidRequest.refererInfo.domain;
openRTBRequest.site.publisher.domain = domain;
openRTBRequest.site.publisher.domain = domain;
openRTBRequest.site.content = deepClone(DEFAULT['OpenRTBBidRequestSiteContent']);
openRTBRequest.source = deepClone(DEFAULT['OpenRTBBidRequestSource']);
openRTBRequest.source.fd = 0;
openRTBRequest.source.tid = prebidRequest.ortb2?.source?.tid;
openRTBRequest.source.pchain = '';
openRTBRequest.device = deepClone(DEFAULT['OpenRTBBidRequestDevice']);
openRTBRequest.user = deepClone(DEFAULT['OpenRTBBidRequestUser']);
openRTBRequest.imp = [];
prebidRequest.bids.forEach(function(bid) {
if (!ret.partnerId) {
ret.partnerId = bid.params.partnerId;
}
let imp = deepClone(DEFAULT['OpenRTBBidRequestImp']);
imp.id = bid.bidId;
imp.secure = bid.ortb2Imp?.secure ?? 1;
imp.tagid = bid.adUnitCode;
imp.ext = {
rawdata: bid
};
openRTBRequest.site.publisher.id = openRTBRequest.site.publisher.id || 0;
openRTBRequest.tmax = openRTBRequest.tmax || bid.params.tmax || 0;
Object.keys(bid.mediaTypes).forEach(function(mediaType) {
if (mediaType == 'banner') {
imp.banner = deepClone(DEFAULT['OpenRTBBidRequestImpBanner']);
imp.banner.w = 0;
imp.banner.h = 0;
imp.banner.format = [];
bid.mediaTypes[mediaType].sizes.forEach(function(adSize) {
if (!imp.banner.w) {
imp.banner.w = adSize[0];
imp.banner.h = adSize[1];
}
imp.banner.format.push({w: adSize[0], h: adSize[1]});
});
}
});
openRTBRequest.imp.push(imp);
});
ret.payload = openRTBRequest;
return ret;
}
function parseResponseOpenRTBToPrebidjs(openRTBResponse) {
let prebidResponse = [];
openRTBResponse.forEach(function(response) {
if (response.seatbid && response.seatbid.forEach) {
response.seatbid.forEach(function(seatbid) {
if (seatbid.bid && seatbid.bid.forEach) {
seatbid.bid.forEach(function(bid) {
let prebid = deepClone(DEFAULT['PrebidBid']);
prebid.requestId = bid.impid;
prebid.ad = bid.adm;
prebid.creativeId = bid.crid;
prebid.cpm = bid.price;
prebid.width = bid.w;
prebid.height = bid.h;
prebid.mediaType = 'banner';
prebid.meta = {
advertiserDomains: bid.adomain,
advertiserId: bid.adid,
mediaType: 'banner',
primaryCatId: bid.cat[0] || '',
secondaryCatIds: bid.cat
};
prebidResponse.push(prebid);
});
}
});
}
});
return prebidResponse;
}
registerBidder(spec);