Skip to content

Commit

Permalink
Onetag bid Adapter: added video support. (#4986)
Browse files Browse the repository at this point in the history
* Cambia nome variabile us privacy in getUserSyncs

* Onetag bid adapter: Added video support and minor code refactoring

* Fix for linter

* Onetag Bid Adapter: code refactoring, added testing params and updated adapter tests

* Onetag Bid Adapter: minor code refactoring

Co-authored-by: valentino <[email protected]>
Co-authored-by: Nicola <ciao@ciao>
  • Loading branch information
3 people authored Mar 26, 2020
1 parent 7c45626 commit b9676bf
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 114 deletions.
200 changes: 140 additions & 60 deletions modules/onetagBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
'use strict';

import { BANNER, VIDEO } from '../src/mediaTypes.js';
const { registerBidder } = require('../src/adapters/bidderFactory.js');

const ENDPOINT = 'https://onetag-sys.com/prebid-request';
const USER_SYNC_ENDPOINT = 'https://onetag-sys.com/usync/';
const BIDDER_CODE = 'onetag';
const BANNER = 'banner';

// =======
// Object BidRequest
//
// .params
// required .pubId: string
// optional .type: "BANNER" | "VIDEO" | "NATIVE" --> only BANNER at present

/**
* Determines whether or not the given bid request is valid.
Expand All @@ -21,15 +14,28 @@ const BANNER = 'banner';
* @return boolean True if this is a valid bid, and false otherwise.
*/
function isBidRequestValid(bid) {
if (typeof bid === 'undefined' || bid.bidder !== BIDDER_CODE || typeof bid.params === 'undefined') {
if (typeof bid === 'undefined' || typeof bid.params === 'undefined' || typeof bid.params.pubId !== 'string') {
return false;
}
return isValid(BANNER, bid) || isValid(VIDEO, bid);
}

if (typeof bid.params.pubId !== 'string' || typeof bid.sizes === 'undefined' || bid.sizes.length === 0) {
return false;
}
export function hasTypeVideo(bid) {
return typeof bid.mediaTypes !== 'undefined' && typeof bid.mediaTypes.video !== 'undefined';
}

return true;
export function isValid(type, bid) {
if (type === BANNER) {
return parseSizes(bid).length > 0;
} else if (type === VIDEO && hasTypeVideo(bid)) {
const context = bid.mediaTypes.video.context;
if (context === 'outstream') {
return parseVideoSize(bid).length > 0 && typeof bid.renderer !== 'undefined' && typeof bid.renderer.render !== 'undefined' && typeof bid.renderer.url !== 'undefined';
} else if (context === 'instream') {
return parseVideoSize(bid).length > 0;
}
}
return false;
}

/**
Expand All @@ -40,7 +46,7 @@ function isBidRequestValid(bid) {
*/

function buildRequests(validBidRequests, bidderRequest) {
const bids = validBidRequests.map(requestsToBids);
const bids = requestsToBids(validBidRequests);
const bidObject = {'bids': bids};
const pageInfo = getPageInfo();

Expand All @@ -57,6 +63,10 @@ function buildRequests(validBidRequests, bidderRequest) {
payload.usPrivacy = bidderRequest.uspConsent;
}

if (bidderRequest && bidderRequest.userId) {
payload.userId = bidderRequest.userId;
}

const payloadString = JSON.stringify(payload);

return {
Expand All @@ -81,25 +91,33 @@ function interpretResponse(serverResponse, request) {
if (!body || (body.nobid && body.nobid === true)) {
return bids;
}

if (body.bids) {
body.bids.forEach(function(bid) {
bids.push({
requestId: bid.requestId,
cpm: bid.cpm,
width: bid.width,
height: bid.height,
creativeId: bid.creativeId,
dealId: bid.dealId ? bid.dealId : '',
currency: bid.currency,
netRevenue: false,
mediaType: bids.type ? bids.type : BANNER,
ad: bid.ad,
ttl: bid.ttl || 300
});
});
if (!body.bids || !Array.isArray(body.bids) || body.bids.length === 0) {
return bids;
}

body.bids.forEach(function(bid) {
let responseBid = {
requestId: bid.requestId,
cpm: bid.cpm,
width: bid.width,
height: bid.height,
creativeId: bid.creativeId,
dealId: bid.dealId ? bid.dealId : '',
currency: bid.currency,
netRevenue: false,
mediaType: bid.mediaType,
ttl: bid.ttl || 300
};

if (bid.mediaType === BANNER) {
responseBid.ad = bid.ad;
} else if (bid.mediaType === VIDEO) {
responseBid.vastXml = bid.ad;
}

bids.push(responseBid);
});

return bids;
}

Expand All @@ -117,9 +135,30 @@ function getPageInfo() {
break
}
}

const params = {

let isDocHidden;
let xOffset;
let yOffset;
try {
if (typeof w.document.hidden !== 'undefined') {
isDocHidden = w.document.hidden;
} else if (typeof w.document['msHidden'] !== 'undefined') {
isDocHidden = w.document['msHidden'];
} else if (typeof w.document['webkitHidden'] !== 'undefined') {
isDocHidden = w.document['webkitHidden'];
} else {
isDocHidden = null;
}
} catch (e) {
isDocHidden = null;
}
try {
xOffset = w.pageXOffset;
yOffset = w.pageYOffset;
} catch (e) {
xOffset = null;
yOffset = null;
}
return {
location: e(l),
referrer: e(r) || '0',
masked: m,
Expand All @@ -133,45 +172,86 @@ function getPageInfo() {
aHeight: s.availHeight,
sLeft: 'screenLeft' in w ? w.screenLeft : w.screenX,
sTop: 'screenTop' in w ? w.screenTop : w.screenY,
xOffset: xOffset,
yOffset: yOffset,
docHidden: isDocHidden,
hLength: history.length,
date: t.toUTCString(),
timeOffset: t.getTimezoneOffset()
};

return params;
}

function requestsToBids(bid) {
const toRet = {};

const params = bid.params;
function requestsToBids(bidRequests) {
const videoBidRequests = bidRequests.filter(bidRequest => hasTypeVideo(bidRequest)).map(bidRequest => {
const videoObj = {};
setGeneralInfo.call(videoObj, bidRequest);
// Pass parameters
// Context: instream - outstream - adpod
videoObj['context'] = bidRequest.mediaTypes.video.context;
// MIME Video Types
videoObj['mimes'] = bidRequest.mediaTypes.video.mimes;
// Sizes
videoObj['playerSize'] = parseVideoSize(bidRequest);
// Other params
videoObj['protocols'] = bidRequest.mediaTypes.video.protocols;
videoObj['maxDuration'] = bidRequest.mediaTypes.video.maxduration;
videoObj['api'] = bidRequest.mediaTypes.video.api;
videoObj['type'] = VIDEO;
return videoObj;
});
const bannerBidRequests = bidRequests.filter(bidRequest => isValid(BANNER, bidRequest)).map(bidRequest => {
const bannerObj = {};
setGeneralInfo.call(bannerObj, bidRequest);
bannerObj['sizes'] = parseSizes(bidRequest);
bannerObj['type'] = BANNER;
return bannerObj;
});
return videoBidRequests.concat(bannerBidRequests);
}

toRet['adUnitCode'] = bid.adUnitCode;
toRet['bidId'] = bid.bidId;
toRet['bidderRequestId'] = bid.bidderRequestId;
toRet['auctionId'] = bid.auctionId;
toRet['transactionId'] = bid.transactionId;
toRet['sizes'] = [];
const sizes = bid.sizes;
for (let i = 0, lenght = sizes.length; i < lenght; i++) {
const size = sizes[i];
toRet['sizes'].push({width: size[0], height: size[1]})
function setGeneralInfo(bidRequest) {
const params = bidRequest.params;
this['adUnitCode'] = bidRequest.adUnitCode;
this['bidId'] = bidRequest.bidId;
this['bidderRequestId'] = bidRequest.bidderRequestId;
this['auctionId'] = bidRequest.auctionId;
this['transactionId'] = bidRequest.transactionId;
this['pubId'] = params.pubId;
if (params.pubClick) {
this['click'] = params.pubClick;
}

toRet['pubId'] = params.pubId;
if (params.type) {
toRet['type'] = params.type;
if (params.dealId) {
this['dealId'] = params.dealId;
}
}

if (params.pubClick) {
toRet['click'] = params.pubClick;
function parseVideoSize(bid) {
const playerSize = bid.mediaTypes.video.playerSize;
if (typeof playerSize !== 'undefined' && Array.isArray(playerSize) && playerSize.length > 0) {
return getSizes(playerSize)
}
return [];
}

if (params.dealId) {
toRet['dealId'] = params.dealId;
function parseSizes(bid) {
let ret = [];
if (typeof bid.mediaTypes !== 'undefined' && typeof bid.mediaTypes.banner !== 'undefined' && typeof bid.mediaTypes.banner.sizes !== 'undefined' && Array.isArray(bid.mediaTypes.banner.sizes) && bid.mediaTypes.banner.sizes.length > 0) {
return getSizes(bid.mediaTypes.banner.sizes)
}
const isVideoBidRequest = hasTypeVideo(bid);
if (!isVideoBidRequest && bid.sizes && Array.isArray(bid.sizes)) {
return getSizes(bid.sizes);
}
return ret;
}

return toRet;
function getSizes(sizes) {
const ret = [];
for (let i = 0, lenght = sizes.length; i < lenght; i++) {
const size = sizes[i];
ret.push({width: size[0], height: size[1]})
}
return ret;
}

function getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
Expand Down Expand Up @@ -202,7 +282,7 @@ function getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
export const spec = {

code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
supportedMediaTypes: [BANNER, VIDEO],

isBidRequestValid: isBidRequestValid,
buildRequests: buildRequests,
Expand Down
34 changes: 23 additions & 11 deletions modules/onetagBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@ Maintainer: [email protected]

# Description

OneTag Bid Adapter supports only banner at present.
OneTag Bid Adapter supports banner and video at present.

# Test Parameters
```
var adUnits = [
{
code: "test-div",
code: "banner-space",
mediaTypes: {
banner: {
sizes: [[300, 250]]
}
},
bids: [
{
bidder: "onetag",
params: {
pubId: "your_publisher_id", // required
type: "banner" // optional. Default "banner"
},
bids: [{
bidder: "onetag",
params: {
pubId: "your_publisher_id" // required, testing pubId: "386276e072"
}
]
}];
}]
}, {
code: 'video-space',
mediaTypes: {
video: {
context: "instream",
mimes: ["video/mp4", "video/webm", "application/javascript", "video/ogg"],
playerSize: [640,480]
}
},
bids: [{
bidder: "onetag",
params: {
pubId: "your_publisher_id" // required, testing pubId: "386276e072"
}
}]
}];
```
Loading

0 comments on commit b9676bf

Please sign in to comment.