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

Sonobi Video Support #2297

Merged
merged 7 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 42 additions & 12 deletions modules/sonobiBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import { getTopWindowLocation, parseSizesInput } from 'src/utils';
import * as utils from '../src/utils';
import { BANNER, VIDEO } from '../src/mediaTypes';

const BIDDER_CODE = 'sonobi';
const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
const PAGEVIEW_ID = utils.generateUUID();

export const spec = {
code: BIDDER_CODE,

supportedMediaTypes: [BANNER, VIDEO],
/**
* Determines whether or not the given bid request is valid.
*
Expand All @@ -25,7 +26,7 @@ export const spec = {
*/
buildRequests: (validBidRequests) => {
const bids = validBidRequests.map(bid => {
let slotIdentifier = _validateSlot(bid)
let slotIdentifier = _validateSlot(bid);
if (/^[\/]?[\d]+[[\/].+[\/]?]?$/.test(slotIdentifier)) {
slotIdentifier = slotIdentifier.charAt(0) === '/' ? slotIdentifier : '/' + slotIdentifier
return {
Expand Down Expand Up @@ -58,16 +59,18 @@ export const spec = {
method: 'GET',
url: STR_ENDPOINT,
withCredentials: true,
data: payload
data: payload,
bidderRequests: validBidRequests
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @param {*} bidderRequests - Info describing the request to the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: (serverResponse) => {
interpretResponse: (serverResponse, { bidderRequests }) => {
const bidResponse = serverResponse.body;
const bidsReturned = [];

Expand All @@ -76,25 +79,41 @@ export const spec = {
}

Object.keys(bidResponse.slots).forEach(slot => {
const bidId = _getBidIdFromTrinityKey(slot);
const bidRequest = bidderRequests.find(bidReqest => bidReqest.bidId === bidId);
const videoMediaType = utils.deepAccess(bidRequest, 'mediaTypes.video');
const mediaType = bidRequest.mediaType || (videoMediaType ? 'video' : null);
const createCreative = _creative(mediaType);
const bid = bidResponse.slots[slot];

if (bid.sbi_aid && bid.sbi_mouse && bid.sbi_size) {
const [
width = 1,
height = 1
] = bid.sbi_size.split('x');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

const bids = {
requestId: slot.split('|').slice(-1)[0],
requestId: bidId,
cpm: Number(bid.sbi_mouse),
width: Number(bid.sbi_size.split('x')[0]) || 1,
height: Number(bid.sbi_size.split('x')[1]) || 1,
ad: _creative(bidResponse.sbi_dc, bid.sbi_aid),
width: Number(width),
height: Number(height),
ad: createCreative(bidResponse.sbi_dc, bid.sbi_aid),
ttl: 500,
creativeId: bid.sbi_aid,
netRevenue: true,
currency: 'USD',
currency: 'USD'
};

if (bid.sbi_dozer) {
bids.dealId = bid.sbi_dozer;
}

const creativeType = bid.sbi_ct;
if (creativeType && (creativeType === 'video' || creativeType === 'outstream')) {
bids.mediaType = 'video';
bids.vastUrl = createCreative(bidResponse.sbi_dc, bid.sbi_aid);
delete bids.ad;
delete bids.width;
delete bids.height;
}
bidsReturned.push(bids);
}
});
Expand Down Expand Up @@ -138,9 +157,20 @@ function _validateFloor (bid) {
return '';
}

function _creative (sbi_dc, sbi_aid) {
const src = 'https://' + sbi_dc + 'apex.go.sonobi.com/sbi.js?aid=' + sbi_aid + '&as=null';
const _creative = (mediaType) => (sbi_dc, sbi_aid) => {
if (mediaType === 'video') {
return _videoCreative(sbi_dc, sbi_aid)
}
const src = 'https://' + sbi_dc + 'apex.go.sonobi.com/sbi.js?aid=' + sbi_aid + '&as=null' + '&ref=' + getTopWindowLocation().host;
return '<script type="text/javascript" src="' + src + '"></script>';
}

function _videoCreative(sbi_dc, sbi_aid) {
return `https://${sbi_dc}apex.go.sonobi.com/vast.xml?vid=${sbi_aid}&ref=${getTopWindowLocation().host}`
}

function _getBidIdFromTrinityKey (key) {
return key.split('|').slice(-1)[0]
}

registerBidder(spec);
38 changes: 38 additions & 0 deletions modules/sonobiBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,41 @@ Module that connects to Sonobi's demand sources.
}
];
```

# Video Test Parameters
```
var videoAdUnit = {
code: 'adUnit_af',
sizes: [640,480],
mediaTypes: {
video: {context: 'instream'}
},
bids: [
{
bidder: 'sonobi',
params: {
placement_id: '92e95368e86639dbd86d',
}
}
]
};
```

Example bidsBackHandler for video bids
```
pbjs.requestBids({
timeout : 700,
bidsBackHandler : function(bids) {
var videoUrl = pbjs.adServers.dfp.buildVideoUrl({
adUnit: videoAdUnit,
params: {
cust_params: {
hb_vid: bids.adUnit_af.bids[0].creativeId
},
iu: '/7780971/apex_jwplayer_video'
}
});
invokeVideoPlayer(videoUrl);
}
});
```
99 changes: 73 additions & 26 deletions test/spec/modules/sonobiBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ describe('SonobiBidAdapter', () => {
expect(bidRequests.data.s).not.to.be.empty
expect(bidRequests.data.pv).to.equal(bidRequestsPageViewID.data.pv)
expect(bidRequests.data.hfa).to.not.exist
expect(bidRequests.bidderRequests).to.eql(bidRequest);
})

it('should return a properly formatted request with hfa', () => {
Expand All @@ -153,23 +154,66 @@ describe('SonobiBidAdapter', () => {
})

describe('.interpretResponse', () => {
const bidRequests = {
'method': 'GET',
'url': 'https://apex.go.sonobi.com/trinity.json',
'withCredentials': true,
'data': {
'key_maker': '{"30b31c1838de1f":"1a2b3c4d5e6f1a2b3c4d|300x250,300x600|f=1.25","/7780971/sparks_prebid_LB|30b31c1838de1e":"300x250,300x600"}', 'ref': 'localhost:9876', 's': '2474372d-c0ff-4f46-aef4-a173058403d9', 'pv': 'c9cfc207-cd83-4a01-b591-8bb29389d4b0'
},
'bidderRequests': [
{
'bidder': 'sonobi',
'params': {
'ad_unit': '/7780971/sparks_prebid_LB',
'sizes': [[300, 250], [300, 600]],
'floor': '1.25'
},
'adUnitCode': 'adunit-code-1',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1f'
},
{
'bidder': 'sonobi',
'params': {
'placement_id': '1a2b3c4d5e6f1a2b3c4d',
'sizes': [[300, 250], [300, 600]]
},
'adUnitCode': 'adunit-code-2',
'sizes': [[120, 600], [300, 600], [160, 600]],
'bidId': '30b31c1838de1e',
'mediaType': 'video'
},
{
'bidder': 'sonobi',
'params': {
'ad_unit': '/7780971/sparks_prebid_LB',
'sizes': [[300, 250], [300, 600]]
},
'adUnitCode': 'adunit-code-3',
'sizes': [[120, 600], [300, 600], [160, 600]],
'bidId': '30b31c1838de1g'
}
]
};

let bidResponse = {
'body': {
'slots': {
'/7780971/sparks_prebid_LB|30b31c1838de1d': {
'/7780971/sparks_prebid_LB|30b31c1838de1f': {
'sbi_size': '300x600',
'sbi_apoc': 'remnant',
'sbi_aid': '30292e432662bd5f86d90774b944b039',
'sbi_mouse': 1.07,
},
'30b31c1838de1f': {
'30b31c1838de1e': {
'sbi_size': '300x250',
'sbi_apoc': 'remnant',
'sbi_aid': '30292e432662bd5f86d90774b944b038',
'sbi_mouse': 1.25,
'sbi_dozer': 'dozerkey',
},
'30b31c1838de1e': {},
'/7780971/sparks_prebid_LB|30b31c1838de1g': {},
},
'sbi_dc': 'mco-1-',
'sbi_px': [{
Expand All @@ -182,31 +226,34 @@ describe('SonobiBidAdapter', () => {
}
};

let prebidResponse = [{
'requestId': '30b31c1838de1d',
'cpm': 1.07,
'width': 300,
'height': 600,
'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null"></script>',
'ttl': 500,
'creativeId': '30292e432662bd5f86d90774b944b039',
'netRevenue': true,
'currency': 'USD'
}, {
'requestId': '30b31c1838de1f',
'cpm': 1.25,
'width': 300,
'height': 250,
'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b038&as=null"></script>',
'ttl': 500,
'creativeId': '30292e432662bd5f86d90774b944b038',
'netRevenue': true,
'currency': 'USD',
'dealId': 'dozerkey'
}];
let prebidResponse = [
{
'requestId': '30b31c1838de1f',
'cpm': 1.07,
'width': 300,
'height': 600,
'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null&ref=localhost:9876"></script>',
'ttl': 500,
'creativeId': '30292e432662bd5f86d90774b944b039',
'netRevenue': true,
'currency': 'USD'
},
{
'requestId': '30b31c1838de1e',
'cpm': 1.25,
'width': 300,
'height': 250,
'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=localhost:9876',
'ttl': 500,
'creativeId': '30292e432662bd5f86d90774b944b038',
'netRevenue': true,
'currency': 'USD',
'dealId': 'dozerkey'
}
];

it('should map bidResponse to prebidResponse', () => {
const response = spec.interpretResponse(bidResponse);
const response = spec.interpretResponse(bidResponse, bidRequests);
expect(response).to.deep.equal(prebidResponse);
})
})
Expand Down