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

Ad unit parse fix #36

Merged
merged 13 commits into from
Aug 20, 2018
129 changes: 74 additions & 55 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as utils from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';
import {
registerBidder
} from 'src/adapters/bidderFactory';
const constants = require('src/constants.json');

const BIDDER_CODE = 'pubmatic';
@@ -71,25 +73,25 @@ function _parseAdSlot(bid) {
bid.params.adSlot = _cleanSlot(bid.params.adSlot);

var slot = bid.params.adSlot;
var splits = slot.split(':');
try{
var splits = slot.split('@');

slot = splits[0];
if (splits.length == 2) {
bid.params.adUnitIndex = splits[1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Complete unit test cases before QA drop.

}
splits = slot.split('@');
if (splits.length != 2) {
utils.logWarn('AdSlot Error: adSlot not in required format');
return;
slot = splits[0];
Copy link
Collaborator

Choose a reason for hiding this comment

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

why are we initializing this variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's already initialized on line no 75 and value has been assigned for ad slot on line no 82

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok

if (splits.length == 2) {
bid.params.adUnitIndex = splits[1].split(":").length == 2 ? splits[1].split(":")[1] : "0";
}
splits = splits[1].split(":")[0].split("x");
if (splits.length != 2) {
utils.logWarn('AdSlot Error: adSlot not in required format');
return;
}
bid.params.width = parseInt(splits[0]);
bid.params.height = parseInt(splits[1]);
bid.params.adUnit = slot;
}
bid.params.adUnit = splits[0];
splits = splits[1].split('x');
if (splits.length != 2) {
catch(e){
utils.logWarn('AdSlot Error: adSlot not in required format');
return;
}
bid.params.width = parseInt(splits[0]);
bid.params.height = parseInt(splits[1]);
}

function _initConf() {
@@ -174,11 +176,11 @@ export const spec = {
code: BIDDER_CODE,

/**
* Determines whether or not the given bid request is valid. Valid bid request must have placementId and hbid
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
* Determines whether or not the given bid request is valid. Valid bid request must have placementId and hbid
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: bid => {
if (bid && bid.params) {
if (!utils.isStr(bid.params.publisherId)) {
@@ -195,11 +197,11 @@ export const spec = {
},

/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: (validBidRequests, bidderRequest) => {
var conf = _initConf();
var payload = _createOrtbTemplate(conf);
@@ -260,40 +262,57 @@ export const spec = {
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {*} response A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
* Unpack the response from the server into a list of bids.
*
* @param {*} response A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: (response, request) => {
const bidResponses = [];
try {
let requestData = JSON.parse(request.data);
if (requestData && requestData.imp && requestData.imp.length > 0) {
requestData.imp.forEach(impData => {
bidResponses.push({
requestId: impData.id,
width: 0,
height: 0,
ttl: 300,
ad: '',
creativeId: 0,
netRevenue: NET_REVENUE,
cpm: 0,
currency: CURRENCY,
referrer: utils.getTopWindowUrl()
})
});
}
if (response.body && response.body.seatbid && utils.isArray(response.body.seatbid)) {
// Supporting multiple bid responses for same adSize
response.body.seatbid.forEach(seatbidder => {
seatbidder.bid &&
utils.isArray(seatbidder.bid) &&
seatbidder.bid.forEach(bid => {
let newBid = {
requestId: bid.impid,
cpm: (parseFloat(bid.price) || 0).toFixed(2),
width: bid.w,
height: bid.h,
creativeId: bid.crid || bid.id,
dealId: bid.dealid,
currency: CURRENCY,
netRevenue: NET_REVENUE,
ttl: 300,
referrer: utils.getTopWindowUrl(),
ad: bid.adm
};

if (bid.ext && bid.ext.deal_channel) {
newBid['dealChannel'] = dealChannelValues[bid.ext.deal_channel] || null;
}

bidResponses.push(newBid);
});
utils.isArray(seatbidder.bid) &&
seatbidder.bid.forEach(bid => {
bidResponses.forEach(br => {
if (br.requestId == bid.impid) {
br.requestId = bid.impid;
br.cpm = (parseFloat(bid.price) || 0).toFixed(2);
br.width = bid.w;
br.height = bid.h;
br.creativeId = bid.crid || bid.id;
br.dealId = bid.dealid;
br.currency = CURRENCY;
br.netRevenue = NET_REVENUE;
br.ttl = 300;
br.referrer = utils.getTopWindowUrl();
br.ad = bid.adm;

if (bid.ext && bid.ext.deal_channel) {
br['dealChannel'] = dealChannelValues[bid.ext.deal_channel] || null;
}
}
})
});
});
}
} catch (error) {
@@ -303,8 +322,8 @@ export const spec = {
},

/**
* Register User Sync.
*/
* Register User Sync.
*/
getUserSyncs: (syncOptions, responses, gdprConsent) => {
let syncurl = USYNCURL + publisherId;

22 changes: 15 additions & 7 deletions src/ajax.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {parse as parseURL, format as formatURL} from './url';
import { config } from 'src/config';

var utils = require('./utils');

@@ -54,9 +55,11 @@ export function ajaxBuilder(timeout = 3000) {
x.onerror = function () {
callbacks.error('error', x);
};
x.ontimeout = function () {
callbacks.error('timeout', x);
};
if (!config.getConfig('disableAjaxTimeout')) {
x.ontimeout = function () {
utils.logError(' xhr timeout after ', x.timeout, 'ms');
};
}
x.onprogress = function() {
utils.logMessage('xhr onprogress');
};
@@ -71,9 +74,11 @@ export function ajaxBuilder(timeout = 3000) {
}
}
};
x.ontimeout = function () {
utils.logError(' xhr timeout after ', x.timeout, 'ms');
};
if (!config.getConfig('disableAjaxTimeout')) {
x.ontimeout = function () {
utils.logError(' xhr timeout after ', x.timeout, 'ms');
};
}
}

if (method === 'GET' && data) {
@@ -84,7 +89,10 @@ export function ajaxBuilder(timeout = 3000) {

x.open(method, url);
// IE needs timoeut to be set after open - see #1410
x.timeout = timeout;
// Disabled timeout temporarily to avoid xhr failed requests. https://github.com/prebid/Prebid.js/issues/2648
if (!config.getConfig('disableAjaxTimeout')) {
x.timeout = timeout;
}

if (!useXDomainRequest) {
if (options.withCredentials) {
9 changes: 8 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ const DEFAULT_BIDDER_TIMEOUT = 3000;
const DEFAULT_PUBLISHER_DOMAIN = window.location.origin;
const DEFAULT_COOKIESYNC_DELAY = 100;
const DEFAULT_ENABLE_SEND_ALL_BIDS = true;

const DEFAULT_DISABLE_AJAX_TIMEOUT = false;
const DEFAULT_TIMEOUTBUFFER = 200;

export const RANDOM = 'random';
@@ -163,6 +163,13 @@ export function newConfig() {
this._timoutBuffer = val;
},

_disableAjaxTimeout: DEFAULT_DISABLE_AJAX_TIMEOUT,
get disableAjaxTimeout() {
return this._disableAjaxTimeout;
},
set disableAjaxTimeout(val) {
this._disableAjaxTimeout = val;
},
};

function hasGranularity(val) {
Loading
Oops, something went wrong.