Skip to content

Commit

Permalink
AdSupply adapter (#1162)
Browse files Browse the repository at this point in the history
* Added adsupply adapter

* Added client Id

* Fixed code review comments
  • Loading branch information
msadsupply authored and jaiminpanchal27 committed May 2, 2017
1 parent 441455d commit 4be50ea
Show file tree
Hide file tree
Showing 3 changed files with 446 additions and 0 deletions.
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"atomx",
"tapsense",
"trion",
"adsupply",
{
"appnexus": {
"alias": "brealtime"
Expand Down
86 changes: 86 additions & 0 deletions src/adapters/adsupply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var bidfactory = require('../bidfactory.js');
var bidmanager = require('../bidmanager.js');
var adloader = require('../adloader');
var utils = require('../utils');
const ADSUPPLY_CODE = 'adsupply';

var AdSupplyAdapter = function AdSupplyAdapter() {
function _validateParams(params) {
if (!params || !params.siteId || !params.zoneId || !params.endpointUrl || !params.clientId) {
return false;
}

if (typeof params.zoneId !== 'number' || params.zoneId <= 0) {
return false;
}

return true;
}

function _getRequestUrl(bid) {
var referrerUrl = encodeURIComponent(window.document.referrer);
var rand = encodeURIComponent(Math.floor(Math.random() * 100000 + 1));
var time = encodeURIComponent(new Date().getTimezoneOffset());
return '//' + bid.params.endpointUrl + '/banner.engine?id=' + bid.params.siteId + '&z=' + bid.params.zoneId + '&rand=' + rand + '&ver=async' + '&time=' + time + '&referrerurl=' + referrerUrl + '&abr=false' + '&hbt=1&cid=' + encodeURIComponent(bid.params.clientId);
}

$$PREBID_GLOBAL$$.adSupplyResponseHandler = function (bidId) {
if (!bidId) return;

var bidRequest = utils.getBidRequest(bidId);

if (!bidRequest || !bidRequest.params) return;

var clientId = bidRequest.params.clientId;
var zoneProp = 'b' + bidRequest.params.zoneId;

if (!window[clientId] || !window[clientId][zoneProp]) return;

var media = window[clientId][zoneProp].Media;

if (!media) return;

if (!media.Url || !media.Ecpm || typeof media.Ecpm !== 'number' || media.Ecpm <= 0) {
var noFillbject = bidfactory.createBid(2, bidRequest);
noFillbject.bidderCode = ADSUPPLY_CODE;
bidmanager.addBidResponse(bidRequest.placementCode, noFillbject);
} else {
var bidObject = bidfactory.createBid(1, bidRequest);
bidObject.bidderCode = ADSUPPLY_CODE;
bidObject.cpm = media.Ecpm;
bidObject.ad = '<iframe style="z-index: 5000001; margin: 0px; padding: 0px; border: none; width: ' + media.Width + 'px; height: ' + media.Height + 'px; " src="//' + bidRequest.params.endpointUrl + media.Url + '"></iframe>';
bidObject.width = media.Width;
bidObject.height = media.Height;
bidmanager.addBidResponse(bidRequest.placementCode, bidObject);
}
};

function _makeCallBackHandler(bidId) {
return function () {
$$PREBID_GLOBAL$$.adSupplyResponseHandler(bidId);
};
}

function _callBids(params) {
var bids = params.bids || [];
for (var i = 0; i < bids.length; i++) {
var bid = bids[i];
if (!_validateParams(bid.params)) continue;

var clientId = bid.params.clientId;
var zoneProp = 'b' + bid.params.zoneId;
window[clientId] = window[clientId] || {};
window.window[clientId][zoneProp] = window.window[clientId][zoneProp] || {};
window.window[clientId][zoneProp].Media = {};

var requestUrl = _getRequestUrl(bid);
adloader.loadScript(requestUrl, _makeCallBackHandler(bid.bidId));
}
}

return {
callBids: _callBids
};
};

module.exports = AdSupplyAdapter;
Loading

0 comments on commit 4be50ea

Please sign in to comment.