Skip to content

Commit

Permalink
Serverbid 1.0 (#1802)
Browse files Browse the repository at this point in the history
* Serverbid: fix ad refreshes

* Serverbid 1.0 Adapter

* Add adsparc alias, maintainer email.

* serverbidBidAdapter: updated docs with working examples

* Updates per PR feedback.
  • Loading branch information
jcswart authored and matthewlane committed Dec 4, 2017
1 parent f93c2dd commit 1614a66
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 329 deletions.
308 changes: 146 additions & 162 deletions modules/serverbidBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,121 +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';

const bidIds = [];

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', 'adsparc'],

/**
* 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);
includePricingData: true,
parallel: true
}, validBidRequests[0].params);

const bids = params.bids || [];

for (let i = 0; i < bids.length; i++) {
const bid = bids[i];

bidIds.push(bid.bidId);
validBidRequests.map(bid => {
let config = CONFIG[bid.bidder];
ENDPOINT_URL = config.BASE_URI;

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

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

function _responseCallback(result) {
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 bidCode;
let placementCode;
let bidResponses = [];

try {
result = JSON.parse(result);
} catch (error) {
utils.logError(error);
}
bids = bidRequest.bidRequest;

for (let i = 0; i < bidIds.length; i++) {
bidId = bidIds[i];
bidObj = utils.getBidRequest(bidId);
bidCode = bidObj.bidder;
placementCode = bidObj.placementCode;
serverResponse = (serverResponse || {}).body;
for (let i = 0; i < bids.length; i++) {
bid = {};
bidObj = bids[i];
bidId = bidObj.bidId;

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

if (decision && price) {
bid = bidfactory.createBid(1, bidObj);
bid.bidderCode = bidCode;
bid.requestId = bidId;
bid.cpm = price;
bid.width = decision.width;
bid.height = decision.height;
bid.ad = retrieveAd(decision);
} else {
bid = bidfactory.createBid(2, bidObj);
bid.bidderCode = bidCode;
bid.currency = 'USD';
bid.creativeId = decision.adId;
bid.ttl = 360;
bid.netRevenue = true;
bid.referrer = utils.getTopWindowUrl();

bidResponses.push(bid);
}
} else {
bid = bidfactory.createBid(2, bidObj);
bid.bidderCode = bidCode;
}
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);
44 changes: 44 additions & 0 deletions modules/serverbidBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Overview

Module Name: Serverbid Bid Adapter

Module Type: Bid Adapter

Maintainer: jgrimes@serverbid.com, jswart@serverbid.com

# Description

Connects to Serverbid for receiving bids from configured demand sources.

# Test Parameters
```javascript
var adUnits = [
{
code: 'test-ad-1',
sizes: [[300, 250]],
bids: [
{
bidder: 'serverbid',
params: {
networkId: '9969',
siteId: '980639'
}
}
]
},
{
code: 'test-ad-2',
sizes: [[300, 250]],
bids: [
{
bidder: 'serverbid',
params: {
networkId: '9969',
siteId: '980639',
zoneId: '178503'
}
}
]
}
];
```
Loading

0 comments on commit 1614a66

Please sign in to comment.