Skip to content

Commit

Permalink
Yieldbot adunit bidder params slot name usage fix (prebid#1394)
Browse files Browse the repository at this point in the history
* Yieldbot adunit bidder params slot name usage fix

* Yieldbot adunit bidder params slot name usage fix, PR feedback++
  • Loading branch information
elljoh authored and philipwatson committed Sep 18, 2017
1 parent c51bbdc commit 5db373f
Show file tree
Hide file tree
Showing 3 changed files with 605 additions and 115 deletions.
201 changes: 201 additions & 0 deletions integrationExamples/gpt/pbjs_yieldbot_gpt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<html>
<head>
<script>
var PREBID_TIMEOUT = 1000;

var googletag = googletag || {};
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.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-1438287399331-0',
sizes: [[728, 90]],
bids: [
{
bidder: 'appnexus',
params: { placementId: '10433394' }
},
{
bidder: 'yieldbot',
params: {
psn: '1234',
slot: 'leaderboard'
}
}
]
},
{
code: 'div-gpt-ad-1438287399331-1',
sizes: [[300, 250], [300, 600]],
bids: [
{
bidder: 'appnexus',
params: { placementId: '10433394' }
},
{
bidder: 'yieldbot',
params: {
psn: '1234',
slot: 'test_slot'
}
}
]
},
{
code: 'div-gpt-ad-1438287399331-2',
sizes: [[300, 250]],
bids: [
{
bidder: 'appnexus',
params: { placementId: '10433394' }
},
{
bidder: 'yieldbot',
params: {
psn: '1234',
slot: 'test_slot'
}
}
]
}
];
pbjs.addAdUnits(adUnits);

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

<script>
var topSlot;
var rightSlot;
var leftSlot;

googletag.cmd.push(function() {
topSlot = googletag.defineSlot('/2476204/leaderboard', [[728, 90]], 'div-gpt-ad-1438287399331-0').addService(googletag.pubads());

rightSlot = googletag.defineSlot('/2476204/multi-size', [[300, 250], [300, 600]], 'div-gpt-ad-1438287399331-1').addService(googletag.pubads());

leftSlot = googletag.defineSlot('/2476204/multi-size', [[300, 250], [300,600]], 'div-gpt-ad-1438287399331-2').addService(googletag.pubads());

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

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

function refreshBid0() {
pbjs.que.push(function() {
pbjs.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: ['div-gpt-ad-1438287399331-0'],
bidsBackHandler: function() {
pbjs.setTargetingForGPTAsync(['div-gpt-ad-1438287399331-0']);
googletag.pubads().refresh([topSlot]);
}
});
});
}

function refreshBid1() {
pbjs.que.push(function() {
pbjs.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: ['div-gpt-ad-1438287399331-1'],
bidsBackHandler: function() {
pbjs.setTargetingForGPTAsync(['div-gpt-ad-1438287399331-1']);
googletag.pubads().refresh([rightSlot]);
}
});
});
}

function refreshBid2() {
pbjs.que.push(function() {
pbjs.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: ['div-gpt-ad-1438287399331-2'],
bidsBackHandler: function() {
pbjs.setTargetingForGPTAsync(['div-gpt-ad-1438287399331-2']);
googletag.pubads().refresh([leftSlot]);
}
});
});
}
</script>
</head>

