-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
truereachBidAdapter.js
executable file
·146 lines (121 loc) · 4.06 KB
/
truereachBidAdapter.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
import { deepAccess, getUniqueIdentifierStr } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
const SUPPORTED_AD_TYPES = [BANNER];
const BIDDER_CODE = 'truereach';
const BIDDER_URL = 'https://ads-sg.momagic.com/exchange/openrtb25/';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: SUPPORTED_AD_TYPES,
isBidRequestValid: function (bidRequest) {
return (bidRequest.params.site_id &&
deepAccess(bidRequest, 'mediaTypes.banner') && (deepAccess(bidRequest, 'mediaTypes.banner.sizes.length') > 0));
},
buildRequests: function (validBidRequests, bidderRequest) {
if (validBidRequests.length === 0) {
return [];
}
let queryParams = buildCommonQueryParamsFromBids(validBidRequests, bidderRequest);
let siteId = deepAccess(validBidRequests[0], 'params.site_id');
// TODO: should this use auctionId? see #8573
// TODO: fix transactionId leak: https://github.com/prebid/Prebid.js/issues/9781
let url = BIDDER_URL + siteId + '?hb=1&transactionId=' + validBidRequests[0].transactionId;
return {
method: 'POST',
url: url,
data: queryParams,
options: { withCredentials: true }
};
},
interpretResponse: function ({ body: serverResponse }, serverRequest) {
const bidResponses = [];
if ((!serverResponse || !serverResponse.id) ||
(!serverResponse.seatbid || serverResponse.seatbid.length === 0 || !serverResponse.seatbid[0].bid || serverResponse.seatbid[0].bid.length === 0)) {
return bidResponses;
}
let adUnits = serverResponse.seatbid[0].bid;
let bidderBid = adUnits[0];
let responseCPM = parseFloat(bidderBid.price);
if (responseCPM === 0) {
return bidResponses;
}
let responseAd = bidderBid.adm;
if (bidderBid.nurl) {
let responseNurl = '<img src="' + bidderBid.nurl + '" height="0px" width="0px">';
responseAd += responseNurl;
}
const bidResponse = {
requestId: bidderBid.impid,
cpm: responseCPM,
currency: serverResponse.cur || 'USD',
width: parseInt(bidderBid.w),
height: parseInt(bidderBid.h),
ad: decodeURIComponent(responseAd),
ttl: 180,
creativeId: bidderBid.crid,
netRevenue: false
};
if (bidderBid.adomain && bidderBid.adomain.length) {
bidResponse.meta = {
advertiserDomains: bidderBid.adomain,
};
}
bidResponses.push(bidResponse);
return bidResponses;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = []
var gdprParams = '';
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
gdprParams = `?gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
} else {
gdprParams = `?gdpr_consent=${gdprConsent.consentString}`;
}
}
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: 'https://ads-sg.momagic.com/jsp/usersync.jsp' + gdprParams
});
}
return syncs;
}
};
function buildCommonQueryParamsFromBids(validBidRequests, bidderRequest) {
let adW = 0;
let adH = 0;
let adSizes = Array.isArray(validBidRequests[0].params.sizes) ? validBidRequests[0].params.sizes : validBidRequests[0].sizes;
let sizeArrayLength = adSizes.length;
if (sizeArrayLength === 2 && typeof adSizes[0] === 'number' && typeof adSizes[1] === 'number') {
adW = adSizes[0];
adH = adSizes[1];
} else {
adW = adSizes[0][0];
adH = adSizes[0][1];
}
let domain = window.location.host;
let page = window.location.host + window.location.pathname + location.search + location.hash;
let defaultParams = {
id: getUniqueIdentifierStr(),
imp: [
{
id: validBidRequests[0].bidId,
banner: {
w: adW,
h: adH
}
}
],
site: {
domain: domain,
page: page
},
device: {
ua: window.navigator.userAgent
},
tmax: bidderRequest.timeout
};
return defaultParams;
}
registerBidder(spec);