-
Notifications
You must be signed in to change notification settings - Fork 0
Multiformat support #5
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
Changes from all commits
7e5986c
d1146d7
609ec16
792b854
b6740d0
0569f41
fd6bf03
bd14420
65e2f1e
13c87de
d9f3562
adbc774
21fe846
9faff35
78cd16d
4a40e1e
a218d43
f65b3fb
a717fbc
af2503d
21277c1
53ac4fe
ac1919f
1de1623
6b059b7
9c80c46
e0f8763
c4ee495
d4abb65
f143cee
06a6e34
84a9c02
e3d068e
468c3ab
11fce22
b23e2dd
6b8c31b
137c71c
9dc4880
8908d88
d3ab524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import * as utils from '../src/utils'; | ||
import { registerBidder } from '../src/adapters/bidderFactory'; | ||
import { BANNER, VIDEO, NATIVE } from '../src/mediaTypes'; | ||
import {config} from '../src/config'; | ||
import * as utils from 'src/utils'; | ||
import { registerBidder } from 'src/adapters/bidderFactory'; | ||
import { BANNER, VIDEO, NATIVE } from 'src/mediaTypes'; | ||
import {config} from 'src/config'; | ||
|
||
const BIDDER_CODE = 'pubmatic'; | ||
const ENDPOINT = '//hbopenbid.pubmatic.com/translator?source=prebid-client'; | ||
|
@@ -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; | ||
|
@@ -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]); | ||
} | ||
} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better if we delete the commented code, we can find it in older versions of code if required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, will delete once i am done with development. |
||
} | ||
|
||
function _initConf(refererInfo) { | ||
|
@@ -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, | ||
|
@@ -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], | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to consider case-insensitive regex, as there will be whitespaces |
||
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 = {}; | ||
|
@@ -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: | ||
|
@@ -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; | ||
} | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we delete the statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just commented out old code for now, till i am testing it. Will remove all commented code before QA drop.