-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
krushmediaBidAdapter.js
158 lines (142 loc) · 5.09 KB
/
krushmediaBidAdapter.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
import { isFn, deepAccess } from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
const BIDDER_CODE = 'krushmedia';
const AD_URL = 'https://ads4.krushmedia.com/?c=rtb&m=hb';
const SYNC_URL = 'https://cs.krushmedia.com/html?src=pbjs'
function isBidResponseValid(bid) {
if (!bid.requestId || !bid.cpm || !bid.creativeId ||
!bid.ttl || !bid.currency) {
return false;
}
switch (bid.mediaType) {
case BANNER:
return Boolean(bid.width && bid.height && bid.ad);
case VIDEO:
return Boolean(bid.vastUrl);
case NATIVE:
return Boolean(bid.native && bid.native.impressionTrackers);
default:
return false;
}
}
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 const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
isBidRequestValid: (bid) => {
return Boolean(bid.bidId && bid.params && !isNaN(parseInt(bid.params.key)));
},
buildRequests: (validBidRequests = [], bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let winTop = window;
let location;
location = bidderRequest?.refererInfo ?? null;
const placements = [];
const request = {
deviceWidth: winTop.screen.width,
deviceHeight: winTop.screen.height,
language: (navigator && navigator.language) ? navigator.language.split('-')[0] : '',
secure: 1,
host: location?.domain ?? '',
page: location?.page ?? '',
placements: placements
};
if (bidderRequest) {
if (bidderRequest.uspConsent) {
request.ccpa = bidderRequest.uspConsent;
}
if (bidderRequest.gdprConsent) {
request.gdpr = bidderRequest.gdprConsent;
}
}
const len = validBidRequests.length;
for (let i = 0; i < len; i++) {
const bid = validBidRequests[i];
const placement = {
key: bid.params.key,
bidId: bid.bidId,
schain: bid.schain || {},
bidFloor: getBidFloor(bid)
};
if (bid.mediaTypes && bid.mediaTypes[BANNER] && bid.mediaTypes[BANNER].sizes) {
placement.traffic = BANNER;
placement.sizes = bid.mediaTypes[BANNER].sizes;
} else if (bid.mediaTypes && bid.mediaTypes[VIDEO] && bid.mediaTypes[VIDEO].playerSize) {
placement.traffic = VIDEO;
placement.wPlayer = bid.mediaTypes[VIDEO].playerSize[0];
placement.hPlayer = bid.mediaTypes[VIDEO].playerSize[1];
placement.minduration = bid.mediaTypes[VIDEO].minduration;
placement.maxduration = bid.mediaTypes[VIDEO].maxduration;
placement.mimes = bid.mediaTypes[VIDEO].mimes;
placement.protocols = bid.mediaTypes[VIDEO].protocols;
placement.startdelay = bid.mediaTypes[VIDEO].startdelay;
placement.placement = bid.mediaTypes[VIDEO].placement;
placement.plcmt = bid.mediaTypes[VIDEO].plcmt;
placement.skip = bid.mediaTypes[VIDEO].skip;
placement.skipafter = bid.mediaTypes[VIDEO].skipafter;
placement.minbitrate = bid.mediaTypes[VIDEO].minbitrate;
placement.maxbitrate = bid.mediaTypes[VIDEO].maxbitrate;
placement.delivery = bid.mediaTypes[VIDEO].delivery;
placement.playbackmethod = bid.mediaTypes[VIDEO].playbackmethod;
placement.api = bid.mediaTypes[VIDEO].api;
placement.linearity = bid.mediaTypes[VIDEO].linearity;
} else if (bid.mediaTypes && bid.mediaTypes[NATIVE]) {
placement.traffic = NATIVE;
placement.native = bid.mediaTypes[NATIVE];
}
placements.push(placement);
}
return {
method: 'POST',
url: AD_URL,
data: request
};
},
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;
},
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent) => {
let syncUrl = SYNC_URL
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}`;
}
}
if (uspConsent && uspConsent.consentString) {
syncUrl += `&ccpa_consent=${uspConsent.consentString}`;
}
return [{
type: 'iframe',
url: syncUrl
}];
}
};
registerBidder(spec);