Skip to content

Commit

Permalink
Kumma adapter updated for Prebid 1.0 (#1766)
Browse files Browse the repository at this point in the history
* kumma adapter for prebid.js 1.0

* add prebid 1.0 example

* changing default ad + supporting bidfloor+function for top window referrer
  • Loading branch information
yehonatanshac authored and matthewlane committed Nov 16, 2017
1 parent 220e316 commit 049af72
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 209 deletions.
176 changes: 113 additions & 63 deletions modules/kummaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,113 @@
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader.js');
var utils = require('src/utils.js');
var CONSTANTS = require('src/constants.json');
var adaptermanager = require('src/adaptermanager');

var KummaAdapter = function KummaAdapter() {
function _callBids(params) {
var bidURL;
var bids = params.bids || [];
var requestURL = window.location.protocol + '//cdn.kumma.com/pb_ortb.js?cb=' + new Date().getTime() + '&ver=1&';

for (var i = 0; i < bids.length; i++) {
var requestParams = {};
var bid = bids[i];

requestParams.pub_id = bid.params.pubId;
requestParams.site_id = bid.params.siteId;
requestParams.placement_id = bid.placementCode;

var parseSized = utils.parseSizesInput(bid.sizes);
var arrSize = parseSized[0].split('x');

requestParams.width = arrSize[0];
requestParams.height = arrSize[1];
requestParams.callback = '$$PREBID_GLOBAL$$._doKummaCallback';
requestParams.callback_uid = bid.bidId;
bidURL = requestURL + utils.parseQueryStringParameters(requestParams);

utils.logMessage('Kumma.prebid, Bid ID: ' + bid.bidId + ', Pub ID: ' + bid.params.pubId);
adloader.loadScript(bidURL);
}
}

$$PREBID_GLOBAL$$._doKummaCallback = function (response) {
var bidObject;
var bidRequest;
var callbackID;
callbackID = response.callback_uid;
bidRequest = utils.getBidRequest(callbackID);
if (response.cpm > 0) {
bidObject = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidRequest);
bidObject.bidderCode = 'kumma';
bidObject.cpm = response.cpm;
bidObject.ad = response.tag;
bidObject.width = response.width;
bidObject.height = response.height;
} else {
bidObject = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidRequest);
bidObject.bidderCode = 'kumma';
utils.logMessage('No Bid response from Kumma request: ' + callbackID);
}
bidmanager.addBidResponse(bidRequest.placementCode, bidObject);
};

return {
callBids: _callBids
};
};
adaptermanager.registerBidAdapter(new KummaAdapter(), 'kumma');

module.exports = KummaAdapter;

import {logError, getTopWindowLocation, getTopWindowReferrer} from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';

export const spec = {

code: 'kumma',

isBidRequestValid: bid => (
!!(bid && bid.params && bid.params.pubId && bid.params.siteId)
),
buildRequests: bidRequests => {
const request = {
id: bidRequests[0].bidderRequestId,
at: 2,
imp: bidRequests.map(slot => impression(slot)),
site: site(bidRequests),
device: device(),
};
return {
method: 'POST',
url: '//hb.kumma.com/',
data: JSON.stringify(request),
};
},
interpretResponse: (response, request) => (
bidResponseAvailable(request, response.body)
),
};
function bidResponseAvailable(bidRequest, bidResponse) {
const idToImpMap = {};
const idToBidMap = {};
let ortbRequest = null;
try {
ortbRequest = JSON.parse(bidRequest.data);
} catch (ex) {
logError('kumma.parse', 'ERROR', ex);
}
ortbRequest.imp.forEach(imp => {
idToImpMap[imp.id] = imp;
});
if (bidResponse) {
bidResponse.seatbid.forEach(seatBid => seatBid.bid.forEach(bid => {
idToBidMap[bid.impid] = bid;
}));
}
const bids = [];
Object.keys(idToImpMap).forEach(id => {
if (idToBidMap[id]) {
const bid = {};
bid.requestId = id;
bid.creativeId = idToBidMap[id].adid;
bid.cpm = idToBidMap[id].price;
bid.currency = bidResponse.cur;
bid.ttl = 360;
bid.netRevenue = true;
bid.ad = idToBidMap[id].adm;
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_IMP_ID(%7D|\})/gi, idToBidMap[id].impid);
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_AD_ID(%7D|\})/gi, idToBidMap[id].adid);
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_PRICE(%7D|\})/gi, idToBidMap[id].price);
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_CURRENCY(%7D|\})/gi, bidResponse.cur);
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_BID_ID(%7D|\})/gi, bidResponse.bidid);
bid.width = idToImpMap[id].banner.w;
bid.height = idToImpMap[id].banner.h;
bids.push(bid);
}
});
return bids;
}
function impression(slot) {
return {
id: slot.bidId,
banner: banner(slot),
bidfloor: slot.params.bidFloor || '0.000001',
tagid: slot.params.placementId.toString(),
};
}
function banner(slot) {
const size = slot.params.size.toUpperCase().split('X');
const width = parseInt(size[0]);
const height = parseInt(size[1]);
return {
w: width,
h: height,
};
}
function site(bidderRequest) {
const pubId = bidderRequest && bidderRequest.length > 0 ? bidderRequest[0].params.pubId : '0';
const siteId = bidderRequest && bidderRequest.length > 0 ? bidderRequest[0].params.siteId : '0';
const appParams = bidderRequest[0].params.app;
if (!appParams) {
return {
publisher: {
id: pubId.toString(),
domain: getTopWindowLocation().hostname,
},
id: siteId.toString(),
ref: getTopWindowReferrer(),
page: getTopWindowLocation().href,
}
}
return null;
}
function device() {
return {
ua: navigator.userAgent,
language: (navigator.language || navigator.browserLanguage || navigator.userLanguage || navigator.systemLanguage),
w: (window.screen.width || window.innerWidth),
h: (window.screen.height || window.innerHeigh),
};
}

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

**Module Name**: Kumma Bidder Adapter
**Module Type**: Bidder Adapter
**Maintainer**: yehonatan@kumma.com

# Description

Connects to Kumma demand source to fetch bids.
Please use ```kumma``` as the bidder code.

# Test Parameters
```
var adUnits = [{
code: 'banner-ad-div',
sizes: [[300, 250]],
bids: [{
bidder: 'kumma',
params: {
pubId: '55879', // required
siteId: '26047', // required
size: '300X250', // required
placementId: '123',
bidFloor: '0.001'
}
}]
}];
```
Loading

0 comments on commit 049af72

Please sign in to comment.