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 5 commits
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
166 changes: 164 additions & 2 deletions modules/jwplayerBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
// import { config } from 'src/config';
import { VIDEO } from '../src/mediaTypes.js';
import { deepSetValue, isFn } from '../src/utils.js';

const BIDDER_CODE = 'jwplayer';
const URL = 'https://ib.adnxs.com/openrtb2/prebid';

const GVLID = 1046;
const SUPPORTED_AD_TYPES = [VIDEO];

// Video Parameters
// https://docs.prebid.org/dev-docs/bidder-adaptor.html#step-2-accept-video-parameters-and-pass-them-to-your-server
const VIDEO_ORTB_PARAMS = [
'mimes',
'minduration',
'maxduration',
'protocols',
'startdelay',
'placement',
'skip',
'skipafter',
'minbitrate',
'maxbitrate',
'delivery',
'playbackmethod',
'api',
'linearity'
];

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
Expand All @@ -29,10 +51,25 @@ 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.
*/
buildRequests: function(validBidRequests, bidderRequest) {},
buildRequests: function(bidRequests, bidderRequest) {
if (!bidRequests) {
return;
}

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

return {
method: 'POST',
url: URL,
data: payload
}
});
},

/**
* Unpack the response from the server into a list of bids.
Expand All @@ -50,4 +87,129 @@ export const spec = {
// onBidderError: function({ error, bidderRequest }) {}
};

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

// 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) {
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(bidRequest);

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

return impressions;
}

function buildImpressionVideo(bidRequest) {
const videoParams = deepAccess(bidRequest, 'mediaTypes.video', {});

const video = {};

// 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 (videoParams.hasOwnProperty(param)) {
video[param] = videoParams[param];
}
});

return video;
}

function buildImpressionExtension(bidRequest) {
return {
appnexus: {
placement_id: bidRequest.params.placementId
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}
};
}

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

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'};
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}

return floorData;
}

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
};

const videoParams = deepAccess(bidRequest, 'mediaTypes.video', {});

// Site Content
if (videoParams.content && isPlainObject(videoParams.content)) {
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
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(videoParams.content[contentKey])) ||
(contentNumberkeys.indexOf(contentKey) > -1 && isNumber(videoParams.content[contentKey])) ||
(contentObjectKeys.indexOf(contentKey) > -1 && isPlainObject(videoParams.content[contentKey])) ||
(contentArrayKeys.indexOf(contentKey) > -1 && isArray(videoParams.content[contentKey]) &&
videoParams.content[contentKey].every(catStr => isStr(catStr)))) {
site.content[contentKey] = videoParams.content[contentKey];
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
} else {
logMessage('JWPlayer bid adapter validation error: ', contentKey, ' is either not supported is OpenRTB V2.5 or value is undefined');
}
}
}
return site;
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}

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

registerBidder(spec);