<body>
<h2>Prebid.js Yieldbot Adapter Basic Example</h2>
Use the links below to enable and disable Yieldbot test bids.<br>
<br>
<b><em>Note:</em></b>
<br>
The "<b><a href="#enable" target="_self">Enable - Yieldbot Test Bids</a></b>" link below will set a cookie to force Yieldbot bid requests to return static test creative: the cookie expires in 24 hrs.
<br>
<ul><li>Use the "<b><a href="#enable" target="_self">Disable - Yieldbot Test Bids</a></b>" link when testing is complete to re-enable live bids.</li></ul>
<ol>
<li><a id="enable" href="http://i.yldbt.com/m/start-testing" target="_blank">Enable - Yieldbot Test Bids</a></li>
<li><a id="disable" href="http://i.yldbt.com/m/stop-testing" target="_blank">Disable - Yieldbot Test Bids</a></li>
</ol>
<h5>Div-0, 728x90</h5>
<button onclick="refreshBid0()">Refresh 728x90 Ad Unit</button>
<div id='div-gpt-ad-1438287399331-0'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1438287399331-0'); });
</script>
</div>
<h5>Div-1, 300x250 or 300x600</h5>
<button onclick="refreshBid1()">Refresh 300x250, 300x600 Ad Unit</button>
<div id='div-gpt-ad-1438287399331-1'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1438287399331-1'); });
</script>
</div>
<h5>Div-2, 300x250 or 300x600</h5>
The bid for the <code>300x250 | 300x600</code> slot is shown under <b><em>Div-1</em></b> above.
<ul>
<li>Refresh this slot after initial page view and you should see the Yieldbot test creative.</li>
</ul>
<button onclick="refreshBid2()">Refresh 300x250, 300x600 Ad Unit</button>
<div id='div-gpt-ad-1438287399331-2'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1438287399331-2'); });
</script>
</div>
</body>
</html>
97 changes: 70 additions & 27 deletions modules/yieldbotBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function YieldbotAdapter() {
AVAILABLE: 1,
EMPTY: 2
},
definedSlots: [],
pageLevelOption: false,
/**
* Builds the Yieldbot creative tag.
Expand Down Expand Up @@ -71,6 +70,29 @@ function YieldbotAdapter() {
bid.bidderCode = 'yieldbot';
return bid;
},
/**
* Unique'ify slot sizes for a Yieldbot bid request<br>
* Bids may refer to a slot and dimension multiple times on a page, but should exist once in the request.
* @param {Array} sizes An array of sizes to deduplicate
* @private
*/
getUniqueSlotSizes: function(sizes) {
var newSizes = [];
var hasSize = {};
if (utils.isArray(sizes)) {
for (var idx = 0; idx < sizes.length; idx++) {
var bidSize = sizes[idx] || '';
if (bidSize && utils.isStr(bidSize) && !hasSize[bidSize]) {
var nSize = bidSize.split('x');
if (nSize.length > 1) {
newSizes.push([nSize[0], nSize[1]]);
}
hasSize[bidSize] = true;
}
}
}
return newSizes;
},
/**
* Yieldbot implementation of {@link module:adaptermanger.callBids}
* @param {Object} params - Adapter bid configuration object
Expand All @@ -84,8 +106,9 @@ function YieldbotAdapter() {

ybotq.push(function () {
var yieldbot = window.yieldbot;
// Empty defined slots bidId array
ybotlib.definedSlots = [];
// Empty defined slot bids object
ybotlib.bids = {};
ybotlib.parsedBidSizes = {};
// Iterate through bids to obtain Yieldbot slot config
// - Slot config can be different between initial and refresh requests
var psn = 'ERROR_PREBID_DEFINE_YB_PSN';
Expand All @@ -94,23 +117,33 @@ function YieldbotAdapter() {
var bid = v;
// bidder params config: http://prebid.org/dev-docs/bidders/yieldbot.html
// - last psn wins
psn = (bid.params && bid.params.psn) || psn;
var slotName = (bid.params && bid.params.slot) || 'ERROR_PREBID_DEFINE_YB_SLOT';

slots[slotName] = bid.sizes || [];
ybotlib.definedSlots.push(bid.bidId);
psn = bid.params && bid.params.psn ? bid.params.psn : psn;
var slotName = bid.params && bid.params.slot ? bid.params.slot : 'ERROR_PREBID_DEFINE_YB_SLOT';
var parsedSizes = utils.parseSizesInput(bid.sizes) || [];
slots[slotName] = slots[slotName] || [];
slots[slotName] = slots[slotName].concat(parsedSizes);
ybotlib.bids[bid.bidId] = bid;
ybotlib.parsedBidSizes[bid.bidId] = parsedSizes;
});

for (var bidSlots in slots) {
if (slots.hasOwnProperty(bidSlots)) {
// The same slot name and size may be used for multiple bids. Get unique sizes
// for the request.
slots[bidSlots] = ybotlib.getUniqueSlotSizes(slots[bidSlots]);
}
}

if (yieldbot._initialized !== true) {
yieldbot.pub(psn);
for (var slotName in slots) {
if (slots.hasOwnProperty(slotName)) {
yieldbot.defineSlot(slotName, { sizes: slots[slotName] || [] });
yieldbot.defineSlot(slotName, { sizes: slots[slotName] });
}
}
yieldbot.enableAsync();
yieldbot.go();
} else {
} else if (!utils.isEmpty(slots)) {
yieldbot.nextPageview(slots);
}
});
Expand All @@ -128,27 +161,37 @@ function YieldbotAdapter() {
*/
handleUpdateState: function () {
var yieldbot = window.yieldbot;
utils._each(ybotlib.definedSlots, function (v) {
var ybRequest;
var adapterConfig;

ybRequest = $$PREBID_GLOBAL$$._bidsRequested
.find(bidderRequest => bidderRequest.bidderCode === 'yieldbot');

adapterConfig = ybRequest && ybRequest.bids ? ybRequest.bids.find(bid => bid.bidId === v) : null;

if (adapterConfig && adapterConfig.params && adapterConfig.params.slot) {
var placementCode = adapterConfig.placementCode || 'ERROR_YB_NO_PLACEMENT';
var criteria = yieldbot.getSlotCriteria(adapterConfig.params.slot);
var bid = ybotlib.buildBid(criteria);

bidmanager.addBidResponse(placementCode, bid);
var slotUsed = {};

for (var bidId in ybotlib.bids) {
if (ybotlib.bids.hasOwnProperty(bidId)) {
var bidRequest = ybotlib.bids[bidId] || null;

if (bidRequest && bidRequest.params && bidRequest.params.slot) {
var placementCode = bidRequest.placementCode || 'ERROR_YB_NO_PLACEMENT';
var criteria = yieldbot.getSlotCriteria(bidRequest.params.slot);
var requestedSizes = ybotlib.parsedBidSizes[bidId] || [];

var slotSizeOk = false;
for (var idx = 0; idx < requestedSizes.length; idx++) {
var requestedSize = requestedSizes[idx];

if (!slotUsed[criteria.ybot_slot] && requestedSize === criteria.ybot_size) {
slotSizeOk = true;
slotUsed[criteria.ybot_slot] = true;
break;
}
}
var bid = ybotlib.buildBid(slotSizeOk ? criteria : { ybot_ad: 'n' });
bidmanager.addBidResponse(placementCode, bid);
}
}
});
}
}
};
return {
callBids: ybotlib.callBids
callBids: ybotlib.callBids,
getUniqueSlotSizes: ybotlib.getUniqueSlotSizes
};
}

Expand Down
Loading

0 comments on commit 5db373f

Please sign in to comment.