Skip to content

Commit

Permalink
TheMediaGrid Bid Adapter (#3204)
Browse files Browse the repository at this point in the history
* Added Grid Bid Adapter

* remove priceType from TheMediaGrid Bid Adapter
  • Loading branch information
TheMediaGrid authored and mike-chowla committed Oct 25, 2018
1 parent 5e27370 commit 2863df1
Show file tree
Hide file tree
Showing 3 changed files with 490 additions and 0 deletions.
151 changes: 151 additions & 0 deletions modules/gridBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import * as utils from 'src/utils';
import {registerBidder} from 'src/adapters/bidderFactory';
const BIDDER_CODE = 'grid';
const ENDPOINT_URL = '//grid.bidswitch.net/hb';
const TIME_TO_LIVE = 360;
const LOG_ERROR_MESS = {
noAuid: 'Bid from response has no auid parameter - ',
noAdm: 'Bid from response has no adm parameter - ',
noBid: 'Array of bid objects is empty',
noPlacementCode: 'Can\'t find in requested bids the bid with auid - ',
emptyUids: 'Uids should be not empty',
emptySeatbid: 'Seatbid array from response has empty item',
emptyResponse: 'Response is empty',
hasEmptySeatbidArray: 'Response has empty seatbid array',
hasNoArrayOfBids: 'Seatbid from response has no array of bid objects - '
};
export const spec = {
code: BIDDER_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.uid;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests - an array of bids
* @param {bidderRequest} bidderRequest bidder request object
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests, bidderRequest) {
const auids = [];
const bidsMap = {};
const bids = validBidRequests || [];
let reqId;

bids.forEach(bid => {
reqId = bid.bidderRequestId;
if (!bidsMap[bid.params.uid]) {
bidsMap[bid.params.uid] = [bid];
auids.push(bid.params.uid);
} else {
bidsMap[bid.params.uid].push(bid);
}
});

const payload = {
u: utils.getTopWindowUrl(),
auids: auids.join(','),
r: reqId
};

if (bidderRequest) {
if (bidderRequest.timeout) {
payload.wtimeout = bidderRequest.timeout;
}
if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.consentString) {
payload.gdpr_consent = bidderRequest.gdprConsent.consentString;
}
payload.gdpr_applies =
(typeof bidderRequest.gdprConsent.gdprApplies === 'boolean')
? Number(bidderRequest.gdprConsent.gdprApplies) : 1;
}
}

return {
method: 'GET',
url: ENDPOINT_URL,
data: utils.parseQueryStringParameters(payload).replace(/\&$/, ''),
bidsMap: bidsMap,
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @param {*} bidRequest
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, bidRequest) {
serverResponse = serverResponse && serverResponse.body;
const bidResponses = [];
const bidsMap = bidRequest.bidsMap;

let errorMessage;

if (!serverResponse) errorMessage = LOG_ERROR_MESS.emptyResponse;
else if (serverResponse.seatbid && !serverResponse.seatbid.length) {
errorMessage = LOG_ERROR_MESS.hasEmptySeatbidArray;
}

if (!errorMessage && serverResponse.seatbid) {
serverResponse.seatbid.forEach(respItem => {
_addBidResponse(_getBidFromResponse(respItem), bidsMap, bidResponses);
});
}
if (errorMessage) utils.logError(errorMessage);
return bidResponses;
}
}

function _getBidFromResponse(respItem) {
if (!respItem) {
utils.logError(LOG_ERROR_MESS.emptySeatbid);
} else if (!respItem.bid) {
utils.logError(LOG_ERROR_MESS.hasNoArrayOfBids + JSON.stringify(respItem));
} else if (!respItem.bid[0]) {
utils.logError(LOG_ERROR_MESS.noBid);
}
return respItem && respItem.bid && respItem.bid[0];
}

function _addBidResponse(serverBid, bidsMap, bidResponses) {
if (!serverBid) return;
let errorMessage;
if (!serverBid.auid) errorMessage = LOG_ERROR_MESS.noAuid + JSON.stringify(serverBid);
if (!serverBid.adm) errorMessage = LOG_ERROR_MESS.noAdm + JSON.stringify(serverBid);
else {
const awaitingBids = bidsMap[serverBid.auid];
if (awaitingBids) {
awaitingBids.forEach(bid => {
const bidResponse = {
requestId: bid.bidId, // bid.bidderRequestId,
bidderCode: spec.code,
cpm: serverBid.price,
width: serverBid.w,
height: serverBid.h,
creativeId: serverBid.auid, // bid.bidId,
currency: 'USD',
netRevenue: false,
ttl: TIME_TO_LIVE,
ad: serverBid.adm,
dealId: serverBid.dealid
};
bidResponses.push(bidResponse);
});
} else {
errorMessage = LOG_ERROR_MESS.noPlacementCode + serverBid.auid;
}
}
if (errorMessage) {
utils.logError(errorMessage);
}
}

registerBidder(spec);
40 changes: 40 additions & 0 deletions modules/gridBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Overview

Module Name: The Grid Media Bidder Adapter
Module Type: Bidder Adapter
Maintainer: grid-tech@themediagrid.com

# Description

Module that connects to Grid demand source to fetch bids.

# Test Parameters
```
var adUnits = [
{
code: 'test-div',
sizes: [[300, 250]],
bids: [
{
bidder: "grid",
params: {
uid: '1',
priceType: 'gross' // by default is 'net'
}
}
]
},{
code: 'test-div',
sizes: [[728, 90]],
bids: [
{
bidder: "grid",
params: {
uid: 2,
priceType: 'gross'
}
}
]
}
];
```
Loading

0 comments on commit 2863df1

Please sign in to comment.