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

Media.net adapter added #2039

Merged
merged 1 commit into from
Jan 22, 2018
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
161 changes: 161 additions & 0 deletions modules/medianetBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import * as utils from 'src/utils';

const BIDDER_CODE = 'medianet';
const BID_URL = 'https://prebid.media.net/rtb/prebid';

function siteDetails(site) {
site = site || {};

return {
domain: site.domain || utils.getTopWindowLocation().host,
page: site.page || utils.getTopWindowUrl(),
ref: site.ref || utils.getTopWindowReferrer()
}
}

function filterUrlsByType(urls, type) {
return urls.filter(url => url.type === type);
}

function transformSizes(sizes) {
if (utils.isArray(sizes) && sizes.length === 2 && !utils.isArray(sizes[0])) {
return [getSize(sizes)];
}

return sizes.map(size => getSize(size))
}

function getSize(size) {
return {
w: parseInt(size[0], 10),
h: parseInt(size[1], 10)
}
}

function configuredParams(params) {
return {
customer_id: params.cid
}
}

function slotParams(bidRequest) {
// check with Media.net Account manager for bid floor and crid parameters
let params = {
id: bidRequest.bidId,
ext: {
dfp_id: bidRequest.adUnitCode
},
banner: transformSizes(bidRequest.sizes)
};

if (bidRequest.params.crid) {
params.tagid = bidRequest.params.crid.toString();
}

let bidFloor = parseFloat(bidRequest.params.bidfloor);
if (bidFloor) {
params.bidfloor = bidFloor;
}
return params;
}

function generatePayload(bidRequests) {
return {
site: siteDetails(bidRequests[0].params.site),
ext: configuredParams(bidRequests[0].params),
id: bidRequests[0].bidderRequestId,
imp: bidRequests.map(request => slotParams(request))
}
}

function isValidBid(bid) {
return bid.no_bid === false && parseFloat(bid.cpm) > 0.0;
}

function fetchCookieSyncUrls(response) {
if (!utils.isEmpty(response) && response[0].body &&
response[0].body.ext && utils.isArray(response[0].body.ext.csUrl)) {
return response[0].body.ext.csUrl;
}

return [];
}

export const spec = {

code: BIDDER_CODE,

/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid (if cid is present), and false otherwise.
*/
isBidRequestValid: function(bid) {
if (!bid.params) {
utils.logError(`${BIDDER_CODE} : Missing bid parameters`);
return false;
}

if (!bid.params.cid || !utils.isStr(bid.params.cid) || utils.isEmptyStr(bid.params.cid)) {
utils.logError(`${BIDDER_CODE} : cid should be a string`);
return false;
}

return true;
},

/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(bidRequests) {
let payload = generatePayload(bidRequests);

return {
method: 'POST',
url: BID_URL,
data: JSON.stringify(payload)
};
},

/**
* 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, request) {
let validBids = [];

if (!serverResponse || !serverResponse.body) {
utils.logInfo(`${BIDDER_CODE} : response is empty`);
return validBids;
}

let bids = serverResponse.body.bidList;
if (!utils.isArray(bids) || bids.length === 0) {
utils.logInfo(`${BIDDER_CODE} : no bids`);
return validBids;
}
validBids = bids.filter(bid => isValidBid(bid));

return validBids;
},

getUserSyncs: function(syncOptions, serverResponses) {
let cookieSyncUrls = fetchCookieSyncUrls(serverResponses);

if (syncOptions.iframeEnabled) {
return filterUrlsByType(cookieSyncUrls, 'iframe');
}

if (syncOptions.pixelEnabled) {
return filterUrlsByType(cookieSyncUrls, 'image');
}
}
};
registerBidder(spec);
59 changes: 59 additions & 0 deletions modules/medianetBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Overview

```
Module Name: media.net Bid Adapter
Module Type: Bidder Adapter
Maintainer: vedant.s@media.net
```

# Description

Connects to Media.net's exchange for bids.
This adapter currently only supports Banner Ads.

# Sample Ad Unit: For Publishers
```javascript
var adUnits = [{
code: 'media.net-hb-ad-123456-1',
sizes: [
[300, 250],
[300, 600],
],
bids: [{
bidder: 'medianet',
params: {
cid: '<required-customerid-provided-by-media.net>',
bidfloor: '<optional-float>',
crid: '<optional-pleacementid-provided-by-media.net>'
}
}]
}];
```

# Ad Unit and Setup: For Testing

```html
<!-- Prebid Config section -->
<script>
var PREBID_TIMEOUT = 2000;
var adUnits = [{
code: 'media.net-hb-ad-123456-1',
sizes: [[300, 250]],
bids: [{
bidder: 'medianet',
params: {
cid: '8CUX0H51C',
// Site member is to be used only for testing
site: {
page: 'http://smoketesting.net/prebidtest/',
domain: 'smoketesting.net',
ref: 'http://smoketesting.net/prebidtest/'
}
}
}]
}];
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
</script>
<!-- End Prebid Config section -->
```
Loading