forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbidUtilsCommon.js
162 lines (141 loc) · 4.26 KB
/
bidUtilsCommon.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
import { config } from '../../src/config.js';
import {
isFn,
isStr,
deepAccess,
getWindowTop,
triggerPixel
} from '../../src/utils.js';
import { BANNER, VIDEO, NATIVE } from '../../src/mediaTypes.js';
function isBidResponseValid(bid) {
if (!bid.requestId || !bid.cpm || !bid.creativeId ||
!bid.ttl || !bid.currency || !bid.meta) {
return false;
}
switch (bid.mediaType) {
case BANNER:
return Boolean(bid.width && bid.height && bid.ad);
case VIDEO:
return Boolean(bid.vastXml || bid.vastUrl);
case NATIVE:
return Boolean(bid.native && bid.native.impressionTrackers);
default:
return false;
}
}
export 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
}
}
export function isBidRequestValid(bid) {
return Boolean(bid.bidId && bid.params && bid.params.placementId);
}
export const buildBidRequests = (adurl) => (validBidRequests = [], bidderRequest) => {
const winTop = getWindowTop();
const location = winTop.location;
const placements = [];
const request = {
deviceWidth: winTop.screen.width,
deviceHeight: winTop.screen.height,
language: (navigator && navigator.language) ? navigator.language.split('-')[0] : '',
host: location.host,
page: location.pathname,
placements: placements
};
consentCheck(bidderRequest, request);
const len = validBidRequests.length;
for (let i = 0; i < len; i++) {
const bid = validBidRequests[i];
const placement = {
placementId: bid.params.placementId,
bidId: bid.bidId,
schain: bid.schain || {},
bidfloor: getBidFloor(bid)
};
if (typeof bid.userId !== 'undefined') {
placement.userId = bid.userId;
}
const mediaType = bid.mediaTypes;
if (mediaType && mediaType[BANNER] && mediaType[BANNER].sizes) {
placement.sizes = mediaType[BANNER].sizes;
placement.adFormat = BANNER;
}
placements.push(placement);
}
return {
method: 'POST',
url: adurl,
data: request
};
}
export function interpretResponse(serverResponse) {
let response = [];
for (let i = 0; i < serverResponse.body.length; i++) {
let resItem = serverResponse.body[i];
if (isBidResponseValid(resItem)) {
const advertiserDomains = resItem.adomain && resItem.adomain.length ? resItem.adomain : [];
resItem.meta = { ...resItem.meta, advertiserDomains };
response.push(resItem);
}
}
return response;
}
export function consentCheck(bidderRequest, req) {
if (bidderRequest) {
if (bidderRequest.uspConsent) {
req.ccpa = bidderRequest.uspConsent;
}
if (bidderRequest.gdprConsent) {
req.gdpr = bidderRequest.gdprConsent
}
if (bidderRequest.gppConsent) {
req.gpp = bidderRequest.gppConsent;
}
}
}
export const buildUserSyncs = (syncOptions, serverResponses, gdprConsent, uspConsent, syncEndpoint) => {
let syncType = syncOptions.iframeEnabled ? 'iframe' : 'image';
const isCk2trk = syncEndpoint.includes('ck.2trk.info');
let syncUrl = isCk2trk ? syncEndpoint : `${syncEndpoint}/${syncType}?pbjs=1`;
if (gdprConsent && gdprConsent.consentString) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
syncUrl += `&gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
} else {
syncUrl += `&gdpr=0&gdpr_consent=${gdprConsent.consentString}`;
}
} else {
syncUrl += isCk2trk ? `&gdpr=0&gdpr_consent=` : '';
}
if (isCk2trk) {
syncUrl += uspConsent ? `&us_privacy=${uspConsent}` : `&us_privacy=`;
syncUrl += (syncOptions.iframeEnabled) ? `&t=4` : `&t=2`
} else {
if (uspConsent && uspConsent.consentString) {
syncUrl += `&ccpa_consent=${uspConsent.consentString}`;
}
const coppa = config.getConfig('coppa') ? 1 : 0;
syncUrl += `&coppa=${coppa}`;
}
return [{
type: syncType,
url: syncUrl
}];
}
export function bidWinReport (bid) {
const cpm = deepAccess(bid, 'adserverTargeting.hb_pb') || '';
if (isStr(bid.nurl) && bid.nurl !== '') {
bid.nurl = bid.nurl.replace(/\${AUCTION_PRICE}/, cpm);
triggerPixel(bid.nurl);
}
}