Skip to content

Commit

Permalink
Merge pull request prebid#11 from PubMatic-OpenWrap/S2S-code-review-c…
Browse files Browse the repository at this point in the history
…hanges

S2 s code review changes
  • Loading branch information
PubMatic-OpenWrap authored Jan 3, 2018
2 parents dc78c43 + df32243 commit c221899
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 72 deletions.
13 changes: 7 additions & 6 deletions modules.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[
"appnexusBidAdapter",
"appnexusBidAdapter",
"sekindoUMBidAdapter",
"pulsepointBidAdapter",
"audienceNetworkBidAdapter",
"openxBidAdapter",
"pulsepointBidAdapter",
"audienceNetworkBidAdapter",
"openxBidAdapter",
"rubiconBidAdapter",
"sovrnBidAdapter",
"pubmaticBidAdapter"
]
"pubmaticBidAdapter",
"pubmaticServerBidAdapter"
]
128 changes: 66 additions & 62 deletions modules/pubmaticServerBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { registerBidder } from 'src/adapters/bidderFactory';
const constants = require('src/constants.json');

const BIDDER_CODE = 'pubmaticServer';
const ENDPOINT = '//hb.pubmatic.com/openrtb/241/?';
// Var USYNCURL is not used, hence commenting it for eslinting
// const USYNCURL = '//ads.pubmatic.com/AdServer/js/showad.js#PIX&kdntuid=1&p=';
const ENDPOINT = '//ow.pubmatic.com/openrtb/241/?';
const CURRENCY = 'USD';
const AUCTION_TYPE = 2;
const AUCTION_TYPE = 1; // PubMaticServer just picking highest bidding bid from the partners configured
const UNDEFINED = undefined;
const IFRAME = 'iframe';
const IMAGE = 'image';
const REDIRECT = 'redirect';
const DEFAULT_VERSION_ID = '0';

