Skip to content

Commit

Permalink
PubMatic Adapter : Support For Video Params from AdUnit MediaTypes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-shashank-jain committed May 25, 2021
1 parent 5a33702 commit 585c710
Show file tree
Hide file tree
Showing 2 changed files with 450 additions and 93 deletions.
128 changes: 60 additions & 68 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const VIDEO_CUSTOM_PARAMS = {
'linearity': DATA_TYPES.NUMBER,
'placement': DATA_TYPES.NUMBER,
'minbitrate': DATA_TYPES.NUMBER,
'maxbitrate': DATA_TYPES.NUMBER
'maxbitrate': DATA_TYPES.NUMBER,
'skip': DATA_TYPES.NUMBER
}

const NATIVE_ASSETS = {
Expand Down Expand Up @@ -167,6 +168,12 @@ const BB_RENDERER = {
}
};

const MEDIATYPE = [
BANNER,
VIDEO,
NATIVE
]

let publisherId = 0;
let isInvalidNativeRequest = false;
let NATIVE_ASSET_ID_TO_KEY_MAP = {};
Expand Down Expand Up @@ -531,7 +538,7 @@ function _createBannerRequest(bid) {
}

function _createVideoRequest(bid) {
var videoData = bid.params.video;
var videoData = utils.mergeDeep(utils.deepAccess(bid.mediaTypes, 'video'), bid.params.video);
var videoObj;

if (videoData !== UNDEFINED) {
Expand All @@ -549,11 +556,6 @@ function _createVideoRequest(bid) {
videoObj.w = parseInt(bid.mediaTypes.video.playerSize[0], 10);
videoObj.h = parseInt(bid.mediaTypes.video.playerSize[1], 10);
}
if (bid.params.video.hasOwnProperty('skippable')) {
videoObj.ext = {
'video_skippable': bid.params.video.skippable ? 1 : 0
};
}
} else {
videoObj = UNDEFINED;
utils.logWarn(LOG_WARN_PREFIX + 'Error: Video config params missing for adunit: ' + bid.params.adUnit + ' with mediaType set as video. Ignoring video impression in the adunit.');
Expand Down Expand Up @@ -581,6 +583,28 @@ function _addPMPDealsInImpression(impObj, bid) {
}
}

function _addDealCustomTargetings(imp, bid) {
var dctr = '';
var dctrLen;
if (bid.params.dctr) {
dctr = bid.params.dctr;
if (utils.isStr(dctr) && dctr.length > 0) {
var arr = dctr.split('|');
dctr = '';
arr.forEach(val => {
dctr += (val.length > 0) ? (val.trim() + '|') : '';
});
dctrLen = dctr.length;
if (dctr.substring(dctrLen, dctrLen - 1) === '|') {
dctr = dctr.substring(0, dctrLen - 1);
}
imp.ext['key_val'] = dctr.trim()
} else {
utils.logWarn(LOG_WARN_PREFIX + 'Ignoring param : dctr with value : ' + dctr + ', expects string-value, found empty or non-string value');
}
}
}

function _createImpressionObject(bid, conf) {
var impObj = {};
var bannerObj;
Expand All @@ -602,7 +626,7 @@ function _createImpressionObject(bid, conf) {
};

_addPMPDealsInImpression(impObj, bid);

_addDealCustomTargetings(impObj, bid);
if (bid.hasOwnProperty('mediaTypes')) {
for (mediaTypes in bid.mediaTypes) {
switch (mediaTypes) {
Expand Down Expand Up @@ -779,22 +803,28 @@ function _handleEids(payload, validBidRequests) {
}
}

function _checkMediaType(adm, newBid) {
function _checkMediaType(bid, newBid) {
// Create a regex here to check the strings
var admStr = '';
var videoRegex = new RegExp(/VAST\s+version/);
if (adm.indexOf('span class="PubAPIAd"') >= 0) {
newBid.mediaType = BANNER;
} else if (videoRegex.test(adm)) {
newBid.mediaType = VIDEO;
if (bid.ext && bid.ext['BidType'] != undefined) {
newBid.mediaType = MEDIATYPE[bid.ext.BidType];
} else {
try {
admStr = JSON.parse(adm.replace(/\\/g, ''));
if (admStr && admStr.native) {
newBid.mediaType = NATIVE;
utils.logInfo(LOG_WARN_PREFIX + 'bid.ext.BidType does not exist, checking alternatively for mediaType')
var adm = bid.adm;
var admStr = '';
var videoRegex = new RegExp(/VAST\s+version/);
if (adm.indexOf('span class="PubAPIAd"') >= 0) {
newBid.mediaType = BANNER;
} else if (videoRegex.test(adm)) {
newBid.mediaType = VIDEO;
} else {
try {
admStr = JSON.parse(adm.replace(/\\/g, ''));
if (admStr && admStr.native) {
newBid.mediaType = NATIVE;
}
} catch (e) {
utils.logWarn(LOG_WARN_PREFIX + 'Error: Cannot parse native reponse for ad response: ' + adm);
}
} catch (e) {
utils.logWarn(LOG_WARN_PREFIX + 'Error: Cannot parse native reponse for ad response: ' + adm);
}
}
}
Expand Down Expand Up @@ -884,38 +914,6 @@ function _blockedIabCategoriesValidation(payload, blockedIabCategories) {
}
}

function _handleDealCustomTargetings(payload, dctrArr, validBidRequests) {
var dctr = '';
var dctrLen;
// set dctr value in site.ext, if present in validBidRequests[0], else ignore
if (dctrArr.length > 0) {
if (validBidRequests[0].params.hasOwnProperty('dctr')) {
dctr = validBidRequests[0].params.dctr;
if (utils.isStr(dctr) && dctr.length > 0) {
var arr = dctr.split('|');
dctr = '';
arr.forEach(val => {
dctr += (val.length > 0) ? (val.trim() + '|') : '';
});
dctrLen = dctr.length;
if (dctr.substring(dctrLen, dctrLen - 1) === '|') {
dctr = dctr.substring(0, dctrLen - 1);
}
payload.site.ext = {
key_val: dctr.trim()
}
} else {
utils.logWarn(LOG_WARN_PREFIX + 'Ignoring param : dctr with value : ' + dctr + ', expects string-value, found empty or non-string value');
}
if (dctrArr.length > 1) {
utils.logWarn(LOG_WARN_PREFIX + 'dctr value found in more than 1 adunits. Value from 1st adunit will be picked. Ignoring values from subsequent adunits');
}
} else {
utils.logWarn(LOG_WARN_PREFIX + 'dctr value not found in 1st adunit, ignoring values from subsequent adunits');
}
}
}

function _assignRenderer(newBid, request) {
let bidParams, context, adUnitCode;
if (request.bidderRequest && request.bidderRequest.bids) {
Expand Down Expand Up @@ -950,22 +948,17 @@ export const spec = {
return false;
}
// video ad validation
if (bid.params.hasOwnProperty('video')) {
if (!bid.params.video.hasOwnProperty('mimes') || !utils.isArray(bid.params.video.mimes) || bid.params.video.mimes.length === 0) {
if (bid.hasOwnProperty('mediaTypes') && bid.mediaTypes.hasOwnProperty(VIDEO)) {
if (!bid.mediaTypes.video.mimes || (bid.params.video && (!bid.params.video.hasOwnProperty('mimes') || !utils.isArray(bid.params.video.mimes) || bid.params.video.mimes.length === 0))) {
utils.logWarn(LOG_WARN_PREFIX + 'Error: For video ads, mimes is mandatory and must specify atlease 1 mime value. Call to OpenBid will not be sent for ad unit:' + JSON.stringify(bid));
return false;
}
if (bid.hasOwnProperty('mediaTypes') && bid.mediaTypes.hasOwnProperty(VIDEO)) {
if (!bid.mediaTypes[VIDEO].hasOwnProperty('context')) {
utils.logError(`${LOG_WARN_PREFIX}: no context specified in bid. Rejecting bid: `, bid);
return false;
}
if (bid.mediaTypes[VIDEO].context === 'outstream' && !utils.isStr(bid.params.outstreamAU) && !bid.hasOwnProperty('renderer') && !bid.mediaTypes[VIDEO].hasOwnProperty('renderer')) {
utils.logError(`${LOG_WARN_PREFIX}: for "outstream" bids either outstreamAU parameter must be provided or ad unit supplied renderer is required. Rejecting bid: `, bid);
return false;
}
} else {
utils.logError(`${LOG_WARN_PREFIX}: mediaTypes or mediaTypes.video is not specified. Rejecting bid: `, bid);
if (!bid.mediaTypes[VIDEO].hasOwnProperty('context')) {
utils.logError(`${LOG_WARN_PREFIX}: no context specified in bid. Rejecting bid: `, bid);
return false;
}
if (bid.mediaTypes[VIDEO].context === 'outstream' && !utils.isStr(bid.params.outstreamAU) && !bid.hasOwnProperty('renderer') && !bid.mediaTypes[VIDEO].hasOwnProperty('renderer')) {
utils.logError(`${LOG_WARN_PREFIX}: for "outstream" bids either outstreamAU parameter must be provided or ad unit supplied renderer is required. Rejecting bid: `, bid);
return false;
}
}
Expand Down Expand Up @@ -1090,7 +1083,6 @@ export const spec = {
utils.deepSetValue(payload, 'regs.coppa', 1);
}

_handleDealCustomTargetings(payload, dctrArr, validBidRequests);
_handleEids(payload, validBidRequests);
_blockedIabCategoriesValidation(payload, blockedIabCategories);
_handleFlocId(payload, validBidRequests);
Expand Down Expand Up @@ -1164,7 +1156,7 @@ export const spec = {
if (parsedRequest.imp && parsedRequest.imp.length > 0) {
parsedRequest.imp.forEach(req => {
if (bid.impid === req.id) {
_checkMediaType(bid.adm, newBid);
_checkMediaType(bid, newBid);
switch (newBid.mediaType) {
case BANNER:
break;
Expand Down
Loading

0 comments on commit 585c710

Please sign in to comment.