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

Update adapter to prebid v1.0 #1908

Merged
merged 9 commits into from
Jan 23, 2018
141 changes: 84 additions & 57 deletions modules/smartadserverBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,87 @@
var utils = require('src/utils.js');
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader.js');
var url = require('src/url.js');
var adaptermanager = require('src/adaptermanager');
import * as utils from 'src/utils';
import {
config
} from 'src/config';
import {
registerBidder
} from 'src/adapters/bidderFactory';
const BIDDER_CODE = 'smartadserver';
export const spec = {
code: BIDDER_CODE,
aliases: ['smart'], // short code
/**
* 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 && bid.params.siteId && bid.params.pageId && bid.params.formatId && bid.params.domain);
},
/**
* 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) {
// use bidderRequest.bids[] to get bidder-dependent request info

var SmartAdServer = function SmartAdServer() {
var generateCallback = function(bid) {
var callbackId = 'sas_' + utils.getUniqueIdentifierStr();
$$PREBID_GLOBAL$$[callbackId] = function(adUnit) {
var bidObject;
if (adUnit) {
utils.logMessage(`[SmartAdServer] bid response for placementCode ${bid.placementCode}`);
bidObject = bidfactory.createBid(1);
bidObject.bidderCode = 'smartadserver';
bidObject.cpm = adUnit.cpm;
bidObject.currency = adUnit.currency;
bidObject.ad = adUnit.ad;
bidObject.width = adUnit.width;
bidObject.height = adUnit.height;
bidObject.dealId = adUnit.dealId;
bidmanager.addBidResponse(bid.placementCode, bidObject);
} else {
utils.logMessage(`[SmartAdServer] no bid response for placementCode ${bid.placementCode}`);
bidObject = bidfactory.createBid(2);
bidObject.bidderCode = 'smartadserver';
bidmanager.addBidResponse(bid.placementCode, bidObject);
}
};
return callbackId;
};
// if your bidder supports multiple currencies, use config.getConfig(currency)
// to find which one the ad server needs

return {
callBids: function(params) {
for (var i = 0; i < params.bids.length; i++) {
var bid = params.bids[i];
var adCall = url.parse(bid.params.domain);
adCall.pathname = '/prebid';
adCall.search = {
'pbjscbk': '$$PREBID_GLOBAL$$.' + generateCallback(bid),
'siteid': bid.params.siteId,
'pgid': bid.params.pageId,
'fmtid': bid.params.formatId,
'ccy': bid.params.currency || 'USD',
'bidfloor': bid.params.bidfloor || 0.0,
'tgt': encodeURIComponent(bid.params.target || ''),
'tag': bid.placementCode,
'sizes': bid.sizes.map(size => size[0] + 'x' + size[1]).join(','),
'async': 1
};
adloader.loadScript(url.format(adCall));
}
// pull requested transaction ID from bidderRequest.bids[].transactionId
var bid = validBidRequests[0];
const payload = {
siteid: bid.params.siteId,
pageid: bid.params.pageId,
formatid: bid.params.formatId,
currencyCode: config.getConfig('currency').adServerCurrency,
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should use config.getConfig('currency.adServerCurency') since the config module does safe deep access. The way you have it here if currency hasn't been set then it will throw "can't read adServerCurrency of undefined" error.

bidfloor: bid.params.bidfloor || 0.0,
targeting: bid.params.target && bid.params.target != '' ? bid.params.target : undefined,
tagId: bid.adUnitCode,
sizes: bid.sizes.map(size => ({
w: size[0],
h: size[1]
})),
pageDomain: utils.getTopWindowUrl(),
transactionId: bid.transactionId,
timeout: config.getConfig('bidderTimeout')
};
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: bid.params.domain + '/prebid/v1',
data: payloadString,
};
},
/**
* 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) {
const bidResponses = [];
var response = serverResponse.body;
if (response) {
const bidResponse = {
requestId: bidRequest.bidId,
bidderCode: spec.code,
Copy link
Collaborator

Choose a reason for hiding this comment

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

bidderCode will be set automatically by bidderFactory now, this line can be dropped

cpm: response.cpm,
width: response.width,
height: response.height,
creativeId: response.creativeId,
dealId: response.dealId,
currency: response.currency,
netRevenue: response.isNetCpm,
ttl: response.ttl,
referrer: utils.getTopWindowUrl(),
ad: response.ad
};
bidResponses.push(bidResponse);
}
};
};

adaptermanager.registerBidAdapter(new SmartAdServer(), 'smartadserver');

module.exports = SmartAdServer;
return bidResponses;
}
}
registerBidder(spec);
34 changes: 34 additions & 0 deletions modules/smartadserverBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Overview

```
Module Name: Smart Ad Server Bidder Adapter
Module Type: Bidder Adapter
```

# Description

Connect to Smart for bids.

The Smart adapter requires setup and approval from the Smart team.
Please reach out to your Technical account manager for more information.

# Test Parameters
```
var adUnits = [
{
code: 'test-div',
sizes: [[300, 250]], // a display size
bids: [
{
bidder: "smart",
params: {
domain: 'http://prg.smartadserver.com',
siteId: 32216,
pageId: 881291,
formatId: 13695,
}
}
]
}
];
```
Loading