Skip to content

Commit

Permalink
Merge pull request #29 from header-bidder/APPS-13502
Browse files Browse the repository at this point in the history
feat(openx adapter): APPS-13502: Allow passing custom key values spec…
  • Loading branch information
Harman Goei committed Sep 23, 2016
2 parents af42d9f + ec7a742 commit 575e445
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
223 changes: 223 additions & 0 deletions openxExamples/prebid_test_page_customparams.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>

<script>


var PREBID_TIMEOUT = 2500;

var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
// googletag.cmd.push(function () {
// googletag.pubads().disableInitialLoad();
// });

/* pbjs.initAdserver will be called either when all bids are back, or
when the timeout is reached.
*/
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;
}

// Load GPT when timeout is reached.
setTimeout(initAdserver, PREBID_TIMEOUT);

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];

// Load the Prebid Javascript Library Async. We recommend loading it immediately after
// the initAdserver() and setTimeout functions.
(function () {
var d = document;
var pbs = d.createElement("script");
pbs.type = "text/javascript";
pbs.src = '/build/dev/prebid.js';
var target = d.getElementsByTagName("head")[0];
target.insertBefore(pbs, target.firstChild);
})();

pbjs.que.push(function() {
pbjs.addAdUnits([{
code: 'div1',
sizes: [[728, 90]],
bids: [
{
bidder: 'openx',
params: {
unit: '1611012595',
delDomain: 'qa-v2-i35-presets.del-qa.openx.net',
customParams: {'key1': 'val1', 'key2': 'val2'}
}
}
]
},
{
code: 'div2',
sizes: [[300, 250]],
bids: [
{
bidder: 'openx',
params: {
unit: '1611012595',
delDomain: 'qa-v2-i35-presets.del-qa.openx.net'
}
}
]
},
{
code: 'div3',
sizes: [[160, 600]],
bids: [
{
bidder: 'openx',
params: {
unit: '1611012595',
delDomain: 'qa-v2-i35-presets.del-qa.openx.net',
customParams: {'key2': 'notval2'}
}
}
]
}]);

pbjs.requestBids({
bidsBackHandler: function() {
initAdserver();
}
});

function _getBucketFromCPM(cpm, bm) {
if (!bm) {
bm = 1;
}

var price,
pubRev = (cpm * 1000) / bm,
pubRevOrMaxTier = Math.min(pubRev, 10 * 2000);

// round round cpms < .025 up to .05
if (pubRevOrMaxTier > 0 && pubRevOrMaxTier < 25) {
pubRevOrMaxTier = 25;
}

if (pubRev < 1000) {// 5 cents to 1 dollar
price = Math.round(pubRevOrMaxTier / 50) * 5;
} else if (pubRev < 5000) {// 10 cents up to 5 dollar
price = Math.round(pubRevOrMaxTier / 100) * 10;
} else {// 50 cent up to max
price = Math.round(pubRevOrMaxTier / 500) * 50;
}
return price + "";
}


pbjs.bidderSettings = {
standard: {
adserverTargeting: [
{
key: "hb_bidder",
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function (bidResponse) {
return bidResponse.pbMg;
}
}
]
}
};


});

</script>

<script type='text/javascript'>
var slots = {};
googletag.cmd.push(function() {
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
console.log('Slot has been rendered:');
console.log(event);
});

slots['slot1'] = googletag.defineSlot('/90577858/prebid_auid', [[728, 90]], 'div1').addService(googletag.pubads());
slots['slot2'] = googletag.defineSlot('/90577858/prebid_auid', [[300, 250]], 'div2').addService(googletag.pubads());
slots['slot3'] = googletag.defineSlot('/90577858/prebid_auid', [[160, 600]], 'div3').addService(googletag.pubads());

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

googletag.pubads().enableSingleRequest();
googletag.enableServices();
});

function refresh(slots) {
pbjs.requestBids({
bidsBackHandler: function() {

googletag.cmd.push(function() {
pbjs.que.push(function() {
pbjs.setTargetingForGPTAsync();
});
googletag.pubads().refresh(slots);
});
}
});
}

</script>
</head>
<body>

<button onclick="refresh()">Slot 1 Refresh</button>

<h3>Slot 1</h3>
<div id='div1' style='width:728px; height:90px;'>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.display('div1');
});
</script>
</div>

<h3>Slot 2</h3>
<div id='div2' style='width:300px; height:250px;'>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.display('div2');
});
</script>
</div>

<h3>Slot 3</h3>
<div id='div3' style='width:160px; height:600px;'>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.display('div3');
});
</script>
</div>

</body>
</html>
8 changes: 8 additions & 0 deletions src/adapters/openx.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ var OpenxAdapter = function OpenxAdapter(options) {
return String(bid.sizes);
}).join('|');

bids.forEach(function (bid) {
for (let customParam in bid.params.customParams) {
if (bid.params.customParams.hasOwnProperty(customParam)) {
params["c." + customParam] = bid.params.customParams[customParam];
}
}
});

params.callback = 'window.$$PREBID_GLOBAL$$.oxARJResponse';
let queryString = buildQueryStringFromParams(params);

Expand Down
26 changes: 26 additions & 0 deletions test/spec/adapters/openx_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ describe('openx adapter tests', function () {
});

it('should call loadscript with the correct params', function () {
var spyLoadScript = sinon.spy(adloader, 'loadScript');
var params = {
bids: [
{
sizes: [[300, 250], [300, 600]],
params: {
delDomain: 'testdelDomain',
unit: 1234
}
}
]
};
adapter().callBids(params);

sinon.assert.calledOnce(spyLoadScript);

var bidUrl = spyLoadScript.getCall(0).args[0];
expect(bidUrl).to.include('testdelDomain');
expect(bidUrl).to.include('1234');
expect(bidUrl).to.include('300x250,300x600');
spyLoadScript.restore();
});

it('should send out custom params on bids that have customParams specified', function () {
var spyLoadScript = sinon.spy(adloader, 'loadScript');
var params = {
bids: [
Expand All @@ -151,6 +175,7 @@ describe('openx adapter tests', function () {
params: {
delDomain: 'testdelDomain',
unit: 1234,
customParams: {'test1': 'testval1'}
}
}
]
Expand All @@ -163,6 +188,7 @@ describe('openx adapter tests', function () {
expect(bidUrl).to.include('testdelDomain');
expect(bidUrl).to.include('1234');
expect(bidUrl).to.include('300x250,300x600');
expect(bidUrl).to.include('c.test1=testval1');
spyLoadScript.restore();
});

Expand Down

0 comments on commit 575e445

Please sign in to comment.