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

JW Demand: Implemented buildRequests. #26

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
incorporated feedback from pr and refactored.
  • Loading branch information
jorgeluisrocha committed May 13, 2022
commit 220b341d2771ad94c265b26b2d5810c1de4c6eaa
187 changes: 136 additions & 51 deletions modules/jwplayerBidAdapter.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,23 @@ const URL = 'https://ib.adnxs.com/openrtb2/prebid';
const GVLID = 1046;
const SUPPORTED_AD_TYPES = [VIDEO];

const VIDEO_ORTB_PARAMS = [
'mimes',
'minduration',
'maxduration',
'placement',
'protocols',
'startdelay',
'skip',
'skipafter',
'minbitrate',
'maxbitrate',
'delivery',
'playbackmethod',
'api',
'linearity'
];
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
@@ -32,7 +49,7 @@ export const spec = {
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @param {BidRequest[]} bidRequests A non-empty list of bid requests, or ad units, which should be sent to the server.
* @param bidderRequest
* @return ServerRequest Info describing the request to the server.
*/
@@ -42,7 +59,7 @@ export const spec = {
}

return bidRequests.map(bidRequest => {
const payload = buildRequest(bidRequest, bidderRequests);
const payload = buildRequest(bidRequest, bidderRequest);

return {
method: 'POST',
@@ -69,75 +86,143 @@ export const spec = {
};

function buildRequest(bidRequest, bidderRequest) {
const {params} = bidRequest;
// bidRequest.mediaTypes.video
// bidRequest.params (bid parameters)

const videoAdUnit = deepAccess(bidRequest, 'mediaTypes.video', {});
const videoBidderParams = deepAccess(bidRequest, 'params.video', {});
// bidderRequest.gdprConsent
// bidderRequest.uspConsent

// Open RTB Request Object
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
const openrtbRequest = {
id: params.bidId,
imp: buildRequestImpression(bidRequest, bidderRequest),
site: buildRequestSite(),
device: buildRequestDevice()
};

const videoParams = {
...videoAdUnit,
...videoBidderParams
// Attaching GDPR Consent Params
if (bidderRequest.gdprConsent) {
deepSetValue(openrtbRequest, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(openrtbRequest, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}

// CCPA
if (bidderRequest.uspConsent) {
deepSetValue(openrtbRequest, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}

return JSON.stringify(openrtbRequest);;
}

function buildRequestImpression(bidRequest, bidderRequest) {
const impressions = [];

const impressionObject = {
id: bidRequest.adUnitCode,
secure: isSecure() ? 1 : 0
};

impressionObject.video = buildImpressionVideo(bidRequest);

const bidFloorData = buildBidFloorData(bidRequest);
impressionObject.bidfloor = bidFloorData.floor;
impressionObject.bidfloorcur = bidFloorData.currency;

impressionObject.ext = buildImpressionExtension(); // TODO: Complete

impressions.push(impressionObject);
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved

return impressions;
}

function buildImpressionVideo(bidRequest) {
const videoAdUnit = deepAccess(bidRequest, 'mediaTypes.video', {});
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved

const playerSize = videoAdUnit.playerSize;

const contentWidth = playerSize[0][0];
const contentHeight = playerSize[0][1];
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved

const video = {
w: parseInt(videoParams.playerSize[0][0], 10),
h: parseInt(videoParams.playerSize[0][1], 10)
w: parseInt(contentWidth, 10),
h: parseInt(contentHeight, 10)
}

// Obtain all ORTB params related video from Ad Unit
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
VIDEO_ORTB_PARAMS.forEach((param) => {
if (videoAdUnit.hasOwnProperty(param)) {
video[param] = videoAdUnit[param];
}
});

// Placement Inference Rules:
// - If no placement is defined then default to 1 (In Stream)
video.placement = video.placement || 2;
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved

// - If product is instream (for instream context) then override placement to 1
if (params.context === 'instream') {
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
video.startdelay = video.startdelay || 0;
video.placement = 1;
}
}

// Bid FLoor
function buildBidFloorData(bidRequest) {
const {params} = bidRequest;
// Bid Floor
const bidFloorRequest = {
currency: bidRequest.params.cur || 'USD',
currency: params.currency || 'USD',
mediaType: 'video',
size: '*'
};

let floorData = bidRquest.params;
let floorData;
if (isFn(bidRequest.getFloor)) {
floorData = bidRequest.getFloor(bidFloorRequest);
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (params.bidfloor) {
floorData = {floor: params.bidfloor, currency: params.currency || 'USD'};
}
}

// Open RTB Request Object
const openrtbRequest = {
id: bidRequest.bidId,
imp: [
{
id: '1',
video: video,
secure: isSecure() ? 1 : 0,
bidfloor: floorData.floor,
bidfloorcur: floorData.currency
}
],
site: {
domain: window.location.hostname,
page: window.location.href,
ref: bidRequest.refererInfo ? bidRequest.refererInfo.referer || null : null
},
ext: {
hb: 1,
prebidver: '$prebid.version$',
adapterver: spec.VERSION
}
} else if (params.bidfloor) {
floorData = {floor: params.bidfloor, currency: params.currency || 'USD'};
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}

// content
return floorData;
}

// Attaching GDPR Consent Params
if (bidderRequest.gdprConsent) {
deepSetValue(openrtbRequest, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(openrtbRequest, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
function buildRequestSite(bidRequest) {
const site = {
domain: window.location.hostname,
page: window.location.href,
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
ref: bidRequest.refererInfo ? bidRequest.refererInfo.referer || null : null
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
};

// CCPA
if (bidderRequest.uspConsent) {
deepSetValue(openrtbRequest, 'regs.ext.us_privacy', bidderRequest.uspConsent);
// Site Content
/*
if (videoAdUnit.content && isPlainObject(videoAdUnit.content)) {
openrtbRequest.site.content = {};
const contentStringKeys = ['id', 'title', 'series', 'season', 'genre', 'contentrating', 'language', 'url'];
const contentNumberkeys = ['episode', 'prodq', 'context', 'livestream', 'len'];
const contentArrayKeys = ['cat'];
const contentObjectKeys = ['ext'];
for (const contentKey in videoBidderParams.content) {
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
if (
(contentStringKeys.indexOf(contentKey) > -1 && isStr(videoAdUnit.content[contentKey])) ||
(contentNumberkeys.indexOf(contentKey) > -1 && isNumber(videoAdUnit.content[contentKey])) ||
(contentObjectKeys.indexOf(contentKey) > -1 && isPlainObject(videoAdUnit.content[contentKey])) ||
(contentArrayKeys.indexOf(contentKey) > -1 && isArray(videoAdUnit.content[contentKey]) &&
videoAdUnit.content[contentKey].every(catStr => isStr(catStr)))) {
site.content[contentKey] = videoAdUnit.content[contentKey];
} else {
logMessage('JWPlayer bid adapter validation error: ', contentKey, ' is either not supported is OpenRTB V2.5 or value is undefined');
}
}
}
*/

return JSON.stringify(openrtbRequest);
return site;
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}

function buildRequestDevice() {
return {
ua: navigator.userAgent,
ip: ''
};
}

registerBidder(spec);