const CUSTOM_PARAMS = {
'kadpageurl': '', // Custom page url
'gender': '', // User gender
Expand All @@ -19,18 +20,10 @@ const CUSTOM_PARAMS = {
'lon': '', // User Location - Longitude
'wiid': '', // OpenWrap Wrapper Impression ID
'profId': '', // OpenWrap Legacy: Profile ID
'verId': '' // OpenWrap Legacy: version ID
'verId': '', // OpenWrap Legacy: version ID
'divId': '' // OpenWrap new
};

// not used, hence commenting it for eslinting
let publisherId = 0;

function _getDomainFromURL(url) {
let anchor = document.createElement('a');
anchor.href = url;
return anchor.hostname;
}

function logNonStringParam(paramName, paramValue) {
utils.logWarn('PubMaticServer: Ignoring param : ' + paramName + ' with value : ' + paramValue + ', expects string-value, found ' + typeof paramValue);
}
Expand Down Expand Up @@ -81,7 +74,7 @@ function _handleCustomParams(params, conf) {
// istanbul ignore else
if (value) {
entry = CUSTOM_PARAMS[key];
if (typeof entry === 'object') {
if (utils.isA(entry, 'Object')) {
// will be used in future when we want to process a custom param before using
// 'keyname': {f: function() {}}
value = entry.f(value, conf);
Expand Down Expand Up @@ -132,7 +125,7 @@ function _createImpressionObject(bid, conf) {
pos: 0,
topframe: utils.inIframe() ? 0 : 1,
format: (function() {
var arr = [];
let arr = [];
for (let i = 0, l = bid.sizes.length; i < l; i++) {
arr.push({
w: bid.sizes[i][0],
Expand All @@ -145,15 +138,14 @@ function _createImpressionObject(bid, conf) {
ext: {
pmZoneId: _parseSlotParam('pmzoneid', bid.params.pmzoneid),
div: bid.params.divId,
adunit: bid.params.adUnitId,
slotIndex: bid.params.adUnitIndex
adunit: bid.params.adUnitId
}
};
}

function mandatoryParamCheck(paramName, paramValue) {
if (!utils.isStr(paramValue)) {
utils.logWarn('PubMaticServer: ' + paramName + ' is mandatory and it should be a string, , found ' + typeof paramValue);
utils.logWarn(BIDDER_CODE + ': ' + paramName + ' is mandatory and it should be a string, , found ' + typeof paramValue);
return false;
}
return true;
Expand Down Expand Up @@ -185,39 +177,43 @@ export const spec = {
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: validBidRequests => {
var conf = _initConf();
var payload = _createOrtbTemplate(conf);
let conf = _initConf();
let payload = _createOrtbTemplate(conf);

if (utils.isEmpty(validBidRequests)) {
utils.logWarn('No Valid Bid Request found for given adUnits');
return;
}

validBidRequests.forEach(bid => {
conf.pubId = conf.pubId || bid.params.publisherId;
conf = _handleCustomParams(bid.params, conf);
conf.transactionId = bid.transactionId;
payload.imp.push(_createImpressionObject(bid, conf));
});

if (payload.imp.length == 0) {
return;
}

payload.site.publisher.id = conf.pubId.trim();
publisherId = conf.pubId;
payload.ext.dm = {
rs: 1,
pubId: conf.pubId,
wp: 'pbjs',
wv: constants.REPO_AND_VERSION,
transactionId: conf.transactionId,
profileid: conf.profId || UNDEFINED,
versionid: conf.verId || '1',
versionid: conf.verId || DEFAULT_VERSION_ID,
wiid: conf.wiid || UNDEFINED
};
payload.user.gender = _parseSlotParam('gender', conf.gender);
payload.user.yob = _parseSlotParam('yob', conf.yob);
payload.user.geo = {};
payload.user.geo.lat = _parseSlotParam('lat', conf.lat);
payload.user.geo.lon = _parseSlotParam('lon', conf.lon);
payload.user = {
gender: _parseSlotParam('gender', conf.gender),
yob: _parseSlotParam('yob', conf.yob),
geo: {
lat: _parseSlotParam('lat', conf.lat),
lon: _parseSlotParam('lon', conf.lon)
}
};
payload.device.geo = payload.user.geo;
payload.site.page = conf.kadpageurl || payload.site.page;
payload.site.domain = _getDomainFromURL(payload.site.page);
payload.site.domain = utils.getTopWindowHostName();
return {
method: 'POST',
url: ENDPOINT,
Expand All @@ -234,32 +230,37 @@ export const spec = {
interpretResponse: (response, request) => {
const bidResponses = [];
try {
if (response.body && response.body.seatbid && response.body.seatbid[0] && response.body.seatbid[0].bid) {
response.body.seatbid[0].bid.forEach(bid => {
if (bid.id !== null && bid.ext.summary) {
bid.ext.summary.forEach((summary, index) => {
if (summary.bidder) {
const firstSummary = index === 0;
const newBid = {
requestId: bid.impid,
bidderCode: BIDDER_CODE,
originalBidder: summary.bidder,
pubmaticServerErrorCode: summary.errorCode,
cpm: (parseFloat(summary.bid) || 0).toFixed(2),
width: summary.width,
height: summary.height,
creativeId: firstSummary ? (bid.crid || bid.id) : bid.id,
dealId: firstSummary ? (bid.dealid || UNDEFINED) : UNDEFINED,
currency: CURRENCY,
netRevenue: true,
ttl: 300,
referrer: utils.getTopWindowUrl(),
ad: firstSummary ? bid.adm : ''
};
bidResponses.push(newBid);
}
});
}
if (response.body && response.body.seatbid) {
// Supporting multiple bid responses for same adSize
const referrer = utils.getTopWindowUrl();
response.body.seatbid.forEach(seatbidder => {
seatbidder.bid &&
seatbidder.bid.forEach(bid => {
if (bid.id !== null && bid.ext.summary) {
bid.ext.summary.forEach((summary, index) => {
if (summary.bidder) {
const firstSummary = index === 0;
const newBid = {
requestId: bid.impid,
bidderCode: BIDDER_CODE,
originalBidder: summary.bidder,
pubmaticServerErrorCode: summary.errorCode,
cpm: (parseFloat(summary.bid) || 0).toFixed(2),
width: summary.width,
height: summary.height,
creativeId: firstSummary ? (bid.crid || bid.id) : bid.id,
dealId: firstSummary ? (bid.dealid || UNDEFINED) : UNDEFINED,
currency: CURRENCY,
netRevenue: true,
ttl: 300,
referrer: referrer,
ad: firstSummary ? bid.adm : ''
};
bidResponses.push(newBid);
}
});
}
});
});
}
} catch (error) {
Expand All @@ -274,6 +275,7 @@ export const spec = {
getUserSyncs: (syncOptions, serverResponses) => {
let serverResponse;
let urls = [];
// Todo: Can fire multiple usersync calls if multiple responses for same adsize found
if (serverResponses.length > 0 && serverResponses[0] && serverResponses[0].body) {
serverResponse = serverResponses[0].body;
}
Expand All @@ -287,17 +289,19 @@ export const spec = {
url: bidder.usersync.url
});
} else {
utils.logWarn('PubMaticServer: Please enable iframe based user sync.');
utils.logWarn(bidder.bidder + ': Please enable iframe based user sync.');
}
} else if (bidder.usersync.type === IMAGE || bidder.usersync.type === 'redirect') {
} else if (bidder.usersync.type === IMAGE || bidder.usersync.type === REDIRECT) {
if (syncOptions.pixelEnabled) {
urls.push({
type: IMAGE,
url: bidder.usersync.url
});
} else {
utils.logWarn('PubMaticServer: Please enable pixel based user sync.');
utils.logWarn(bidder.bidder + ': Please enable pixel based user sync.');
}
} else {
utils.logWarn(bidder.bidder + ': Please provide valid user sync type.');
}
}
});
Expand Down
47 changes: 47 additions & 0 deletions modules/pubmaticServerBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Overview

```
Module Name: PubMatic-Server Bid Adapter
Module Type: Bidder Adapter
Maintainer: UOEDev@pubmatic.com
```

# Description

Connects to PubMatic exchange for bids.

PubMaticServer bid adapter supports Banner currently.

# Sample Ad Unit: For Publishers
```
var pbjs = pbjs || {};
pbjs.que.push(function() {
var adUnits = [{
code: 'test-div',
sizes: [
[300, 250],
[728, 90]
],
bids: [{
bidder: 'pubmaticServer',
params: {
publisherId: '301', // required
adSlot: '/15671365/DMDemo@728x90', // required
profileid: '', // required
divId: '', // required
versionid: '', // optional (Default 0)
// openRTB params
lat: '40.712775', // optional
lon: '-74.005973', // optional
yob: '1982', // optional
gender: 'M' // optional
}
}]
}];
});
```
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ exports.getTopWindowUrl = function () {
return href;
};

exports.getTopWindowHostName = function () {
let href;
try {
href = this.getTopWindowLocation().hostname;
} catch (e) {
href = '';
}

return href;
};

exports.getTopWindowReferrer = function() {
try {
return window.top.document.referrer;
Expand Down
8 changes: 4 additions & 4 deletions test/spec/modules/pubmaticServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ describe('PubMaticServer adapter', () => {
publisherId: '301',
adUnitId: '/15671365/DMDemo',
adUnitIndex: '0',
divId: '/19968336/header-bid-tag-1'
divId: '/19968336/header-bid-tag-1',
profId: 1
}
},
isValid = spec.isBidRequestValid(validBid);
Expand Down Expand Up @@ -189,14 +190,14 @@ describe('PubMaticServer adapter', () => {
describe('Request formation', () => {
it('Endpoint checking', () => {
let request = spec.buildRequests(bidRequests);
expect(request.url).to.equal('//hb.pubmatic.com/openrtb/241/?');
expect(request.url).to.equal('//ow.pubmatic.com/openrtb/241/?');
expect(request.method).to.equal('POST');
});

it('Request params check', () => {
let request = spec.buildRequests(bidRequests);
let data = JSON.parse(request.data);
expect(data.at).to.equal(2); // auction type
expect(data.at).to.equal(1); // auction type
expect(data.cur[0]).to.equal('USD'); // currency
expect(data.site.domain).to.be.a('string'); // domain should be set
expect(data.site.page).to.equal(bidRequests[0].params.kadpageurl); // forced pageURL
Expand All @@ -221,7 +222,6 @@ describe('PubMaticServer adapter', () => {
expect(data.imp[0].banner.format[1].h).to.equal(600); // height
expect(data.imp[0].ext.pmZoneId).to.equal(bidRequests[0].params.pmzoneid.split(',').slice(0, 50).map(id => id.trim()).join()); // pmzoneid
expect(data.imp[0].ext.adunit).to.equal(bidRequests[0].params.adUnitId); // adUnitId
expect(data.imp[0].ext.slotIndex).to.equal(bidRequests[0].params.adUnitIndex); // adUnitIndex
expect(data.imp[0].ext.div).to.equal(bidRequests[0].params.divId); // div
});
});
Expand Down

0 comments on commit c221899

Please sign in to comment.