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

Sovrn adapter - create no-bid responses for those placements where sovrn doesn't give a response. #163

Closed
wants to merge 6 commits into from
Closed
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
74 changes: 67 additions & 7 deletions src/adapters/criteo.js
Original file line number Diff line number Diff line change
@@ -46,26 +46,86 @@ var CriteoAdapter = function CriteoAdapter() {
var adResponse;
var content = window[varname];

var zoneNamesWithPositiveResponse = [];
if (content) {
zoneNamesWithPositiveResponse = content.split(';').filter(function(x){return x.length > 0});
}

var responseHasAds = false;
if (content && zoneNamesWithPositiveResponse.length > 0) {
responseHasAds = true;
}

// Add a response for each bid matching the "nid"
bids.forEach(function(existingBid) {
if (existingBid.params.nid === bid.params.nid) {
if (content) {
adResponse = bidfactory.createBid(1);
adResponse.bidderCode = 'criteo';

adResponse.keys = content.replace(/\;$/, '').split(';');
// If this bid object declares that it is linked to a specific zone, only add an adResponse if the zone got a positive response
var addedBidResponse = false;
if (existingBid.params.zone) {
var zoneObject = existingBid.params.zone;
if(utils.contains(zoneNamesWithPositiveResponse, zoneObject.name)) {
adResponse = bidfactory.createBid(1);
adResponse.bidderCode = 'criteo';
adResponse.keys = [zoneObject.name];
if(zoneObject.cpm != undefined){
adResponse.cpm = zoneObject.cpm;
}
if(zoneObject.id != undefined){
adResponse.ad = _getAdCodeHtmlForZoneIdAndClickUrl(zoneObject.id, zoneObject.click_url);
}
if(zoneObject.width){
adResponse.width = zoneObject.width;
}
if(zoneObject.height){
adResponse.height = zoneObject.height;
}
bidmanager.addBidResponse(existingBid.placementCode, adResponse);
addedBidResponse = true;
}
} else {
// Bid is not linked to a zone
if (responseHasAds) {
adResponse = bidfactory.createBid(1);
adResponse.bidderCode = 'criteo';

adResponse.keys = zoneNamesWithPositiveResponse;
bidmanager.addBidResponse(existingBid.placementCode, adResponse);
addedBidResponse = true;
}
}
if ( ! addedBidResponse) {
// Indicate an ad was not returned
adResponse = bidfactory.createBid(2);
adResponse.bidderCode = 'criteo';
bidmanager.addBidResponse(existingBid.placementCode, adResponse);
addedBidResponse = true;
}

bidmanager.addBidResponse(existingBid.placementCode, adResponse);
}
});

});
}

function _getAdCodeHtmlForZoneIdAndClickUrl(zone_id, click_url){
return "<script type='text/javascript'>\n"+
"<!--//<![CDATA[\n"+
"document.MAX_ct0 ='"+click_url+"';\n"+
"var m3_u = (location.protocol=='https:'?'https://cas.criteo.com/delivery/ajs.php?':'http://cas.criteo.com/delivery/ajs.php?');\n"+
"var m3_r = Math.floor(Math.random()*99999999999);\n"+
"document.write (\"<scr\"+\"ipt type='text/javascript' src='\"+m3_u);\n"+
"document.write (\"zoneid="+zone_id+"\");\n"+
"document.write ('&amp;cb=' + m3_r);\n"+
"if (document.MAX_used != ',') document.write (\"&amp;exclude=\" + document.MAX_used); document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ?\n"+
"'&amp;charset='+document.characterSet : ''));\n"+
"document.write (\"&amp;loc=\" + escape(window.location).substring(0,1600));\n"+
"if (document.context) document.write (\"&context=\" + escape(document.context));\n"+
"if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) {\n"+
"document.write (\"&amp;ct0=\" + escape(document.MAX_ct0)); }\n"+
"if (document.mmm_fo) document.write (\"&amp;mmm_fo=1\");\n"+
"document.write (\"'></scr\"+\"ipt>\");\n"+
"//]]>--></script>";
}

