forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginadBidAdapter.js
186 lines (145 loc) · 5.14 KB
/
nginadBidAdapter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var CONSTANTS = require('src/constants.json');
var utils = require('src/utils.js');
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader');
var adaptermanager = require('src/adaptermanager');
var defaultPlacementForBadBid = null;
/**
* Adapter for requesting bids from NginAd
*/
var NginAdAdapter = function NginAdAdapter() {
var rtbServerDomain = 'placeholder.for.nginad.server.com';
function _callBids(params) {
var nginadBids = params.bids || [];
// De-dupe by tagid then issue single bid request for all bids
_requestBids(_getUniqueTagids(nginadBids));
}
// filter bids to de-dupe them?
function _getUniqueTagids(bids) {
var key;
var map = {};
var PubZoneIds = [];
for (key in bids) {
map[utils.getBidIdParameter('pzoneid', bids[key].params)] = bids[key];
}
for (key in map) {
if (map.hasOwnProperty(key)) {
PubZoneIds.push(map[key]);
}
}
return PubZoneIds;
}
function getWidthAndHeight(bid) {
var adW = null;
var adH = null;
var sizeArrayLength = bid.sizes.length;
if (sizeArrayLength === 2 && typeof bid.sizes[0] === 'number' && typeof bid.sizes[1] === 'number') {
adW = bid.sizes[0];
adH = bid.sizes[1];
} else {
adW = bid.sizes[0][0];
adH = bid.sizes[0][1];
}
return [adW, adH];
}
function _requestBids(bidReqs) {
// build bid request object
var domain = window.location.host;
var page = window.location.pathname + location.search + location.hash;
var nginadImps = [];
// assign the first adUnit (placement) for bad bids;
defaultPlacementForBadBid = bidReqs[0].placementCode;
// build impression array for nginad
utils._each(bidReqs, function(bid) {
var tagId = utils.getBidIdParameter('pzoneid', bid.params);
var bidFloor = utils.getBidIdParameter('bidfloor', bid.params);
var whArr = getWidthAndHeight(bid);
var imp = {
id: bid.bidId,
banner: {
w: whArr[0],
h: whArr[1]
},
tagid: tagId,
bidfloor: bidFloor
};
nginadImps.push(imp);
// bidmanager.pbCallbackMap[imp.id] = bid;
rtbServerDomain = bid.params.nginadDomain;
});
// build bid request with impressions
var nginadBidReq = {
id: utils.getUniqueIdentifierStr(),
imp: nginadImps,
site: {
domain: domain,
page: page
}
};
var scriptUrl = window.location.protocol + '//' + rtbServerDomain + '/bid/rtb?callback=window.$$PREBID_GLOBAL$$.nginadResponse' +
'&br=' + encodeURIComponent(JSON.stringify(nginadBidReq));
adloader.loadScript(scriptUrl);
}
function handleErrorResponse(bidReqs, defaultPlacementForBadBid) {
// no response data
if (defaultPlacementForBadBid === null) {
// no id with which to create an dummy bid
return;
}
var bid = bidfactory.createBid(2);
bid.bidderCode = 'nginad';
bidmanager.addBidResponse(defaultPlacementForBadBid, bid);
}
// expose the callback to the global object:
$$PREBID_GLOBAL$$.nginadResponse = function(nginadResponseObj) {
var bid = {};
var key;
// valid object?
if (!nginadResponseObj || !nginadResponseObj.id) {
return handleErrorResponse(nginadResponseObj, defaultPlacementForBadBid);
}
if (!nginadResponseObj.seatbid || nginadResponseObj.seatbid.length === 0 || !nginadResponseObj.seatbid[0].bid || nginadResponseObj.seatbid[0].bid.length === 0) {
return handleErrorResponse(nginadResponseObj, defaultPlacementForBadBid);
}
for (key in nginadResponseObj.seatbid[0].bid) {
var nginadBid = nginadResponseObj.seatbid[0].bid[key];
var responseCPM;
var placementCode = '';
var id = nginadBid.impid;
// try to fetch the bid request we sent NginAd
var bidObj = $$PREBID_GLOBAL$$._bidsRequested.find(bidSet => bidSet.bidderCode === 'nginad').bids
.find(bid => bid.bidId === id);
if (!bidObj) {
return handleErrorResponse(nginadBid, defaultPlacementForBadBid);
}
placementCode = bidObj.placementCode;
bidObj.status = CONSTANTS.STATUS.GOOD;
// place ad response on bidmanager._adResponsesByBidderId
responseCPM = parseFloat(nginadBid.price);
if (responseCPM === 0) {
handleErrorResponse(nginadBid, id);
}
nginadBid.placementCode = placementCode;
nginadBid.size = bidObj.sizes;
var responseAd = nginadBid.adm;
// store bid response
// bid status is good (indicating 1)
bid = bidfactory.createBid(1);
bid.creative_id = nginadBid.Id;
bid.bidderCode = 'nginad';
bid.cpm = responseCPM;
// The bid is a mock bid, the true bidding process happens after the publisher tag is called
bid.ad = decodeURIComponent(responseAd);
var whArr = getWidthAndHeight(bidObj);
bid.width = whArr[0];
bid.height = whArr[1];
bidmanager.addBidResponse(placementCode, bid);
}
}; // nginadResponse
return {
callBids: _callBids
};
};
adaptermanager.registerBidAdapter(new NginAdAdapter(), 'nginad');
module.exports = NginAdAdapter;