Skip to content

Commit

Permalink
Add Callback for Bids all back per slot (issue) #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mkendall07 committed Aug 19, 2015
1 parent 581dc50 commit cfecbe5
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 29 deletions.
107 changes: 96 additions & 11 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var utils = require('./utils.js');
var objectType_function = 'function';
var objectType_undefined = 'undefined';

var externalCallbackByAdUnitArr = [];
var externalCallbackArr = [];
var biddersByPlacementMap = {};

var pbCallbackMap = {};
exports.pbCallbackMap = pbCallbackMap;

Expand Down Expand Up @@ -73,6 +77,7 @@ exports.addBidResponse = function(adUnitCode, bid) {
//increment the bid count
bidResponseRecievedCount++;
//get price settings here

if (bid.getStatusCode() === 2) {
bid.cpm = 0;
}
Expand Down Expand Up @@ -125,16 +130,12 @@ exports.addBidResponse = function(adUnitCode, bid) {
bidResponseObj = pbBidResponseByPlacement[adUnitCode];
//bidResponseObj.status = statusCode;
bidResponseObj.bids.push(bid);
//increment bid response by placement
bidResponseObj.bidsReceivedCount++;

} else {
//create an empty bid bid response object
bidResponseObj = {
//status: statusPending,
bids: [],
allBidsAvailable: false
};
//bidResponseObj.status = statusBidsAvail;
bidResponseObj.bids.push(bid);
//should never reach this code
utils.logError('Internal error');
}


Expand All @@ -146,15 +147,16 @@ exports.addBidResponse = function(adUnitCode, bid) {
//store the bidResponse in a map
pbBidResponseByPlacement[adUnitCode] = bidResponseObj;

this.checkIfAllBidsAreIn();
this.checkIfAllBidsAreIn(adUnitCode);

//TODO: check if all bids are in
};

exports.createEmptyBidResponseObj = function() {
return {
bids: [],
allBidsAvailable: false
allBidsAvailable: false,
bidsReceivedCount : 0
};
};

Expand Down Expand Up @@ -235,6 +237,7 @@ exports.registerDefaultBidderSetting = function(bidderCode, defaultSetting) {

exports.executeCallback = function() {

//this pbjs.registerBidCallbackHandler will be deprecated soon
if (typeof pbjs.registerBidCallbackHandler === objectType_function && !_callbackExecuted) {
try {
pbjs.registerBidCallbackHandler();
Expand All @@ -244,24 +247,106 @@ exports.executeCallback = function() {
utils.logError('Exception trying to execute callback handler registered : ' + e.message);
}
}

//trigger allBidsBack handler
//todo: get args
if(externalCallbackArr.called !== true){
var params = [];
processCallbacks(externalCallbackArr, params);
externalCallbackArr.called = true;
}



};

exports.allBidsBack = function() {
return _allBidsAvailable;
};

function triggerAdUnitCallbacks(adUnitCode){
//todo : get bid responses and send in args
var params = [adUnitCode];
processCallbacks(externalCallbackByAdUnitArr, params);
}

function processCallbacks(callbackQueue, params){
var i;
for(i = 0; i < callbackQueue.length; i++){
var func = callbackQueue[i];
if(typeof func === 'function'){
try{
func.apply(pbjs, params);
//func.executed = true;
}
catch(e){
utils.logError('Error executing callback function: ' + e.message);
}
}

}

}

function checkBidsBackByAdUnit(adUnitCode){
for(var i = 0; i < pbjs.adUnits.length; i++){
var adUnit = pbjs.adUnits[i];
if(adUnit.code === adUnitCode){
var bidsBack = pbBidResponseByPlacement[adUnitCode].bidsReceivedCount;
console.log('bids back :' + bidsBack);
//all bids back for ad unit
if(bidsBack === adUnit.bids.length){
triggerAdUnitCallbacks(adUnitCode);

}
}
}
/*
utils.mapForEach(biddersByPlacementMap, function(value, key){
console.log('key: '+ key);
console.log(value);
if(key === adUnitCode){
var val = pbBidResponseByPlacement[adUnitCode];
alert('we have a winner: ' + val.bidsReceivedCount );
}
});
*/
}

exports.setBidderMap = function(bidderMap){
biddersByPlacementMap = bidderMap;
};

/*
* This method checks if all bids have a response (bid, no bid, timeout) and will execute callback method if all bids are in
* TODO: Need to track bids by placement as well
*/

exports.checkIfAllBidsAreIn = function() {
exports.checkIfAllBidsAreIn = function(adUnitCode) {
if (bidRequestCount !== 0 && bidRequestCount === bidResponseRecievedCount) {
_allBidsAvailable = true;
}

//check by ad units
checkBidsBackByAdUnit(adUnitCode);


if (_allBidsAvailable) {
//execute our calback method if it exists && pbjs.initAdserverSet !== true
this.executeCallback();

}
};

exports.addCallback = function(id, callback, cbEvent){
callback['id'] = id;
if(CONSTANTS.CB.TYPE.ALL_BIDS_BACK === cbEvent){
externalCallbackArr.push(callback);
}
else if(CONSTANTS.CB.TYPE.AD_UNIT_BIDS_BACK === cbEvent){
externalCallbackByAdUnitArr.push(callback);
}


};
6 changes: 6 additions & 0 deletions src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
"STATUS": {
"GOOD": "good",
"TIMEOUT": "timed out"
},
"CB" : {
"TYPE" : {
"ALL_BIDS_BACK" : "allBidsBack",
"AD_UNIT_BIDS_BACK" : "adUnitBidsBack"
}
}
}
68 changes: 50 additions & 18 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function init(adUnitCode) {
utils.logMessage('No adUnits configured. No bids requested.');
return;
}

//set timeout for all bids
setTimeout(bidmanager.executeCallback, pbjs.bidderTimeout);
//parse settings into internal vars
Expand Down Expand Up @@ -150,6 +149,8 @@ function loadPreBidders() {
for (i = 0; i < pb_preBidders.length; i++) {
pb_preBidders[i].loadPreBid();
}
//send a reference to bidmanager
bidmanager.setBidderMap(pb_bidderMap);
}

function storeBidRequestByBidder(placementCode, sizes, bids) {
Expand Down Expand Up @@ -282,20 +283,7 @@ function getBidResponsesByAdUnit(adunitCode) {
}
}

/*
* This function returns a "cleaned up" version of the bid response targeting paramasters in JSON form
*/
pbjs.getAdserverTargetingParamsForAdUnit = function(adunitCode) {
// call to populate pb_targetingMap
pbjs.getBidResponses(adunitCode);

if (adunitCode) {
return pb_targetingMap[adunitCode];
}
return pb_targetingMap;


};
/*
* Copies bids into a bidArray response
*/
Expand Down Expand Up @@ -349,6 +337,33 @@ function getCloneBid(bid) {
return bidClone;
}

function resetBids() {
bidmanager.clearAllBidResponses();
pb_bidderMap = {};
}


//////////////////////////////////
// //
// Start Public APIs //
// //
//////////////////////////////////

/*
* This function returns a "cleaned up" version of the bid response targeting paramasters in JSON form
*/
pbjs.getAdserverTargetingParamsForAdUnit = function(adunitCode) {
// call to populate pb_targetingMap
pbjs.getBidResponses(adunitCode);

if (adunitCode) {
return pb_targetingMap[adunitCode];
}
return pb_targetingMap;


};

/*
* This function returns a "cleaned up" version of the bid response in JSON form
*/
Expand Down Expand Up @@ -473,10 +488,6 @@ pbjs.renderAd = function(doc, params) {

};

function resetBids() {
bidmanager.clearAllBidResponses();
pb_bidderMap = {};
}

/*
* This function will refresh the bid requests for all adUnits or for specified adUnitCode
Expand Down Expand Up @@ -540,6 +551,27 @@ pbjs.addAdUnit = function(adUnitObj) {
}
};


/**
* @function
* Add a callback event
* @param {String} event event to attach callback to.
* @param {Function} func function to execute
* @returns {String} id for callback
*/
pbjs.addCallback = function(eventStr, func){
var id = null;
if(!eventStr || !func || typeof func !== objectType_function){
console.logError('error registering callback. Check method signature');
return id;
}


id = utils.getUniqueIdentifierStr;
bidmanager.addCallback(id, func, eventStr);
return id;
};

// Register the bid adaptors here
registerBidAdapter(RubiconAdapter(), 'rubicon');
registerBidAdapter(AppNexusAdapter(), 'appnexus');
Expand Down

0 comments on commit cfecbe5

Please sign in to comment.