return {
callBids: _callBids
};
47 changes: 30 additions & 17 deletions src/adapters/sovrn.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ var bidfactory = require('../bidfactory.js');
var bidmanager = require('../bidmanager.js');
var adloader = require('../adloader');

var defaultPlacementForBadBid = '';
var allPlacementCodes;

/**
* Adapter for requesting bids from Sovrn
@@ -40,9 +40,7 @@ var SovrnAdapter = function SovrnAdapter() {
var page = window.location.pathname + location.search + location.hash;

var sovrnImps = [];
//assign the first adUnit (placement) for bad bids;
defaultPlacementForBadBid = bidReqs[0].placementCode;

allPlacementCodes = [];
//build impression array for sovrn
utils._each(bidReqs, function(bid)
{
@@ -74,6 +72,7 @@ var SovrnAdapter = function SovrnAdapter() {
};
sovrnImps.push(imp);
bidmanager.pbCallbackMap[imp.id] = bid;
allPlacementCodes.push(bid.placementCode);
});

// build bid request with impressions
@@ -91,24 +90,41 @@ var SovrnAdapter = function SovrnAdapter() {
adloader.loadScript(scriptUrl, null);
}

function addBlankBidResponsesForAllPlacementsExceptThese(placementsWithBidsBack){
utils._each(allPlacementCodes, function(placementCode)
{
if(utils.contains(placementsWithBidsBack, placementCode)) {
// A bid was returned for this placement already
} else {
// Add a no-bid response for this placement.
var bid = {};
bid = bidfactory.createBid(2);
bid.bidderCode = 'sovrn';
bidmanager.addBidResponse(placementCode, bid);
}
});
}


//expose the callback to the global object:
pbjs.sovrnResponse = function(sovrnResponseObj) {
var bid = {};
// valid object?
if (sovrnResponseObj && sovrnResponseObj.id) {
// valid object w/ bid responses?
if (sovrnResponseObj.seatbid && sovrnResponseObj.seatbid.length !==0 && sovrnResponseObj.seatbid[0].bid && sovrnResponseObj.seatbid[0].bid.length !== 0) {

var placementsWithBidsBack = [];
sovrnResponseObj.seatbid[0].bid.forEach(function(sovrnBid){

var responseCPM;
var placementCode = '';
var id = sovrnBid.impid;
var bid = {};

// try to fetch the bid request we sent Sovrn
var bidObj = bidmanager.getPlacementIdByCBIdentifer(id);
var bidObj = bidmanager.getPlacementIdByCBIdentifer(id);
if (bidObj){
placementCode = bidObj.placementCode;
placementsWithBidsBack.push(placementCode);
bidObj.status = CONSTANTS.STATUS.GOOD;

//place ad response on bidmanager._adResponsesByBidderId
@@ -145,32 +161,29 @@ var SovrnAdapter = function SovrnAdapter() {

bidmanager.addBidResponse(placementCode, bid);

} else {
} else {
//0 price bid
//indicate that there is no bid for this placement
bid = bidfactory.createBid(2);
bid.bidderCode = 'sovrn';
bidmanager.addBidResponse(placementCode, bid);

}
} else { // bid not found, we never asked for this?
} else { // bid not found, we never asked for this?
//no response data
bid = bidfactory.createBid(2);
bid.bidderCode = 'sovrn';
bidmanager.addBidResponse(placementCode, bid);
}
});
addBlankBidResponsesForAllPlacementsExceptThese(placementsWithBidsBack);
} else {
//no response data
bid = bidfactory.createBid(2);
bid.bidderCode = 'sovrn';
bidmanager.addBidResponse(defaultPlacementForBadBid, bid);
//no response data for any placements
addBlankBidResponsesForAllPlacementsExceptThese([]);
}
} else {
//no response data
bid = bidfactory.createBid(2);
bid.bidderCode = 'sovrn';
bidmanager.addBidResponse(defaultPlacementForBadBid, bid);
//no response data for any placements
addBlankBidResponsesForAllPlacementsExceptThese([]);
}

}; // sovrnResponse