Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverbid 1.0 #1802

Merged
merged 6 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is meant by grouping by bidder, but multiple requests would be required for different endpoints.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@idettman That answers this question, thanks! We aren't going to make a change for this now since it isn't a scenario that has come up yet, but will add in support for that at some point.

// 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1748 changed the first argument of interpretResponse to:

{
  body: responseBody,
  headers: {
    get: function(header) { /* returns a header from the HTTP response */ }
  }
}

so adding something like

serverResponse = serverResponse.body;

just below this line, or however you'd prefer to grab the body, and updating corresponding tests if needed should get this back to working properly

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