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

Add Piximedia adapter #595

Merged
merged 1 commit into from
Sep 7, 2016
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
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"indexExchange",
"kruxlink",
"openx",
"piximedia",
"pubmatic",
"pulsepoint",
"rubicon",
Expand Down
157 changes: 157 additions & 0 deletions src/adapters/piximedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
var CONSTANTS = require('../constants.json');
var utils = require('../utils.js');
var bidmanager = require('../bidmanager.js');
var bidfactory = require('../bidfactory.js');
var adloader = require('../adloader.js');
var Adapter = require('./adapter.js');

var PiximediaAdapter = function PiximediaAdapter() {
var PREBID_URL = '//static.adserver.pm/prebid';
var baseAdapter = Adapter.createNew('piximedia');
var bidStash = {};

var tryAppendPixiQueryString = function(url, name, value) {
return url + "/" + encodeURIComponent(name) + "=" + value;
};

baseAdapter.callBids = function callBidsPiximedia(params) {
utils._each(params.bids, function(bid) {

// valid bids must include a siteId and an placementId
if (bid.placementCode && bid.sizes && bid.params && bid.params.siteId && bid.params.placementId) {

var sizes = bid.params.hasOwnProperty('sizes')? bid.params.sizes: bid.sizes;
sizes = utils.parseSizesInput(sizes);

var cbid = utils.getUniqueIdentifierStr();

// we allow overriding the URL in the params
var url = bid.params.prebidUrl || PREBID_URL;

// params are passed to the Piximedia endpoint, including custom params
for(var key in bid.params) {
/* istanbul ignore else */
if(bid.params.hasOwnProperty(key)) {
var value = bid.params[key];
switch(key) {
case "siteId":
url = tryAppendPixiQueryString(url, 'site_id', encodeURIComponent(value));
break;

case "placementId":
url = tryAppendPixiQueryString(url, 'placement_id', encodeURIComponent(value));
break;

case "dealId":
url = tryAppendPixiQueryString(url, 'l_id', encodeURIComponent(value));
break;

case "sizes":
case "prebidUrl":
break;

default:
if(typeof value === "function") {
url = tryAppendPixiQueryString(url, key, encodeURIComponent((value(baseAdapter, params, bid) || "").toString()));
} else {
url = tryAppendPixiQueryString(url, key, encodeURIComponent((value || "").toString()));
}
break;
}
}
}

url = tryAppendPixiQueryString(url, 'jsonp', '$$PREBID_GLOBAL$$.handlePiximediaCallback');
url = tryAppendPixiQueryString(url, 'sizes', encodeURIComponent(sizes.join(",")));
url = tryAppendPixiQueryString(url, 'cbid', encodeURIComponent(cbid));
url = tryAppendPixiQueryString(url, 'rand', String(Math.floor(Math.random() * 1000000000)));

bidStash[cbid] = {
'bidObj': bid,
'url': url,
'start': new Date().getTime()
};
utils.logMessage('[Piximedia] Dispatching header Piximedia to URL ' + url);
adloader.loadScript(url);
}
});
};

/*
* Piximedia's bidResponse should look like:
*
* {
* 'foundbypm': true, // a Boolean, indicating if an ad was found
* 'currency': 'EUR', // the currency, as a String
* 'cpm': 1.99, // the win price as a Number, in currency
* 'dealId': null, // or string value of winning deal ID
* 'width': 300, // width in pixels of winning ad
* 'height': 250, // height in pixels of winning ad
* 'html': 'tag_here' // HTML tag to load if we are picked
* }
*
*/
$$PREBID_GLOBAL$$.handlePiximediaCallback = function(bidResponse) {
if (bidResponse && bidResponse.hasOwnProperty("foundbypm")) {
var stash = bidStash[bidResponse.cbid]; // retrieve our stashed data, by using the cbid
var bid;

if (stash) {
var bidObj = stash.bidObj;
var timelapsed = new Date().getTime();
timelapsed = timelapsed - stash.start;

if (bidResponse.foundbypm && bidResponse.width && bidResponse.height && bidResponse.html && bidResponse.cpm && bidResponse.currency) {

/* we have a valid ad to display */
bid = bidfactory.createBid(CONSTANTS.STATUS.GOOD);
bid.bidderCode = bidObj.bidder;
bid.width = bidResponse.width;
bid.height = bidResponse.height;
bid.ad = bidResponse.html;
bid.cpm = bidResponse.cpm;
bid.currency = bidResponse.currency;

if (bidResponse.dealId) {
bid.dealId = bidResponse.dealId;
} else {
bid.dealId = null;
}

bidmanager.addBidResponse(bidObj.placementCode, bid);

utils.logMessage('[Piximedia] Registered bidresponse from URL ' + stash.url +
' (time: ' + String(timelapsed) + ')');
utils.logMessage('[Piximedia] ======> BID placementCode: ' + bidObj.placementCode +
' CPM: ' + String(bid.cpm) + ' ' + bid.currency +
' Format: ' + String(bid.width) + 'x' + String(bid.height));
} else {

/* we have no ads to display */
bid = bidfactory.createBid(CONSTANTS.STATUS.NO_BID);
bid.bidderCode = bidObj.bidder;
bidmanager.addBidResponse(bidObj.placementCode, bid);

utils.logMessage('[Piximedia] Registered BLANK bidresponse from URL ' + stash.url +
' (time: ' + String(timelapsed) + ')');
utils.logMessage('[Piximedia] ======> NOBID placementCode: ' + bidObj.placementCode);
}

// We should no longer need this stashed object, so drop reference:
bidStash[bidResponse.cbid] = null;

} else {
utils.logMessage("[Piximedia] Couldn't find stash for cbid=" + bidResponse.cbid);
}
}
};

// return an object with PiximediaAdapter methods
return {
callBids: baseAdapter.callBids,
setBidderCode: baseAdapter.setBidderCode,
getBidderCode: baseAdapter.getBidderCode
};
};

module.exports = PiximediaAdapter;
Loading