-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
precisoBidAdapter.js
205 lines (176 loc) · 5.74 KB
/
precisoBidAdapter.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { isFn, deepAccess, logInfo } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
const BIDDER_CODE = 'preciso';
const AD_URL = 'https://ssp-bidder.mndtrk.com/bid_request/openrtb';
const URL_SYNC = 'https://ck.2trk.info/rtb/user/usersync.aspx?';
const SUPPORTED_MEDIA_TYPES = [BANNER, NATIVE, VIDEO];
const GVLID = 874;
let userId = 'NA';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: SUPPORTED_MEDIA_TYPES,
gvlid: GVLID,
isBidRequestValid: (bid) => {
return Boolean(bid.bidId && bid.params && !isNaN(bid.params.publisherId) && bid.params.host == 'prebid');
},
buildRequests: (validBidRequests = [], bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
// userId = validBidRequests[0].userId.pubcid;
let winTop = window;
let location;
var offset = new Date().getTimezoneOffset();
logInfo('timezone ' + offset);
var city = Intl.DateTimeFormat().resolvedOptions().timeZone;
logInfo('location test' + city)
const countryCode = getCountryCodeByTimezone(city);
logInfo(`The country code for ${city} is ${countryCode}`);
location = bidderRequest?.refererInfo ?? null;
let request = {
id: validBidRequests[0].bidderRequestId,
imp: validBidRequests.map(request => {
const { bidId, sizes, mediaType, ortb2 } = request
const item = {
id: bidId,
region: request.params.region,
traffic: mediaType,
bidFloor: getBidFloor(request),
ortb2: ortb2
}
if (request.mediaTypes.banner) {
item.banner = {
format: (request.mediaTypes.banner.sizes || sizes).map(size => {
return { w: size[0], h: size[1] }
}),
}
}
if (request.schain) {
item.schain = request.schain;
}
if (request.floorData) {
item.bidFloor = request.floorData.floorMin;
}
return item
}),
auctionId: validBidRequests[0].auctionId,
'deviceWidth': winTop.screen.width,
'deviceHeight': winTop.screen.height,
'language': (navigator && navigator.language) ? navigator.language : '',
geo: navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords;
return {
latitude: latitude,
longitude: longitude
}
// Show a map centered at latitude / longitude.
}) || { utcoffset: new Date().getTimezoneOffset() },
city: city,
'host': location?.domain ?? '',
'page': location?.page ?? '',
'coppa': config.getConfig('coppa') === true ? 1 : 0
// userId: validBidRequests[0].userId
};
request.language.indexOf('-') != -1 && (request.language = request.language.split('-')[0])
if (bidderRequest) {
if (bidderRequest.uspConsent) {
request.ccpa = bidderRequest.uspConsent;
}
if (bidderRequest.gdprConsent) {
request.gdpr = bidderRequest.gdprConsent
}
if (bidderRequest.gppConsent) {
request.gpp = bidderRequest.gppConsent;
}
}
return {
method: 'POST',
url: AD_URL,
data: request,
};
},
interpretResponse: function (serverResponse) {
const response = serverResponse.body
const bids = []
response.seatbid.forEach(seat => {
seat.bid.forEach(bid => {
bids.push({
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.crid,
ad: bid.adm,
currency: 'USD',
netRevenue: true,
ttl: 300,
meta: {
advertiserDomains: bid.adomain || [],
},
})
})
})
return bids
},
getUserSyncs: (syncOptions, serverResponses = [], gdprConsent = {}, uspConsent = '', gppConsent = '') => {
let syncs = [];
let { gdprApplies, consentString = '' } = gdprConsent;
if (serverResponses.length > 0) {
logInfo('preciso bidadapter getusersync serverResponses:' + serverResponses.toString);
}
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: `${URL_SYNC}id=${userId}&gdpr=${gdprApplies ? 1 : 0}&gdpr_consent=${consentString}&us_privacy=${uspConsent}&t=4`
});
} else {
syncs.push({
type: 'image',
url: `${URL_SYNC}&gdpr=${gdprApplies ? 1 : 0}&gdpr_consent=${consentString}&us_privacy=${uspConsent}&t=2`
});
}
return syncs
}
};
function getCountryCodeByTimezone(city) {
try {
const now = new Date();
const options = {
timeZone: city,
timeZoneName: 'long',
};
const [timeZoneName] = new Intl.DateTimeFormat('en-US', options)
.formatToParts(now)
.filter((part) => part.type === 'timeZoneName');
if (timeZoneName) {
// Extract the country code from the timezone name
const parts = timeZoneName.value.split('-');
if (parts.length >= 2) {
return parts[1];
}
}
} catch (error) {
// Handle errors, such as an invalid timezone city
logInfo(error);
}
// Handle the case where the city is not found or an error occurred
return 'Unknown';
}
function getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return deepAccess(bid, 'params.bidFloor', 0);
}
try {
const bidFloor = bid.getFloor({
currency: 'USD',
mediaType: '*',
size: '*',
});
return bidFloor.floor;
} catch (_) {
return 0
}
}
registerBidder(spec);