-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
widespace.js
115 lines (91 loc) · 3.22 KB
/
widespace.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
import { getBidRequest } from '../utils.js';
const utils = require('../utils.js');
const adloader = require('../adloader.js');
const bidmanager = require('../bidmanager.js');
const bidfactory = require('../bidfactory.js');
const WS_ADAPTER_VERSION = '1.0.1';
function WidespaceAdapter() {
let useSSL = 'https:' === document.location.protocol,
baseURL = (useSSL ? 'https:' : 'http:') + '//engine.widespace.com/map/engine/hb/dynamic?',
callbackName = '$$PREBID_GLOBAL$$.widespaceHandleCB';
function _callBids(params) {
let bids = params && params.bids || [];
for (var i = 0; i < bids.length; i++) {
const bid = bids[i],
callbackUid = bid.bidId,
sid = bid.params.sid,
currency = bid.params.cur || bid.params.currency;
//Handle Sizes string
let sizeQueryString = '';
let parsedSizes = utils.parseSizesInput(bid.sizes);
sizeQueryString = parsedSizes.reduce((prev, curr) => {
return prev ? `${prev},${curr}` : curr;
}, sizeQueryString);
let requestURL = baseURL;
requestURL = utils.tryAppendQueryString(requestURL, 'hb.ver', WS_ADAPTER_VERSION);
const params = {
'hb': '1',
'hb.name': 'prebidjs',
'hb.callback': callbackName,
'hb.callbackUid': callbackUid,
'hb.sizes': sizeQueryString,
'hb.currency': currency,
'sid': sid
};
requestURL += '#';
// Append all params to requestURL
for (let key of Object.keys(params)) {
requestURL += key + '=' + params[key] + '&';
}
// Expose the callback
$$PREBID_GLOBAL$$.widespaceHandleCB = window[callbackName] = handleCallback;
adloader.loadScript(requestURL);
}
}
//Handle our callback
var handleCallback = function handleCallback(bidsArray) {
if (!bidsArray) { return; }
var bidObject,
bidCode = 'widespace';
for (var i = 0, l = bidsArray.length; i < l; i++) {
var bid = bidsArray[i],
placementCode = '',
validSizes = [];
bid.sizes = {height: bid.height, width: bid.height};
var inBid = getBidRequest(bid.callbackUid);
if (inBid) {
bidCode = inBid.bidder;
placementCode = inBid.placementCode;
validSizes = inBid.sizes;
}
if (bid && bid.callbackUid && bid.status !=='noad' && verifySize(bid.sizes, validSizes)) {
bidObject = bidfactory.createBid(1);
bidObject.bidderCode = bidCode;
bidObject.cpm = bid.cpm;
bidObject.cur = bid.currency;
bidObject.creative_id = bid.adId;
bidObject.ad = bid.code;
bidObject.width = bid.width;
bidObject.height = bid.height;
bidmanager.addBidResponse(placementCode, bidObject);
} else {
bidObject = bidfactory.createBid(2);
bidObject.bidderCode = bidCode;
bidmanager.addBidResponse(placementCode, bidObject);
}
}
function verifySize(bid, validSizes) {
for (var j = 0, k = validSizes.length; j < k; j++) {
if (bid.width === validSizes[j][0] &&
bid.height === validSizes[j][1]) {
return true;
}
}
return false;
}
};
return {
callBids: _callBids
};
}
module.exports = WidespaceAdapter;