Skip to content

Commit

Permalink
Prebid Server to Server (#1165)
Browse files Browse the repository at this point in the history
* s2s

* -update

* Revert "-update"

This reverts commit 1bcd3cb.

* fixed heigth and width

* updates

* Prebid server to server

* Add cookie sync

* Adding bid to bidsRequested

* Updated bid_id code returned by server

* update per URL coming back in response

* saving bid requests in adapter

* Unit tests for s2s adapter

* Handling no bid response and cookie sync url updates

* Code refactor, added new param to s2sConfig public api

* Added missing functions back and updated endpoint

* Added required param check and unit tests

* top url fix

* Remove clutter and bug fix

* Add cookie persist.

* Rename s2s to prebidServer.

* Add back unit tests.

* Update cookie persist.

* substitute for the `${AUCTION_PRICE}` macro

* address review notes

* Adding adapter back
  • Loading branch information
jaiminpanchal27 authored and Nate Cozi committed May 2, 2017
1 parent c17c483 commit 4470c08
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 9 deletions.
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"atomx",
"tapsense",
"trion",
"prebidServer",
"adsupply",
{
"appnexus": {
Expand Down
95 changes: 95 additions & 0 deletions integrationExamples/gpt/prebidServer_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<html>
<head>
<script>
var PREBID_TIMEOUT = 3000;

var googletag = googletag || {};
var sizes = [[728, 90],[300, 250], [300,600]];
googletag.cmd = googletag.cmd || [];

function initAdserver() {
if (pbjs.initAdserverSet) return;
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
pbjs.initAdserverSet = true;
};
setTimeout(initAdserver, PREBID_TIMEOUT);

var pbjs = pbjs || {};
pbjs.bidderTimeout = 3000;
pbjs.que = pbjs.que || [];
(function() {
var pbjsEl = document.createElement("script");
pbjsEl.type = "text/javascript";
pbjsEl.async = true;
pbjsEl.src = '/build/dev/prebid.js';
var pbjsTargetEl = document.getElementsByTagName("head")[0];
pbjsTargetEl.insertBefore(pbjsEl, pbjsTargetEl.firstChild);
})();

pbjs.que.push(function() {
var adUnits = [{
code: 'div-gpt-ad-1460505748561-0',
sizes: [[300, 250], [300,600]],
bids: [
{
bidder: 'appnexus',
params: {
placementId: '10433394'
}
}
]
}];

pbjs.setS2SConfig({
accountId : '1',
enabled : true, //default value set to false
bidders : ['appnexus'],
timeout : 1000, //default value is 1000
adapter : 'prebidServer', //if we have any other s2s adapter, default value is s2s
endpoint : 'https://prebid.adnxs.com/pbs/v1/auction?url_override=http%3A%2F%2Fwww.nytimes.com'
});

pbjs.addAdUnits(adUnits);

pbjs.requestBids({
bidsBackHandler: function(bidResponses) {
initAdserver();
}
})
});
</script>

<script>
googletag.cmd.push(function() {
var rightSlot = googletag.defineSlot('/19968336/header-bid-tag-0', [[300, 250], [300, 600]], 'div-gpt-ad-1460505748561-0').addService(googletag.pubads());

pbjs.que.push(function() {
pbjs.setTargetingForGPTAsync();
});

googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>

<body>
<h2>Prebid.js S2S Example</h2>

<h5>Div-1</h5>
<div id='div-gpt-ad-1460505748561-0'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1460505748561-0'); });
</script>
</div>
</body>
</html>
68 changes: 67 additions & 1 deletion src/adaptermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { BaseAdapter } from './adapters/baseAdapter';
var _bidderRegistry = {};
exports.bidderRegistry = _bidderRegistry;

//create s2s settings objectType_function
let _s2sConfig = {};
var _analyticsRegistry = {};
let _bidderSequence = null;

Expand All @@ -31,7 +33,7 @@ function getBids({bidderCode, requestId, bidderRequestId, adUnits}) {
mediaType: adUnit.mediaType,
transactionId : adUnit.transactionId,
sizes: sizes,
bidId: utils.getUniqueIdentifierStr(),
bidId: bid.bid_id || utils.getUniqueIdentifierStr(),
bidderRequestId,
requestId
});
Expand All @@ -55,6 +57,51 @@ exports.callBids = ({adUnits, cbTimeout}) => {
bidderCodes = shuffle(bidderCodes);
}

if(_s2sConfig.enabled) {
//these are called on the s2s adapter
let adaptersServerSide = _s2sConfig.bidders;

//don't call these client side
bidderCodes = bidderCodes.filter((elm) => {
return !adaptersServerSide.includes(elm);
});
let adUnitsCopy = utils.cloneJson(adUnits);

//filter out client side bids
adUnitsCopy.forEach((adUnit) => {
adUnit.sizes = transformHeightWidth(adUnit);
adUnit.bids = adUnit.bids.filter((bid) => {
return adaptersServerSide.includes(bid.bidder);
}).map((bid) => {
bid.bid_id = utils.getUniqueIdentifierStr();
return bid;
});
});

let tid = utils.generateUUID();
adaptersServerSide.forEach(bidderCode => {
const bidderRequestId = utils.getUniqueIdentifierStr();
const bidderRequest = {
bidderCode,
requestId,
bidderRequestId,
tid,
bids: getBids({bidderCode, requestId, bidderRequestId, 'adUnits' : adUnitsCopy}),
start: new Date().getTime(),
auctionStart: auctionStart,
timeout: _s2sConfig.timeout
};
//Pushing server side bidder
$$PREBID_GLOBAL$$._bidsRequested.push(bidderRequest);
});

let s2sBidRequest = {tid, 'ad_units' : adUnitsCopy};
let s2sAdapter = _bidderRegistry[_s2sConfig.adapter]; //jshint ignore:line
utils.logMessage(`CALLING S2S HEADER BIDDERS ==== ${adaptersServerSide.join(',')}`);
s2sAdapter.setConfig(_s2sConfig);
s2sAdapter.callBids(s2sBidRequest);
}

bidderCodes.forEach(bidderCode => {
const adapter = _bidderRegistry[bidderCode];
if (adapter) {
Expand All @@ -80,6 +127,21 @@ exports.callBids = ({adUnits, cbTimeout}) => {
});
};


function transformHeightWidth(adUnit) {
let sizesObj = [];
let sizes = utils.parseSizesInput(adUnit.sizes);
sizes.forEach(size => {
let heightWidth = size.split('x');
let sizeObj = {
'w' : parseInt(heightWidth[0]),
'h' : parseInt(heightWidth[1])
};
sizesObj.push(sizeObj);
});
return sizesObj;
}

exports.registerBidAdapter = function (bidAdaptor, bidderCode) {
if (bidAdaptor && bidderCode) {

Expand Down Expand Up @@ -158,6 +220,10 @@ exports.setBidderSequence = function (order) {
_bidderSequence = order;
};

exports.setS2SConfig = function (config) {
_s2sConfig = config;
};

/** INSERT ADAPTERS - DO NOT EDIT OR REMOVE */

/** END INSERT ADAPTERS */
Expand Down
121 changes: 121 additions & 0 deletions src/adapters/prebidServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Adapter from 'src/adapters/adapter';
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import { ajax } from 'src/ajax';
import { STATUS } from 'src/constants';
import { queueSync, persist } from 'src/cookie.js';

const TYPE = 's2s';
const cookiePersistMessage = `Your browser may be blocking 3rd party cookies. By clicking on this page you allow Prebid Server and other advertising partners to place cookies to help us advertise. You can opt out of their cookies <a href="https://www.appnexus.com/en/company/platform-privacy-policy#choices" target="_blank">here</a>.`;
const cookiePersistUrl = '//ib.adnxs.com/seg?add=1&redir=';
/**
* Bidder adapter for Prebid Server
*/
function PrebidServer() {

let baseAdapter = Adapter.createNew('prebidServer');
let bidRequests = [];
let config;

baseAdapter.setConfig = function(s2sconfig) {
config = s2sconfig;
};

/* Prebid executes this function when the page asks to send out bid requests */
baseAdapter.callBids = function(bidRequest) {

bidRequest.ad_units.forEach(adUnit => {
adUnit.bids.forEach(bidder => {
bidRequests[bidder.bidder] = utils.getBidRequest(bidder.bid_id);
});
});

let requestJson = {
account_id : config.accountId,
tid : bidRequest.tid,
max_bids: config.maxBids,
timeout_millis : config.timeout,
url: utils.getTopWindowUrl(),
prebid_version : '$prebid.version$',
ad_units : bidRequest.ad_units
};

const payload = JSON.stringify(requestJson);
ajax(config.endpoint, handleResponse, payload, {
contentType: 'text/plain',
withCredentials : true
});
};

/* Notify Prebid of bid responses so bids can get in the auction */
function handleResponse(response) {
let result;
try {
result = JSON.parse(response);

if(result.status === 'OK') {
if(result.bidder_status) {
result.bidder_status.forEach(bidder => {
if(bidder.no_bid || bidder.no_cookie) {
let bidRequest = bidRequests[bidder.bidder];
let bidObject = bidfactory.createBid(STATUS.NO_BID, bidRequest);
bidObject.bidderCode = bidRequest.bidder;
bidmanager.addBidResponse(bidRequest.placementCode, bidObject);
}
if(bidder.no_cookie) {
queueSync({bidder: bidder.bidder, url : bidder.usersync.url, type : bidder.usersync.type});
}
});
}
if(result.bids) {
result.bids.forEach(bidObj => {
let bidRequest = utils.getBidRequest(bidObj.bid_id);
let cpm = bidObj.price;
let status;
if (cpm !== 0) {
status = STATUS.GOOD;
} else {
status = STATUS.NO_BID;
}

let bidObject = bidfactory.createBid(status, bidRequest);
bidObject.creative_id = bidObj.creative_id;
bidObject.bidderCode = bidObj.bidder;
bidObject.cpm = cpm;
bidObject.ad = bidObj.adm;
bidObject.width = bidObj.width;
bidObject.height = bidObj.height;

bidmanager.addBidResponse(bidObj.code, bidObject);
});
}
}
else if (result.status === 'no_cookie') {
//cookie sync
persist(cookiePersistUrl, cookiePersistMessage);
}
} catch (error) {
utils.logError(error);
}

if (!result || result.status && result.status.includes('Error')) {
utils.logError('error parsing response: ', result.status);
}
}

return {
setConfig : baseAdapter.setConfig,
createNew: PrebidServer.createNew,
callBids: baseAdapter.callBids,
setBidderCode: baseAdapter.setBidderCode,
type : TYPE
};

}

PrebidServer.createNew = function() {
return new PrebidServer();
};

module.exports = PrebidServer;
5 changes: 4 additions & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@
"hb_pb",
"hb_size",
"hb_deal"
]
],
"S2S" : {
"DEFAULT_ENDPOINT" : "https://prebid.adnxs.com/pbs/v1/auction"
}
}
Loading

0 comments on commit 4470c08

Please sign in to comment.