Skip to content

Commit

Permalink
Merge pull request #5 from PubMatic/OW_PB_2
Browse files Browse the repository at this point in the history
Merging ow_pb_2 branch
  • Loading branch information
pm-harshad-mane authored Aug 31, 2017
2 parents 8291a04 + 098659e commit 71fda65
Show file tree
Hide file tree
Showing 20 changed files with 1,162 additions and 367 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ gulp.task('lint', () => {
// Task to build minified version of owt.js
gulp.task('bundle', function () {
console.log("Executing build");
return gulp.src(['prebid-header.js', prebidRepoPath + '/build/dist/prebid.js','./build/dist/owt.js'])
return gulp.src(['prebid-header.js', prebidRepoPath + '/build/dist/prebid.js','prebid-footer.js','./build/dist/owt.js'])
.pipe(concat('owt.min.js'))
.pipe(gulp.dest('build'));
});
Expand All @@ -155,7 +155,7 @@ gulp.task('bundle', function () {
// Task to build non-minified version of owt.js
gulp.task('devbundle', function () {
console.log("Executing Dev Build");
return gulp.src(['prebid-header.js', prebidRepoPath + '/build/dev/prebid.js', './build/dev/owt.js'])
return gulp.src(['prebid-header.js', prebidRepoPath + '/build/dev/prebid.js', 'prebid-footer.js', './build/dev/owt.js'])
.pipe(concat('owt.js'))
.pipe(gulp.dest('build'));
});
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
"test/globals.js",
'test/**/*.spec.js',
'test/helpers/karma-init.js'
],
Expand Down
1 change: 1 addition & 0 deletions prebid-footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
}
3 changes: 2 additions & 1 deletion prebid-header.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
var pbjs = pbjs || {}; pbjs.que = pbjs.que || [];
function pwtCreatePrebidNamespace(preBidNameSpace) {
window[preBidNameSpace] = window[preBidNameSpace] || {}; window[preBidNameSpace].que = window[preBidNameSpace].que || [];
102 changes: 73 additions & 29 deletions src_new/adapters/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
Note:
Whenever we support a new PB adapter, we need to check if it needs actual sizes to be passed,
if so we will need to add special handling
TODO:
PubMatic special handliing
We are not doing mandatory param check as PB does it
does PB logs if mandatory param is missing
PreBid does not do mandatory parameters checking
*/
var CONFIG = require("../config.js");
var CONSTANTS = require("../constants.js");
Expand All @@ -18,6 +14,9 @@ var CONF = require("../conf.js");

var parentAdapterID = CONSTANTS.COMMON.PARENT_ADAPTER_PREBID;

var pbNameSpace = "pbjs";
var pbNameSpaceCounter = 0;

/* start-test-block */
exports.parentAdapterID = parentAdapterID;
/* end-test-block */
Expand All @@ -29,6 +28,44 @@ exports.kgpvMap = kgpvMap;

var refThis = this;

function transformPBBidToOWBid(bid, kgpv){
var theBid = BID.createBid(bid.bidderCode, kgpv);
theBid.setGrossEcpm(bid.cpm);
theBid.setDealID(bid.dealId);
theBid.setDealChannel(bid.dealChannel);
theBid.setAdHtml(bid.ad || "");
theBid.setAdUrl(bid.adUrl || "");
theBid.setWidth(bid.width);
theBid.setHeight(bid.height);
theBid.setReceivedTime(bid.responseTimestamp);

util.forEachOnObject(bid.adserverTargeting, function(key, value){
theBid.setKeyValuePair(key, value);
});
return theBid;
}

/* start-test-block */
exports.transformPBBidToOWBid = transformPBBidToOWBid;
/* end-test-block */

function pbBidStreamHandler(pbBid){
var responseID = pbBid.adUnitCode || "";
/* istanbul ignore else */
if(util.isOwnProperty(refThis.kgpvMap, responseID)){
/* istanbul ignore else */
if(pbBid.bidderCode){
bidManager.setBidFromBidder(
refThis.kgpvMap[responseID].divID,
refThis.transformPBBidToOWBid(pbBid, refThis.kgpvMap[responseID].kgpv)
);
}
}
}

/* start-test-block */
exports.pbBidStreamHandler = pbBidStreamHandler;
/* end-test-block */

function handleBidResponses(bidResponses){
for(var responseID in bidResponses){
Expand All @@ -40,22 +77,8 @@ function handleBidResponses(bidResponses){
for(var i = 0; i<bids.length; i++){
var bid = bids[i];
/* istanbul ignore else */
if(bid.bidderCode){

var theBid = BID.createBid(bid.bidderCode, refThis.kgpvMap[responseID].kgpv);
theBid.setGrossEcpm(bid.cpm);
theBid.setDealID(bid.dealId);
theBid.setDealChannel(bid.dealChannel);
theBid.setAdHtml(bid.ad || "");
theBid.setAdUrl(bid.adUrl || "");
theBid.setWidth(bid.width);
theBid.setHeight(bid.height);
theBid.setReceivedTime(bid.responseTimestamp);

util.forEachOnObject(bid.adserverTargeting, function(key, value){
theBid.setKeyValuePair(key, value);
});
bidManager.setBidFromBidder(refThis.kgpvMap[responseID].divID, theBid);
if(bid.bidderCode){
bidManager.setBidFromBidder(refThis.kgpvMap[responseID].divID, transformPBBidToOWBid(bid, refThis.kgpvMap[responseID].kgpv));
}
}
}
Expand Down Expand Up @@ -133,6 +156,7 @@ exports.generatedKeyCallback = generatedKeyCallback;
function generatePbConf(adapterID, adapterConfig, activeSlots, adUnits, impressionID){
util.log(adapterID+CONSTANTS.MESSAGES.M1);

/* istanbul ignore else */
if(!adapterConfig){
return;
}
Expand All @@ -156,11 +180,26 @@ exports.generatePbConf = generatePbConf;
/* end-test-block */

function fetchBids(activeSlots, impressionID){
if(! window.pbjs){ // todo: move this code to initial state of adhooks

var newPBNameSpace = pbNameSpace + pbNameSpaceCounter++;
window.pwtCreatePrebidNamespace(newPBNameSpace);

/* istanbul ignore else */
if(! window[newPBNameSpace]){ // todo: move this code to initial state of adhooks
util.log("PreBid js is not loaded");
return;
}


if(util.isFunction(window[newPBNameSpace].onEvent)){
window[newPBNameSpace].onEvent('bidResponse', refThis.pbBidStreamHandler);
} else {
util.log("PreBid js onEvent method is not available");
return;
}

window[newPBNameSpace].logging = util.isDebugLogEnabled();

var adUnits = {};// create ad-units for prebid
var randomNumberBelow100 = adapterManager.getRandomNumberBelow100();
CONFIG.forEachAdapter(function(adapterID, adapterConfig){
Expand Down Expand Up @@ -188,23 +227,28 @@ function fetchBids(activeSlots, impressionID){
}

/* istanbul ignore else */
if(adUnitsArray.length > 0 && window.pbjs){
if(adUnitsArray.length > 0 && window[newPBNameSpace]){

try{
/* istanbul ignore else */
if(util.isFunction(window.pbjs.setBidderSequence)){
window.pbjs.setBidderSequence("random");
if(util.isFunction(window[newPBNameSpace].setBidderSequence)){
window[newPBNameSpace].setBidderSequence("random");
}
/* istanbul ignore else */
if(util.isFunction(window.pbjs.requestBids)){
window.pbjs.logging = false;//todo: enable optionally
window.pbjs.requestBids({
if(util.isFunction(window[newPBNameSpace].requestBids)){
window[newPBNameSpace].requestBids({
adUnits: adUnitsArray,
// Note: Though we are not doing anything in the bidsBackHandler, it is required by PreBid
bidsBackHandler: function(bidResponses) {
refThis.handleBidResponses(bidResponses);
util.log("In PreBid bidsBackHandler with bidResponses: ");
util.log(bidResponses);
//refThis.handleBidResponses(bidResponses);
},
timeout: CONFIG.getTimeout()-50 //todo is it higher ?: major pre and post processing time and then
});
} else {
util.log("PreBid js requestBids function is not available");
return;
}
} catch (e) {
util.log('Error occured in calling PreBid.');
Expand Down
49 changes: 29 additions & 20 deletions src_new/bidManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var bmEntry = require("./bmEntry.js");
var refThis = this;

function createBidEntry(divID){ // TDD, i/o : done
/* istanbul ignore else */
if(! util.isOwnProperty(window.PWT.bidMap, divID) ){
window.PWT.bidMap[divID] = bmEntry.createBMEntry(divID);
}
Expand All @@ -30,7 +31,7 @@ exports.setBidFromBidder = function(divID, bidDetails){ // TDD done
var bidderID = bidDetails.getAdapterID();
var bidID = bidDetails.getBidID();
var bidMapEntry = window.PWT.bidMap[divID];

/* istanbul ignore else */
if(!util.isOwnProperty(window.PWT.bidMap, divID)){
util.log("BidManager is not expecting bid for "+ divID +", from " + bidderID);
return;
Expand All @@ -42,7 +43,7 @@ exports.setBidFromBidder = function(divID, bidDetails){ // TDD done
refThis.createBidEntry(divID);

util.log("BdManagerSetBid: divID: "+divID+", bidderID: "+bidderID+", ecpm: "+bidDetails.getGrossEcpm() + ", size: " + bidDetails.getWidth()+"x"+bidDetails.getHeight() + ", postTimeout: "+isPostTimeout);

/* istanbul ignore else */
if(isPostTimeout === true){
bidDetails.setPostTimeoutStatus();
}
Expand All @@ -55,7 +56,7 @@ exports.setBidFromBidder = function(divID, bidDetails){ // TDD done
;

if( lastBidWasDefaultBid || !isPostTimeout){

/* istanbul ignore else */
if(lastBidWasDefaultBid){
util.log(CONSTANTS.MESSAGES.M23);
}
Expand Down Expand Up @@ -104,7 +105,7 @@ exports.resetBid = function(divID, impressionID){ // TDD, i/o : done
window.PWT.bidMap[divID].setImpressionID(impressionID);
};

function auctionBids(bmEntry) {
function auctionBids(bmEntry) { // TDD, i/o : done
var winningBid = null,
keyValuePairs = {};

Expand All @@ -122,9 +123,8 @@ function auctionBids(bmEntry) {



function auctionBidsCallBack(adapterID, adapterEntry, keyValuePairs, winningBid) {
function auctionBidsCallBack(adapterID, adapterEntry, keyValuePairs, winningBid) { // TDD, i/o : done
if (adapterEntry.getLastBidID() != "") {
// console.log("Comming here and adapterEntry.bids ==>", adapterEntry.bids);
util.forEachOnObject(adapterEntry.bids, function(bidID, theBid) {
// do not consider post-timeout bids
/* istanbul ignore else */
Expand Down Expand Up @@ -168,11 +168,11 @@ exports.auctionBidsCallBack = auctionBidsCallBack;
exports.auctionBids = auctionBids;
/* end-test-block */

exports.getBid = function(divID){ // TDD done
exports.getBid = function(divID){ // TDD, i/o : done

var winningBid = null;
var keyValuePairs = null;

/* istanbul ignore else */
if( util.isOwnProperty(window.PWT.bidMap, divID) ){
var data = refThis.auctionBids(window.PWT.bidMap[divID]);
winningBid = data.wb;
Expand All @@ -197,8 +197,8 @@ exports.getBid = function(divID){ // TDD done
return {wb: winningBid, kvp: keyValuePairs};
};

exports.getBidById = function(bidID) { // TDD done

exports.getBidById = function(bidID) { // TDD, i/o : done
/* istanbul ignore else */
if (!util.isOwnProperty(window.PWT.bidIdMap, bidID)) {
util.log(CONSTANTS.MESSAGES.M25 + bidID);
return null;
Expand All @@ -207,11 +207,11 @@ exports.getBidById = function(bidID) { // TDD done
var divID = window.PWT.bidIdMap[bidID].s;
var adapterID = window.PWT.bidIdMap[bidID].a;


/* istanbul ignore else */
if (util.isOwnProperty(window.PWT.bidMap, divID)) {
util.log("BidID: " + bidID + ", DivID: " + divID + CONSTANTS.MESSAGES.M19 + adapterID);
var theBid = window.PWT.bidMap[divID].getBid(adapterID, bidID);

/* istanbul ignore else */
if (theBid == null) {
return null;
}
Expand All @@ -227,8 +227,9 @@ exports.getBidById = function(bidID) { // TDD done
};


exports.displayCreative = function(theDocument, bidID){ // TDD done
exports.displayCreative = function(theDocument, bidID){ // TDD, i/o : done
var bidDetails = refThis.getBidById(bidID);
/* istanbul ignore else */
if(bidDetails){
var theBid = bidDetails.bid,
divID = bidDetails.slotid
Expand All @@ -239,14 +240,14 @@ exports.displayCreative = function(theDocument, bidID){ // TDD done
}
};

exports.executeAnalyticsPixel = function(){
exports.executeAnalyticsPixel = function(){ // TDD, i/o : done
var outputObj = {
s: []
},
pixelURL = CONFIG.getAnalyticsPixelURL(),
impressionIDMap = {} // impID => slots[]
;

/* istanbul ignore else */
if(!pixelURL){
return;
}
Expand All @@ -264,7 +265,7 @@ exports.executeAnalyticsPixel = function(){
refThis.analyticalPixelCallback(slotID, bmEntry, impressionIDMap);
});

util.forEachOnObject(impressionIDMap, function(impressionID, slots){
util.forEachOnObject(impressionIDMap, function(impressionID, slots){ /* istanbul ignore next */
/* istanbul ignore else */
if(slots.length > 0){
outputObj.s = slots;
Expand All @@ -274,9 +275,10 @@ exports.executeAnalyticsPixel = function(){
});
};

exports.executeMonetizationPixel = function(slotID, theBid){ // TDD done
exports.executeMonetizationPixel = function(slotID, theBid){ // TDD, i/o : done
var pixelURL = CONFIG.getMonetizationPixelURL();

/* istanbul ignore else */
if(!pixelURL){
return;
}
Expand All @@ -297,8 +299,9 @@ exports.executeMonetizationPixel = function(slotID, theBid){ // TDD done
refThis.setImageSrcToPixelURL(pixelURL);
};

function analyticalPixelCallback(slotID, bmEntry, impressionIDMap) {
function analyticalPixelCallback(slotID, bmEntry, impressionIDMap) { // TDD, i/o : done
var startTime = bmEntry.getCreationTime();
/* istanbul ignore else */
if (bmEntry.getAnalyticEnabledStatus() && !bmEntry.getExpiredStatus()) {
var slotObject = {
"sn": slotID,
Expand All @@ -317,7 +320,13 @@ function analyticalPixelCallback(slotID, bmEntry, impressionIDMap) {
}

util.forEachOnObject(adapterEntry.bids, function(bidID, theBid) {


if(CONFIG.getAdapterMaskBidsStatus(adapterID) == 1){
if(theBid.getWinningBidStatus() === false){
return;
}
}

var endTime = theBid.getReceivedTime();
//todo: take all these key names from constants
slotObject["ps"].push({
Expand Down Expand Up @@ -348,7 +357,7 @@ exports.analyticalPixelCallback = analyticalPixelCallback;



exports.setImageSrcToPixelURL = function (pixelURL) { // TDD done
exports.setImageSrcToPixelURL = function (pixelURL) { // TDD, i/o : done
var img = new window.Image();
img.src = util.metaInfo.protocol + pixelURL;
};
Loading

0 comments on commit 71fda65

Please sign in to comment.