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
Show file tree
Hide file tree
Changes from 7 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
157 changes: 155 additions & 2 deletions modules/jwplayerBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
// import { config } from 'src/config';
import { VIDEO } from '../src/mediaTypes.js';
import { isStr,
isPlainObject,
isNumber,
isArray,
isFn,
deepAccess,
deepSetValue,
logMessage } 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 +58,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 +94,113 @@ 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.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 impressionObject = {
id: bidRequest.adUnitCode,
};

impressionObject.video = buildImpressionVideo(bidRequest);

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

impressionObject.ext = buildImpressionExtension(bidRequest);

return [impressionObject];
}

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;
const currency = params.currency || 'USD';

let floorData;
if (isFn(bidRequest.getFloor)) {
// Bid Floor
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
const bidFloorRequest = {
currency: currency,
mediaType: 'video',
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
size: '*'
};
floorData = bidRequest.getFloor(bidFloorRequest);
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
} else if (params.bidfloor) {
floorData = {floor: params.bidfloor, currency: currency};
}

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
site.content = {};
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
for (const contentKey in videoParams.content) {
site.content[contentKey] = videoParams.content[contentKey];
}
}
return site;
jorgeluisrocha marked this conversation as resolved.
Show resolved Hide resolved
}

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

registerBidder(spec);
64 changes: 63 additions & 1 deletion test/spec/modules/jwplayerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ describe('jwplayer adapter tests', function() {
var sandbox, clock, frozenNow = new Date();

beforeEach(function() {
this.defaultBidderRequest = {
'gdprConsent': {
'consentString': '',
'gdprApplies': true
},
'uspConsent': true
}

sandbox = sinon.sandbox.create();
clock = sinon.useFakeTimers(frozenNow.getTime());
});
Expand Down Expand Up @@ -37,7 +45,61 @@ describe('jwplayer adapter tests', function() {
});
});

describe('buildRequests for video', function() {});
describe('buildRequests for video', function() {
it('buildRequests works', function() {
const bidRequests = [
{
'bidder': 'jwplayer',
'params': {
'placementId': 12345
},
'mediaTypes': {
'video': {
'playerSize': [640, 480],
'content': {}
}
},
'bidRequestsCount': 1,
'adUnitCode': 'testAdUnitCode',
'bidId': 'testBidId'
}
]

const serverRequests = spec.buildRequests(bidRequests, this.defaultBidderRequest);

/* eslint-disable no-console */

serverRequests.forEach(serverRequest => {
expect(serverRequest.url).to.have.string('https://ib.adnxs.com/openrtb2/prebid');
expect(serverRequest.method).to.equal('POST');

const openrtbRequest = JSON.parse(serverRequest.data);
console.log(openrtbRequest);
expect(openrtbRequest.id).to.not.equal(null);
expect(openrtbRequest.id).to.have.string('testBidId');

expect(openrtbRequest.site).to.not.equal(null);
expect(openrtbRequest.device).to.not.equal(null);
expect(openrtbRequest.device.ua).to.equal(navigator.userAgent);

expect(openrtbRequest.imp).to.not.equal(null);
expect(openrtbRequest.imp[0]).to.not.equal(null);
expect(openrtbRequest.imp[0].video).to.not.equal(null);
expect(openrtbRequest.imp[0].ext).to.not.equal(null);
expect(openrtbRequest.imp[0].ext.appnexus).to.not.equal(null);
expect(openrtbRequest.imp[0].ext.appnexus.placement_id).to.not.equal(null);
expect(openrtbRequest.imp[0].ext.appnexus.placement_id).to.equal(12345);

expect(openrtbRequest.user).to.not.equal(null);
expect(openrtbRequest.user.ext).to.not.equal(null);

expect(openrtbRequest.regs).to.not.equal(null);
expect(openrtbRequest.regs.ext).to.not.equal(null);
expect(openrtbRequest.regs.ext.gdpr).to.equal(1);
expect(openrtbRequest.regs.ext.us_privacy).to.equal(true);
})
});
});

describe('interpretResponse for video', function() {});

Expand Down