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

Pollux Adapter #1431

Merged
merged 10 commits into from
Aug 15, 2017
105 changes: 105 additions & 0 deletions integrationExamples/pollux/zone_728x90.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this example page to integrationExamples/gpt/pollux_zone_728x90.html

<html>
<head>
<!-- Prebid Config Section START -->
<!-- Make sure this is inserted before your GPT tag -->
<script>
var PREBID_TIMEOUT = 3000;

var adUnits = [{
code: 'div-gpt-ad-1460505661639-0',
sizes: [[728, 90]],
bids: [
{
bidder: 'appnexus',
params: {
placementId: '4799418'
}
},
{
bidder: 'pollux',
bidderUrl: '//adn.plxnt.com/prebid',
params: {
zone: '276'
}
}
]
}];

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
</script>
<!-- Prebid Config Section END -->

<!-- Prebid Boilerplate Section START. No Need to Edit. -->
<script type="text/javascript" src="//cdn-assetsx.plxnt.com/assets/public/js/prebid.js" async></script>
<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function() {
googletag.pubads().disableInitialLoad();
});

pbjs.que.push(function() {
pbjs.addAdUnits(adUnits);
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest
});
});

function sendAdserverRequest() {
if (pbjs.adserverRequestSent) return;
pbjs.adserverRequestSent = true;
googletag.cmd.push(function() {
pbjs.que.push(function() {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh();
});
});
};

setTimeout(function() {
sendAdserverRequest();
}, PREBID_TIMEOUT);

</script>
<!-- Prebid Boilerplate Section END -->

<script>
(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);
})();
</script>

<script>
googletag.cmd.push(function () {
googletag.defineSlot('/19968336/header-bid-tag1', [[728, 90]], 'div-gpt-ad-1460505661639-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>

<style>
body {
margin: 0;
padding: 0;
}
</style>
<title>test</title>
</head>

<body>
<div id='div-gpt-ad-1460505661639-0'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1460505661639-0'); });
</script>
</div>
</body>
</html>
95 changes: 95 additions & 0 deletions modules/polluxBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import adloader from 'src/adloader';
import adaptermanager from 'src/adaptermanager';
import { STATUS } from 'src/constants';

// Prebid adapter for Pollux header bidding client
function polluxBidAdapter() {
function _callBids(params) {
var bids, bidderUrl = (window.location.protocol) + '//adn.plxnt.com/prebid';
bids = params.bids || [];
for (var i = 0; i < bids.length; i++) {
var request_obj = {};
var bid = bids[i];
// check params
if (bid.params.zone) {
var domain = utils.getParameterByName('domain');
var tracker2 = utils.getParameterByName('tracker2');
if (domain) {
request_obj.domain = domain;
} else {
request_obj.domain = window.location.host;
}
if (tracker2) {
request_obj.tracker2 = tracker2;
}
request_obj.zone = bid.params.zone;
} else {
utils.logError('required param "zone" is missing', 'polluxHandler');
continue;
}
var parsedSizes = utils.parseSizesInput(bid.sizes);
var parsedSizesLength = parsedSizes.length;
if (parsedSizesLength > 0) {
// first value should be "size"
request_obj.size = parsedSizes[0];
if (parsedSizesLength > 1) {
// any subsequent values should be "promo_sizes"
var promo_sizes = [];
for (var j = 1; j < parsedSizesLength; j++) {
promo_sizes.push(parsedSizes[j]);
}
request_obj.promo_sizes = promo_sizes.join(',');
}
}
// detect urls
request_obj.callback_id = bid.bidId;
// set a different url bidder
if (bid.bidderUrl) {
bidderUrl = bid.bidderUrl;
}
var prebidUrl = bidderUrl + '?' + utils.parseQueryStringParameters(request_obj);
utils.logMessage('Pollux request built: ' + prebidUrl);
adloader.loadScript(prebidUrl, null, true);
}
}

// expose the callback to global object
$$PREBID_GLOBAL$$.polluxHandler = function (response) {
// pollux handler
var bidObject = {};
var callback_id = response.callback_id;
var placementCode = '';
var bidObj = utils.getBidRequest(callback_id);
if (bidObj) {
placementCode = bidObj.placementCode;
}
if (bidObj && response.cpm > 0 && !!response.ad) {
bidObject = bidfactory.createBid(STATUS.GOOD);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass bidRequest as second parameter. This will match request response pairs

bidObject.bidderCode = bidObj.bidder;
bidObject.mediaType = response.mediaType;
bidObject.cpm = parseFloat(response.cpm);
if (response.ad_type === 'url') {
bidObject.adUrl = response.ad;
} else {
bidObject.ad = response.ad;
}
bidObject.width = response.width;
bidObject.height = response.height;
} else {
bidObject = bidfactory.createBid(STATUS.NO_BID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass bidRequest as second parameter. This will match request response pairs

bidObject.bidderCode = 'pollux';
utils.logMessage('No prebid response from polluxHandler for placement code ' + placementCode);
}
bidmanager.addBidResponse(placementCode, bidObject);
};
// Export the `callBids` function, so that Prebid.js can execute
// this function when the page asks to send out bid requests.
return {
callBids: _callBids
};
};
adaptermanager.registerBidAdapter(new polluxBidAdapter(), 'pollux');
module.exports = polluxBidAdapter;
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ var getParameterByName = function (name) {

return decodeURIComponent(results[1].replace(/\+/g, ' '));
};
exports.getParameterByName = getParameterByName;

/**
* This function validates paramaters.
Expand Down
39 changes: 39 additions & 0 deletions test/spec/modules/polluxBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {expect} from 'chai';
import Adapter from '../../../modules/polluxBidAdapter';
import bidManager from '../../../src/bidmanager';
import adLoader from '../../../src/adloader';

describe('Pollux Adapter', () => {
let adapter;

const REQUEST = {
'bidderCode': 'pollux',
'requestId': 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6',
'bidderRequestId': '7101db09af0db2',
'bids': [
{
'bidder': 'pollux',
'bidderUrl': '//adn-dev.polluxnetwork.com/prebid',
'params': {
'zone': '123'
},
'code': 'div-gpt-ad-1460505661639-0',
'sizes': [[728, 90]],
'bidId': '84ab500420319d',
'bidderRequestId': '7101db09af0db2',
'requestId': 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6'
}
],
'start': 1469479810130
};

sinon.stub(bidManager, 'addBidResponse');
const adLoaderStub = sinon.stub(adLoader, 'loadScript');

describe('callBids', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more test cases. Your test coverage seems just 54.1 %. Make it atleast >85
To view test coverage run 1) gulp test-coverage 2) gulp view-coverage

adapter = new Adapter();
adapter.callBids(REQUEST);
expect(adLoaderStub.getCall(0).args[0]).to.contain('zone=123');
expect(adLoaderStub.getCall(0).args[0]).to.contain('domain=');
});
});