forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowadvertisingBidAdapter.js
167 lines (142 loc) · 4.56 KB
/
growadvertisingBidAdapter.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
'use strict';
import {deepAccess, _each, triggerPixel, getBidIdParameter} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
const BIDDER_CODE = 'growads';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
isBidRequestValid: function (bid) {
return bid.params && !!bid.params.zoneId;
},
buildRequests: function (validBidRequests) {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let zoneId;
let domain;
let requestURI;
let data = {};
const zoneCounters = {};
return validBidRequests.map(bidRequest => {
zoneId = getBidIdParameter('zoneId', bidRequest.params);
domain = getBidIdParameter('domain', bidRequest.params);
if (!(zoneId in zoneCounters)) {
zoneCounters[zoneId] = 0;
}
if (typeof domain === 'undefined' || domain.length === 0) {
domain = 'portal.growadvertising.com';
}
requestURI = 'https://' + domain + '/adserve/bid';
data = {
type: 'prebidjs',
zoneId: zoneId,
i: zoneCounters[zoneId]
};
zoneCounters[zoneId]++;
return {
method: 'GET',
url: requestURI,
data: data,
bidRequest: bidRequest
};
});
},
interpretResponse: function (serverResponse, bidRequest) {
const request = bidRequest.bidRequest;
let bidResponses = [];
let CPM;
let width;
let height;
let response;
let isCorrectSize = false;
let isCorrectCPM = true;
let minCPM;
let maxCPM;
let bid = {};
let body = serverResponse.body;
try {
response = JSON.parse(body);
} catch (ex) {
response = body;
}
if (response && response.status === 'success' && request) {
CPM = parseFloat(response.cpm);
width = parseInt(response.width);
height = parseInt(response.height);
minCPM = getBidIdParameter('minCPM', request.params);
maxCPM = getBidIdParameter('maxCPM', request.params);
width = parseInt(response.width);
height = parseInt(response.height);
// Ensure response CPM is within the given bounds
if (minCPM !== '' && CPM < parseFloat(minCPM)) {
isCorrectCPM = false;
}
if (maxCPM !== '' && CPM > parseFloat(maxCPM)) {
isCorrectCPM = false;
}
if (isCorrectCPM) {
bid = {
requestId: request.bidId,
bidderCode: request.bidder,
creativeId: response.creativeId,
cpm: CPM,
width: width,
height: height,
currency: response.currency,
netRevenue: true,
ttl: response.ttl,
adUnitCode: request.adUnitCode,
// TODO: is 'page' the right value here?
referrer: deepAccess(request, 'refererInfo.page')
};
if (response.hasOwnProperty(NATIVE)) {
bid[NATIVE] = {
title: response[NATIVE].title,
body: response[NATIVE].body,
body2: response[NATIVE].body2,
cta: response[NATIVE].cta,
sponsoredBy: response[NATIVE].sponsoredBy,
clickUrl: response[NATIVE].clickUrl,
impressionTrackers: response[NATIVE].impressionTrackers,
};
if (response[NATIVE].image) {
bid[NATIVE].image = {
url: response[NATIVE].image.url,
height: response[NATIVE].image.height,
width: response[NATIVE].image.width
};
}
if (response[NATIVE].icon) {
bid[NATIVE].icon = {
url: response[NATIVE].icon.url,
height: response[NATIVE].icon.height,
width: response[NATIVE].icon.width
};
}
bid.mediaType = NATIVE;
isCorrectSize = true;
} else {
bid.ad = response.ad;
bid.mediaType = BANNER;
// Ensure that response ad matches one of the placement sizes.
_each(deepAccess(request, 'mediaTypes.banner.sizes', []), function (size) {
if (width === size[0] && height === size[1]) {
isCorrectSize = true;
}
});
}
if (isCorrectSize) {
bidResponses.push(bid);
}
}
}
return bidResponses;
},
onBidWon: function (bid) {
if (bid.vurl) {
triggerPixel(bid.vurl);
}
},
};
registerBidder(spec);