Skip to content

merging UOE_4057 multiformat #1

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

201 changes: 150 additions & 51 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function _parseAdSlot(bid) {
bid.params.adUnitIndex = '0';
bid.params.width = 0;
bid.params.height = 0;
var sizesArrayExists = (bid.hasOwnProperty('sizes') && utils.isArray(bid.sizes) && bid.sizes.length >= 1);
// var sizesArrayExists = (bid.hasOwnProperty('sizes') && utils.isArray(bid.sizes) && bid.sizes.length >= 1);
bid.params.adSlot = _cleanSlot(bid.params.adSlot);

var slot = bid.params.adSlot;
Expand All @@ -219,26 +219,37 @@ function _parseAdSlot(bid) {
}
// check if size is mentioned in sizes array. in that case do not check for @ in adslot
splits = slot.split('@');
if (splits.length != 2) {
if (!(sizesArrayExists)) {
/* if (splits.length != 2) {
if (!sizesArrayExists) {
utils.logWarn('AdSlot Error: adSlot not in required format');
return;
}
}
} */
bid.params.adUnit = splits[0];
if (splits.length > 1) { // i.e size is specified in adslot, so consider that and ignore sizes array
if (splits.length > 1) {
// i.e size is specified in adslot, so consider that and ignore sizes array
splits = splits[1].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]);
delete bid.sizes;
} else if (sizesArrayExists) {
// delete bid.sizes;
} else {
if (bid.hasOwnProperty('mediaTypes') &&
bid.mediaTypes.hasOwnProperty(BANNER) &&
bid.mediaTypes.banner.hasOwnProperty('sizes')) {
bid.params.width = parseInt(bid.mediaTypes.banner.sizes[0][0]);
bid.params.height = parseInt(bid.mediaTypes.banner.sizes[0][1]);
bid.mediaTypes.banner.sizes = bid.mediaTypes.banner.sizes.splice(1, bid.mediaTypes.banner.sizes.length - 1);
} else {
utils.logWarn(BIDDER_CODE + ' error: Missing mediaTypes.banner in adSlot: ' + bid.params.adSlot);
}
} /* else if (sizesArrayExists) {
bid.params.width = parseInt(bid.sizes[0][0]);
bid.params.height = parseInt(bid.sizes[0][1]);
}
} */
}

function _initConf(refererInfo) {
Expand Down Expand Up @@ -586,11 +597,76 @@ function _createNativeRequest(params) {
return nativeRequestObject;
}

function _createBannerRequest(bid) {
var sizes = bid.mediaTypes.banner.sizes;
var format = [];
var bannerObj;

if (sizes !== undefined && utils.isArray(sizes)) {
bannerObj = {};
if (!bid.params.width && !bid.params.height) {
bannerObj.w = parseInt(sizes[0][0]);
bannerObj.h = parseInt(sizes[0][1]);
sizes = sizes.splice(1, sizes.length - 1);
} else {
bannerObj.w = bid.params.width;
bannerObj.h = bid.params.height;
}
if (sizes.length > 0) {
format = [];
sizes.forEach(function (size) {
format.push({ w: size[0], h: size[1] });
});
bannerObj.format = format;
}
bannerObj.pos = 0;
bannerObj.topframe = utils.inIframe() ? 0 : 1;
} else {
utils.logWarn(BIDDER_CODE + 'Error: mediaTypes.banner.size missing for adunit: ' + bid.params.adUnit + '. Ignoring the banner impression in the adunit.');
bannerObj = undefined;
}
return bannerObj;
}

function _createVideoRequest(bid) {
var videoData = bid.params.video;
var videoObj;

if (videoData !== undefined) {
videoObj = {};
for (var key in VIDEO_CUSTOM_PARAMS) {
if (videoData.hasOwnProperty(key)) {
videoObj[key] = _checkParamDataType(key, videoData[key], VIDEO_CUSTOM_PARAMS[key]);
}
}
// read playersize and assign to h and w.
if (utils.isArray(bid.mediaTypes.video.playerSize[0])) {
videoObj.w = parseInt(bid.mediaTypes.video.playerSize[0][0]);
videoObj.h = parseInt(bid.mediaTypes.video.playerSize[0][1]);
} else if (utils.isNumber(bid.mediaTypes.video.playerSize[0])) {
videoObj.w = parseInt(bid.mediaTypes.video.playerSize[0]);
videoObj.h = parseInt(bid.mediaTypes.video.playerSize[1]);
}
if (bid.params.video.hasOwnProperty('skippable')) {
videoObj.ext = {
'video_skippable': bid.params.video.skippable ? 1 : 0
};
}
} else {
videoObj = undefined;
utils.logWarn(BIDDER_CODE + 'Error: Video config params missing for adunit: ' + bid.params.adUnit + ' with mediaType set as video. Ignoring video impression in the adunit.');
}
return videoObj;
}

function _createImpressionObject(bid, conf) {
var impObj = {};
var bannerObj = {};
var videoObj = {};
var bannerObj;
var videoObj;
var nativeObj = {};
var sizes = bid.hasOwnProperty('sizes') ? bid.sizes : [];
var mediaTypes = '';
var format = [];

impObj = {
id: bid.bidId,
Expand All @@ -603,42 +679,42 @@ function _createImpressionObject(bid, conf) {
bidfloorcur: bid.params.currency ? _parseSlotParam('currency', bid.params.currency) : DEFAULT_CURRENCY
};

if (bid.params.hasOwnProperty('video')) {
var videoData = bid.params.video;

for (var key in VIDEO_CUSTOM_PARAMS) {
if (videoData.hasOwnProperty(key)) {
videoObj[key] = _checkParamDataType(key, videoData[key], VIDEO_CUSTOM_PARAMS[key])
}
}
// read playersize and assign to h and w.
if (utils.isArray(bid.mediaTypes.video.playerSize[0])) {
videoObj.w = bid.mediaTypes.video.playerSize[0][0];
videoObj.h = bid.mediaTypes.video.playerSize[0][1];
} else if (utils.isNumber(bid.mediaTypes.video.playerSize[0])) {
videoObj.w = bid.mediaTypes.video.playerSize[0];
videoObj.h = bid.mediaTypes.video.playerSize[1];
}
if (bid.params.video.hasOwnProperty('skippable')) {
videoObj.ext = {
'video_skippable': bid.params.video.skippable ? 1 : 0
if (bid.hasOwnProperty('mediaTypes')) {
for (mediaTypes in bid.mediaTypes) {
switch (mediaTypes) {
case BANNER:
bannerObj = _createBannerRequest(bid);
if (bannerObj !== undefined) {
impObj.banner = bannerObj;
}
break;
case NATIVE:
nativeObj['request'] = JSON.stringify(_createNativeRequest(bid.nativeParams));
if (!isInvalidNativeRequest) {
impObj.native = nativeObj;
} else {
utils.logWarn(BIDDER_CODE + 'Error: Error in Native adunit ' + bid.params.adUnit + '. Ignoring the adunit. Refer to ' + PREBID_NATIVE_HELP_LINK + ' for more details.');
}
break;
case VIDEO:
videoObj = _createVideoRequest(bid);
if (videoObj !== undefined) {
impObj.video = videoObj;
}
break;
}
}

impObj.video = videoObj;
} else if (bid.nativeParams) {
impObj.native = {};
impObj.native['request'] = JSON.stringify(_createNativeRequest(bid.nativeParams));
} else {
// mediaTypes is not present, so this is a banner only impression
// this part of code is required for older testcases with no 'mediaTypes' to run succesfully.
bannerObj = {
pos: 0,
w: bid.params.width,
h: bid.params.height,
topframe: utils.inIframe() ? 0 : 1,
}
topframe: utils.inIframe() ? 0 : 1
};
if (utils.isArray(sizes) && sizes.length > 1) {
sizes = sizes.splice(1, sizes.length - 1);
var format = [];
sizes.forEach(size => {
format.push({
w: size[0],
Expand All @@ -649,11 +725,10 @@ function _createImpressionObject(bid, conf) {
}
impObj.banner = bannerObj;
}
if (isInvalidNativeRequest && impObj.hasOwnProperty('native')) {
utils.logWarn(BIDDER_CODE + ': Call to OpenBid will not be sent for native ad unit as it does not contain required valid native params.' + JSON.stringify(bid) + ' Refer:' + PREBID_NATIVE_HELP_LINK);
return;
}
return impObj;

return impObj.hasOwnProperty(BANNER) ||
impObj.hasOwnProperty(NATIVE) ||
impObj.hasOwnProperty(VIDEO) ? impObj : undefined;
}

function _getDigiTrustObject(key) {
Expand Down Expand Up @@ -709,6 +784,24 @@ function _handleEids(payload) {
payload.user.eids = eids;
}
}
// TODO: need a better way to identify mediaType from response.
function _checkMediaType(adm, newBid) {
// Create a regex here to check the strings
var admStr = '';
if (adm.indexOf('VAST version') >= 0) {
newBid.mediaType = VIDEO;
} else if (adm.indexOf('span class="PubAPIAd"') >= 0) {
newBid.mediaType = BANNER;
}
try {
admStr = JSON.parse(adm.replace(/\\/g, ''));
if (admStr && admStr.native) {
newBid.mediaType = NATIVE;
}
} catch (e) {

}
}

function _parseNativeResponse(bid, newBid) {
newBid.native = {};
Expand All @@ -721,7 +814,7 @@ function _parseNativeResponse(bid, newBid) {
return;
}
if (adm && adm.native && adm.native.assets && adm.native.assets.length > 0) {
newBid.mediaType = 'native';
newBid.mediaType = NATIVE;
for (let i = 0, len = adm.native.assets.length; i < len; i++) {
switch (adm.native.assets[i].id) {
case NATIVE_ASSET_ID.TITLE:
Expand Down Expand Up @@ -961,14 +1054,20 @@ export const spec = {
};
if (parsedRequest.imp && parsedRequest.imp.length > 0) {
parsedRequest.imp.forEach(req => {
if (bid.impid === req.id && req.hasOwnProperty('video')) {
newBid.mediaType = 'video';
newBid.width = bid.hasOwnProperty('w') ? bid.w : req.video.w;
newBid.height = bid.hasOwnProperty('h') ? bid.h : req.video.h;
newBid.vastXml = bid.adm;
}
if (bid.impid === req.id && req.hasOwnProperty('native')) {
_parseNativeResponse(bid, newBid);
if (bid.impid === req.id) {
_checkMediaType(bid.adm, newBid);
switch (newBid.mediaType) {
case BANNER:
break;
case VIDEO:
newBid.width = bid.hasOwnProperty('w') ? bid.w : req.video.w;
newBid.height = bid.hasOwnProperty('h') ? bid.h : req.video.h;
newBid.vastXml = bid.adm;
break;
case NATIVE:
_parseNativeResponse(bid, newBid);
break;
}
}
});
}
Expand Down
Loading