Skip to content

Sonobi - Outstream Support #3559

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

Merged
merged 13 commits into from
Feb 14, 2019
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
84 changes: 80 additions & 4 deletions modules/sonobiBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { registerBidder } from '../src/adapters/bidderFactory';
import { parseSizesInput, logError, generateUUID, isEmpty, deepAccess } from '../src/utils';
import { parseSizesInput, logError, generateUUID, isEmpty, deepAccess, logWarn, logMessage } from '../src/utils';
import { BANNER, VIDEO } from '../src/mediaTypes';
import { config } from '../src/config';
import { Renderer } from '../src/Renderer';

const BIDDER_CODE = 'sonobi';
const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
const PAGEVIEW_ID = generateUUID();
const SONOBI_DIGITRUST_KEY = 'fhnS5drwmH';
const OUTSTREAM_REDNERER_URL = 'https://mtrx.go.sonobi.com/sbi_outstream_renderer.js';

export const spec = {
code: BIDDER_CODE,
Expand Down Expand Up @@ -107,15 +109,23 @@ export const spec = {
const bidResponse = serverResponse.body;
const bidsReturned = [];
const referrer = bidderRequest.data.ref;

if (Object.keys(bidResponse.slots).length === 0) {
return bidsReturned;
}

Object.keys(bidResponse.slots).forEach(slot => {
const bid = bidResponse.slots[slot];
const bidId = _getBidIdFromTrinityKey(slot);
const mediaType = (bid.sbi_ct === 'video') ? 'video' : null;
const bidRequest = _findBidderRequest(bidderRequest.bidderRequests, bidId);
let mediaType = null;
if (bid.sbi_ct === 'video') {
mediaType = 'video';
const context = deepAccess(bidRequest, 'mediaTypes.video.context');
if (context === 'outstream') {
mediaType = 'outstream';
}
}

const createCreative = _creative(mediaType, referrer);
if (bid.sbi_aid && bid.sbi_mouse && bid.sbi_size) {
const [
Expand Down Expand Up @@ -145,6 +155,21 @@ export const spec = {
delete bids.ad;
delete bids.width;
delete bids.height;
} else if (mediaType === 'outstream' && bidRequest) {
bids.mediaType = 'video';
bids.vastUrl = createCreative(bidResponse.sbi_dc, bid.sbi_aid);
bids.renderer = newRenderer(bidRequest.adUnitCode, bids, deepAccess(
bidRequest,
'renderer.options'
));
let videoSize = deepAccess(bidRequest, 'params.sizes');
if (Array.isArray(videoSize) && Array.isArray(videoSize[0])) { // handle case of multiple sizes
videoSize = videoSize[0] // Only take the first size for outstream
}
if (videoSize) {
bids.width = videoSize[0];
bids.height = videoSize[1];
}
}
bidsReturned.push(bids);
}
Expand All @@ -170,6 +195,14 @@ export const spec = {
}
};

function _findBidderRequest(bidderRequests, bidId) {
for (let i = 0; i < bidderRequests.length; i++) {
if (bidderRequests[i].bidId === bidId) {
return bidderRequests[i];
}
}
}

function _validateSize (bid) {
if (bid.params.sizes) {
return parseSizesInput(bid.params.sizes).join(',');
Expand All @@ -192,7 +225,7 @@ function _validateFloor (bid) {
}

const _creative = (mediaType, referer) => (sbiDc, sbiAid) => {
if (mediaType === 'video') {
if (mediaType === 'video' || mediaType === 'outstream') {
return _videoCreative(sbiDc, sbiAid, referer)
}
const src = `https://${sbiDc}apex.go.sonobi.com/sbi.js?aid=${sbiAid}&as=null&ref=${encodeURIComponent(referer)}`;
Expand Down Expand Up @@ -247,4 +280,47 @@ function _getDigiTrustObject(key) {
return digiTrustId;
}

function newRenderer(adUnitCode, bid, rendererOptions = {}) {
const renderer = Renderer.install({
id: bid.aid,
url: OUTSTREAM_REDNERER_URL,
config: rendererOptions,
loaded: false,
adUnitCode
});

try {
renderer.setRender(outstreamRender);
} catch (err) {
logWarn('Prebid Error calling setRender on renderer', err);
}

renderer.setEventHandlers({
impression: () => logMessage('Sonobi outstream video impression event'),
loaded: () => logMessage('Sonobi outstream video loaded event'),
ended: () => {
logMessage('Sonobi outstream renderer video event');
// document.querySelector(`#${adUnitCode}`).style.display = 'none';
}
});
return renderer;
}

function outstreamRender(bid) {
// push to render queue because SbiOutstreamRenderer may not be loaded yet
bid.renderer.push(() => {
const [
width,
height
] = bid.getSize().split('x');
const renderer = new window.SbiOutstreamRenderer();
renderer.init({
vastUrl: bid.vastUrl,
height,
width,
});
renderer.setRootElement(bid.adUnitCode);
});
}

registerBidder(spec);
52 changes: 49 additions & 3 deletions test/spec/modules/sonobiBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ describe('SonobiBidAdapter', function () {
'adUnitCode': 'adunit-code-3',
'sizes': [[120, 600], [300, 600], [160, 600]],
'bidId': '30b31c1838de1g'
},
{
'bidId': '30b31c1838de1zzzz',
'adUnitCode': 'outstream-dom-id',
bidder: 'sonobi',
mediaTypes: {
video: {
context: 'outstream'
}
},
params: {
placement_id: '92e95368e86639dbd86d',
sizes: [[640, 480]]
}
}
]
};
Expand Down Expand Up @@ -374,6 +388,17 @@ describe('SonobiBidAdapter', function () {
'sbi_mouse': 1.07,
},
'/7780971/sparks_prebid_LB|30b31c1838de1g': {},
'30b31c1838de1zzzz': {
sbi_aid: 'force_1550072228_da1c5d030cb49150c5db8a2136175755',
sbi_apoc: 'premium',
sbi_ct: 'video',
sbi_curr: 'USD',
sbi_mouse: 1.25,
sbi_size: 'preroll',
'sbi_crid': 'somecrid',

}

},
'sbi_dc': 'mco-1-',
'sbi_px': [{
Expand Down Expand Up @@ -404,13 +429,14 @@ describe('SonobiBidAdapter', function () {
'cpm': 1.25,
'width': 300,
'height': 250,
'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=http%3A%2F%2Flocalhost%2F',
'vastUrl': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=http%3A%2F%2Flocalhost%2F',
'ttl': 500,
'creativeId': '30292e432662bd5f86d90774b944b038',
'netRevenue': true,
'currency': 'USD',
'dealId': 'dozerkey',
'aid': '30292e432662bd5f86d90774b944b038'
'aid': '30292e432662bd5f86d90774b944b038',
'mediaType': 'video'
},
{
'requestId': '30b31c1838de1g',
Expand All @@ -424,6 +450,21 @@ describe('SonobiBidAdapter', function () {
'currency': 'USD',
'aid': '30292e432662bd5f86d90774b944b038'
},
{
'requestId': '30b31c1838de1zzzz',
'cpm': 1.25,
'width': 640,
'height': 480,
'vastUrl': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=http%3A%2F%2Flocalhost%2F',
'ttl': 500,
'creativeId': 'somecrid',
'netRevenue': true,
'currency': 'USD',
'dealId': 'dozerkey',
'aid': 'force_1550072228_da1c5d030cb49150c5db8a2136175755',
'mediaType': 'video',
renderer: () => {}
},
];

it('should map bidResponse to prebidResponse', function () {
Expand All @@ -437,7 +478,12 @@ describe('SonobiBidAdapter', function () {
expect(resp.netRevenue).to.equal(prebidResponse[i].netRevenue);
expect(resp.currency).to.equal(prebidResponse[i].currency);
expect(resp.aid).to.equal(prebidResponse[i].aid);
if (resp.mediaType === 'video') {
if (resp.mediaType === 'video' && resp.renderer) {
expect(resp.vastUrl.indexOf('vast.xml')).to.be.greaterThan(0);
expect(resp.width).to.equal(prebidResponse[i].width);
expect(resp.height).to.equal(prebidResponse[i].height);
expect(resp.renderer).to.be.ok;
} else if (resp.mediaType === 'video') {
expect(resp.vastUrl.indexOf('vast.xml')).to.be.greaterThan(0);
expect(resp.ad).to.be.undefined;
expect(resp.width).to.be.undefined;
Expand Down