Skip to content

Commit

Permalink
Serverbid 1.0 Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrimes committed Nov 8, 2017
1 parent dce0a8a commit 0189c71
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 312 deletions.
332 changes: 159 additions & 173 deletions modules/serverbidBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,75 @@
import Adapter from 'src/adapter';
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import { ajax } from 'src/ajax';
import adaptermanager from 'src/adaptermanager';

var ServerBidAdapter;
ServerBidAdapter = function ServerBidAdapter() {
const baseAdapter = new Adapter('serverbid');

const CONFIG = {
'serverbid': {
'BASE_URI': 'https://e.serverbid.com/api/v2',
'SMARTSYNC_BASE_URI': 'https://s.zkcdn.net/ss'
},
'connectad': {
'BASE_URI': 'https://i.connectad.io/api/v2',
'SMARTSYNC_BASE_URI': 'https://s.zkcdn.net/ss'
},
'onefiftytwo': {
'BASE_URI': 'https://e.serverbid.com/api/v2',
'SMARTSYNC_BASE_URI': 'https://s.zkcdn.net/ss'
}
};

const SMARTSYNC_CALLBACK = 'serverbidCallBids';

const sizeMap = [
null,
'120x90',
'120x90',
'468x60',
'728x90',
'300x250',
'160x600',
'120x600',
'300x100',
'180x150',
'336x280',
'240x400',
'234x60',
'88x31',
'120x60',
'120x240',
'125x125',
'220x250',
'250x250',
'250x90',
'0x0',
'200x90',
'300x50',
'320x50',
'320x480',
'185x185',
'620x45',
'300x125',
'800x250'
];

sizeMap[77] = '970x90';
sizeMap[123] = '970x250';
sizeMap[43] = '300x600';

baseAdapter.callBids = function(params) {
if (params && params.bids &&
utils.isArray(params.bids) &&
params.bids.length &&
CONFIG[params.bidderCode]) {
const config = CONFIG[params.bidderCode];
config.request = window[params.bidderCode.toUpperCase() + '_CONFIG'];
if (!window.SMARTSYNC) {
_callBids(config, params);
} else {
window[SMARTSYNC_CALLBACK] = function() {
window[SMARTSYNC_CALLBACK] = function() {};
_callBids(config, params);
};

const siteId = params.bids[0].params.siteId;
_appendScript(config.SMARTSYNC_BASE_URI + '/' + siteId + '.js');

const sstimeout = window.SMARTSYNC_TIMEOUT || ((params.timeout || 500) / 2);
setTimeout(function() {
var cb = window[SMARTSYNC_CALLBACK];
window[SMARTSYNC_CALLBACK] = function() {};
cb();
}, sstimeout);
}
import { registerBidder } from 'src/adapters/bidderFactory';

const BIDDER_CODE = 'serverbid';

const CONFIG = {
'serverbid': {
'BASE_URI': 'https://e.serverbid.com/api/v2'
},
'connectad': {
'BASE_URI': 'https://i.connectad.io/api/v2'
},
'onefiftytwo': {
'BASE_URI': 'https://e.serverbid.com/api/v2'
},
'insticator': {
'BASE_URI': 'https://e.serverbid.com/api/v2'
}
};

export const spec = {
code: BIDDER_CODE,
aliases: ['connectad', 'onefiftytwo', 'insticator'],

/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid.params.networkId && bid.params.siteId);
},

/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/

buildRequests: function(validBidRequests) {
// Do we need to group by bidder? i.e. to make multiple requests for
// different endpoints.

let ret = {
method: 'POST',
url: '',
data: '',
bidRequest: []
};

if (validBidRequests.length < 1) {
return ret;
}
};

function _appendScript(src) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}
let ENDPOINT_URL;

function _callBids(config, params) {
const data = Object.assign({
placements: [],
time: Date.now(),
user: {},
url: utils.getTopWindowUrl(),
referrer: document.referrer,
enableBotFiltering: true,
includePricingData: true
}, config.request);

const bids = params.bids || [];
includePricingData: true,
parallel: true
}, validBidRequests[0].params);

