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

Add banner support to Beachfront adapter #2117

Merged
merged 20 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
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
Update display request and response to support multiple bids
  • Loading branch information
John Salis committed Feb 7, 2018
commit 78ba0dc2923f57e5966b4bab48be51ba300b7919
124 changes: 91 additions & 33 deletions modules/beachfrontBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as utils from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';
import { VIDEO, BANNER } from 'src/mediaTypes';
import find from 'core-js/library/fn/array/find';

const ADAPTER_VERSION = '1.0';
const ADAPTER_NAME = 'BFIO_PREBID';

export const VIDEO_ENDPOINT = '//reachms.bfmio.com/bid.json?exchange_id=';
export const BANNER_ENDPOINT = '//display.bfmio.com/bid_display?exchange_id=';
export const BANNER_ENDPOINT = '//display.bfmio.com/prebid_display';

export const spec = {
code: 'beachfront',
Expand All @@ -14,50 +18,69 @@ export const spec = {
},

buildRequests(bids) {
return bids.map(bid => {
let isVideo = bid.mediaTypes && bid.mediaTypes.video;
return {
let requests = [];
let videoBids = bids.filter(bid => isVideoBid(bid));
let bannerBids = bids.filter(bid => !isVideoBid(bid));

console.log('BIDS', bids);

videoBids.forEach(bid => {
requests.push({
method: 'POST',
url: (isVideo ? VIDEO_ENDPOINT : BANNER_ENDPOINT) + bid.params.appId,
data: createRequestParams(bid),
url: VIDEO_ENDPOINT + bid.params.appId,
data: createVideoRequestData(bid),
bidRequest: bid
};
});
});
if (bannerBids.length) {
requests.push({
method: 'POST',
url: BANNER_ENDPOINT,
data: createBannerRequestData(bannerBids),
bidRequest: bannerBids
});
}
return requests;
},

interpretResponse(response, { bidRequest }) {
response = response.body;
let size = getSize(bidRequest.sizes);
let isVideo = bidRequest.mediaTypes && bidRequest.mediaTypes.video;
if (isVideo) {

if (isVideoBid(bidRequest)) {
if (!response || !response.url || !response.bidPrice) {
utils.logWarn(`No valid bids from ${spec.code} bidder`);
utils.logWarn(`No valid video bids from ${spec.code} bidder`);
return [];
}
let size = getFirstSize(bidRequest);
return {
requestId: bidRequest.bidId,
bidderCode: spec.code,
vastUrl: response.url,
cpm: response.bidPrice,
width: size.width,
height: size.height,
width: size.w,
height: size.h,
creativeId: response.cmpId,
mediaType: VIDEO,
currency: 'USD',
netRevenue: true,
ttl: 300
};
} else {
let bids = response.seatbid[0].bid;
return bids.map((bid) => {
if (!response || !response.length) {
utils.logWarn(`No valid banner bids from ${spec.code} bidder`);
return [];
}
return response.map((bid) => {
let request = find(bidRequest, req => req.adUnitCode === bid.slot);
let size = getFirstSize(request);
return {
requestId: bidRequest.bidId,
requestId: request.bidId,
bidderCode: spec.code,
ad: bid.adm,
cpm: parseFloat(bid.price),
width: parseInt(bid.w),
height: parseInt(bid.h),
creativeId: bid.crid,
cpm: bid.price,
width: size.w,
height: size.h,
mediaType: BANNER,
currency: 'USD',
netRevenue: true,
Expand All @@ -68,13 +91,19 @@ export const spec = {
}
};

function getSize(sizes) {
let parsedSizes = utils.parseSizesInput(sizes);
let [ width, height ] = parsedSizes.length ? parsedSizes[0].split('x') : [];
return {
width: parseInt(width, 10) || undefined,
height: parseInt(height, 10) || undefined
};
function getSizes(bid) {
return utils.parseSizesInput(bid.sizes).map(size => {
let [ width, height ] = size.split('x');
return {
w: parseInt(width, 10) || undefined,
h: parseInt(height, 10) || undefined
};
});
}

function getFirstSize(bid) {
let sizes = getSizes(bid);
return sizes.length ? sizes[0] : { w: undefined, h: undefined };
}

function isMobile() {
Expand All @@ -85,23 +114,27 @@ function isConnectedTV() {
return (/(smart[-]?tv|hbbtv|appletv|googletv|hdmi|netcast\.tv|viera|nettv|roku|\bdtv\b|sonydtv|inettvbrowser|\btv\b)/i).test(global.navigator.userAgent);
}

function createRequestParams(bid) {
let size = getSize(bid.sizes);
let isVideo = bid.mediaTypes && bid.mediaTypes.video;
function isVideoBid(bid) {
return bid.mediaTypes && bid.mediaTypes.video;
}

function createVideoRequestData(bid) {
let size = getFirstSize(bid);
let topLocation = utils.getTopWindowLocation();
return {
isPrebid: true,
appId: bid.params.appId,
domain: document.location.hostname,
id: utils.getUniqueIdentifierStr(),
imp: [{
[ isVideo ? VIDEO : BANNER ]: {
w: size.width,
h: size.height
video: {
w: size.w,
h: size.h
},
bidfloor: bid.params.bidfloor
}],
site: {
page: utils.getTopWindowLocation().host
page: topLocation.host
},
device: {
ua: global.navigator.userAgent,
Expand All @@ -112,4 +145,29 @@ function createRequestParams(bid) {
};
}

function createBannerRequestData(bids) {
let topLocation = utils.getTopWindowLocation();
let referrer = utils.getTopWindowReferrer();
let slots = bids.map(bid => {
return {
slot: bid.adUnitCode,
id: bid.params.appId,
bidfloor: bid.params.bidfloor,
sizes: getSizes(bid)
};
});
return {
slots: slots,
page: topLocation.href,
domain: topLocation.hostname,
search: topLocation.search,
referrer: referrer,
ua: global.navigator.userAgent,
isMobile: isMobile() ? 1 : 0,
dnt: 0,
adapterVersion: ADAPTER_VERSION,
adapterName: ADAPTER_NAME
};
}

registerBidder(spec);
Loading