forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartytechBidAdapter.js
148 lines (118 loc) · 3.53 KB
/
smartytechBidAdapter.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
import {buildUrl, deepAccess} from '../src/utils.js'
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
const BIDDER_CODE = 'smartytech';
export const ENDPOINT_PROTOCOL = 'https';
export const ENDPOINT_DOMAIN = 'server.smartytech.io';
export const ENDPOINT_PATH = '/hb/v2/bidder';
export const spec = {
supportedMediaTypes: [ BANNER, VIDEO ],
code: BIDDER_CODE,
isBidRequestValid: function (bidRequest) {
return (
!!parseInt(bidRequest.params.endpointId) &&
spec._validateBanner(bidRequest) &&
spec._validateVideo(bidRequest)
);
},
_validateBanner: function(bidRequest) {
const bannerAdUnit = deepAccess(bidRequest, 'mediaTypes.banner');
if (bannerAdUnit === undefined) {
return true;
}
if (!Array.isArray(bannerAdUnit.sizes)) {
return false;
}
return true;
},
_validateVideo: function(bidRequest) {
const videoAdUnit = deepAccess(bidRequest, 'mediaTypes.video');
if (videoAdUnit === undefined) {
return true;
}
if (!Array.isArray(videoAdUnit.playerSize)) {
return false;
}
if (!videoAdUnit.context) {
return false;
}
return true;
},
buildRequests: function (validBidRequests, bidderRequest) {
const referer = bidderRequest?.refererInfo?.page || window.location.href;
const bidRequests = validBidRequests.map((validBidRequest) => {
let video = deepAccess(validBidRequest, 'mediaTypes.video', false);
let banner = deepAccess(validBidRequest, 'mediaTypes.banner', false);
let sizes = validBidRequest.params.sizes;
let oneRequest = {
endpointId: validBidRequest.params.endpointId,
adUnitCode: validBidRequest.adUnitCode,
referer: referer,
bidId: validBidRequest.bidId
};
if (video) {
oneRequest.video = video;
if (sizes) {
oneRequest.video.sizes = sizes;
}
} else if (banner) {
oneRequest.banner = banner;
if (sizes) {
oneRequest.banner.sizes = sizes;
}
}
return oneRequest
});
let adPartnerRequestUrl = buildUrl({
protocol: ENDPOINT_PROTOCOL,
hostname: ENDPOINT_DOMAIN,
pathname: ENDPOINT_PATH,
});
return {
method: 'POST',
url: adPartnerRequestUrl,
data: bidRequests
};
},
interpretResponse: function (serverResponse, bidRequest) {
if (typeof serverResponse.body === 'undefined') {
return [];
}
const validBids = bidRequest.data;
const keys = Object.keys(serverResponse.body)
const responseBody = serverResponse.body;
return keys.filter(key => {
return responseBody[key].ad
}).map(key => {
return {
bid: validBids.find(b => b.adUnitCode === key),
response: responseBody[key]
}
}).map(item => spec._adResponse(item.bid, item.response));
},
_adResponse: function (request, response) {
const bidObject = {
requestId: request.bidId,
adUnitCode: request.adUnitCode,
ad: response.ad,
cpm: response.cpm,
width: response.width,
height: response.height,
ttl: 60,
creativeId: response.creativeId,
netRevenue: true,
currency: response.currency,
mediaType: BANNER,
meta: {}
};
if (response.mediaType === VIDEO) {
bidObject.vastXml = response.ad;
bidObject.mediaType = VIDEO;
}
if (response.meta) {
bidObject.meta = response.meta;
}
return bidObject;
},
}
registerBidder(spec);