for (let i = 0; i < bids.length; i++) {
const bid = bids[i];
validBidRequests.map(bid => {
let config = CONFIG[bid.bidder];
ENDPOINT_URL = config.BASE_URI;

const placement = Object.assign({
divName: bid.bidId,
Expand All @@ -121,86 +79,114 @@ ServerBidAdapter = function ServerBidAdapter() {
if (placement.networkId && placement.siteId) {
data.placements.push(placement);
}
}
});

if (data.placements.length) {
ajax(config.BASE_URI, _responseCallback(bids), JSON.stringify(data), { method: 'POST', withCredentials: true, contentType: 'application/json' });
}
}
ret.data = JSON.stringify(data);
ret.bidRequest = validBidRequests;
ret.url = ENDPOINT_URL;

function _responseCallback(bids) {
return (function (result) {
let bid;
let bidId;
let bidObj;
let bidCode;
let placementCode;

try {
result = JSON.parse(result);
} catch (error) {
utils.logError(error);
}
return ret;
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, bidRequest) {
let bid;
let bids;
let bidId;
let bidObj;
let bidResponses = [];

for (let i = 0; i < bids.length; i++) {
bidObj = bids[i];
bidId = bidObj.bidId;
bidCode = bidObj.bidder;
placementCode = bidObj.placementCode;

if (result) {
const decision = result.decisions && result.decisions[bidId];
const price = decision && decision.pricing && decision.pricing.clearPrice;

if (decision && price) {
bid = bidfactory.createBid(1);
bid.bidderCode = bidCode;
bid.cpm = price;
bid.width = decision.width;
bid.height = decision.height;
bid.ad = retrieveAd(decision);
} else {
bid = bidfactory.createBid(2);
bid.bidderCode = bidCode;
}
} else {
bid = bidfactory.createBid(2);
bid.bidderCode = bidCode;
bids = bidRequest.bidRequest;

for (let i = 0; i < bids.length; i++) {
bid = {};
bidObj = bids[i];
bidId = bidObj.bidId;
bid.bidderCode = bidObj.bidder;

if (serverResponse) {
const decision = serverResponse.decisions && serverResponse.decisions[bidId];
const price = decision && decision.pricing && decision.pricing.clearPrice;

if (decision && price) {
bid.requestId = bidId;
bid.cpm = price;
bid.width = decision.width;
bid.height = decision.height;
bid.ad = retrieveAd(decision);
bid.currency = 'USD';
bid.creativeId = decision.adId;
bid.ttl = 360;
bid.netRevenue = true;
bid.referrer = utils.getTopWindowUrl();

bidResponses.push(bid);
}
bidmanager.addBidResponse(placementCode, bid);
}
});
}
}

function retrieveAd(decision) {
return decision.contents && decision.contents[0] && decision.contents[0].body + utils.createTrackPixelHtml(decision.impressionUrl);
}
return bidResponses;
},

function getSize(sizes) {
const result = [];
sizes.forEach(function(size) {
const index = sizeMap.indexOf(size[0] + 'x' + size[1]);
if (index >= 0) {
result.push(index);
}
});
return result;
getUserSyncs: function(syncOptions) {
return [];
}

// Export the `callBids` function, so that Prebid.js can execute
// this function when the page asks to send out bid requests.
return Object.assign(this, {
callBids: baseAdapter.callBids,
setBidderCode: baseAdapter.setBidderCode
});
};

ServerBidAdapter.createNew = function() {
return new ServerBidAdapter();
};
const sizeMap = [
null,
'120x90',
'120x90',
'468x60',
'728x90',
'300x250',
'160x600',
'120x600',
'300x100',
'180x150',
'336x280',
'240x400',
'234x60',
'88x31',
'120x60',
'120x240',
'125x125',
'220x250',
'250x250',
'250x90',
'0x0',
'200x90',
'300x50',
'320x50',
'320x480',
'185x185',
'620x45',
'300x125',
'800x250'
];

sizeMap[77] = '970x90';
sizeMap[123] = '970x250';
sizeMap[43] = '300x600';

function getSize(sizes) {
const result = [];
sizes.forEach(function(size) {
const index = sizeMap.indexOf(size[0] + 'x' + size[1]);
if (index >= 0) {
result.push(index);
}
});
return result;
}

adaptermanager.registerBidAdapter(new ServerBidAdapter(), 'serverbid');
adaptermanager.aliasBidAdapter('serverbid', 'connectad');
adaptermanager.aliasBidAdapter('serverbid', 'onefiftytwo');
function retrieveAd(decision) {
return decision.contents && decision.contents[0] && decision.contents[0].body + utils.createTrackPixelHtml(decision.impressionUrl);
}

module.exports = ServerBidAdapter;
registerBidder(spec);
Loading

0 comments on commit 0189c71

Please sign in to comment.