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 Adapter - Added debugging and analytics query params. #2463

Merged
merged 10 commits into from
Apr 30, 2018
57 changes: 45 additions & 12 deletions modules/sonobiBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import * as utils from 'src/utils';
import { getTopWindowLocation, parseSizesInput, logError, generateUUID, deepAccess, isEmpty } from '../src/utils';
import { BANNER, VIDEO } from '../src/mediaTypes';
import find from 'core-js/library/fn/array/find';

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

export const spec = {
code: BIDDER_CODE,
Expand Down Expand Up @@ -37,7 +36,7 @@ export const spec = {
[bid.bidId]: `${slotIdentifier}|${_validateSize(bid)}${_validateFloor(bid)}`
}
} else {
utils.logError(`The ad unit code or Sonobi Placement id for slot ${bid.bidId} is invalid`);
logError(`The ad unit code or Sonobi Placement id for slot ${bid.bidId} is invalid`);
}
});

Expand All @@ -46,15 +45,23 @@ export const spec = {

const payload = {
'key_maker': JSON.stringify(data),
'ref': utils.getTopWindowLocation().host,
's': utils.generateUUID(),
'ref': getTopWindowLocation().host,
's': generateUUID(),
'pv': PAGEVIEW_ID,
'vp': _getPlatform(),
'lib_name': 'prebid',
'lib_v': '$prebid.version$'
};

if (validBidRequests[0].params.hfa) {
payload.hfa = validBidRequests[0].params.hfa;
}

// If there is no key_maker data, then dont make the request.
if (isEmpty(data)) {
return null;
}

return {
method: 'GET',
url: STR_ENDPOINT,
Expand All @@ -80,8 +87,8 @@ export const spec = {

Object.keys(bidResponse.slots).forEach(slot => {
const bidId = _getBidIdFromTrinityKey(slot);
const bidRequest = find(bidderRequests, bidReqest => bidReqest.bidId === bidId);
const videoMediaType = utils.deepAccess(bidRequest, 'mediaTypes.video');
const bidRequest = bidderRequests.find(bidReqest => bidReqest.bidId === bidId);
const videoMediaType = deepAccess(bidRequest, 'mediaTypes.video');
const mediaType = bidRequest.mediaType || (videoMediaType ? 'video' : null);
const createCreative = _creative(mediaType);
const bid = bidResponse.slots[slot];
Expand Down Expand Up @@ -138,9 +145,9 @@ export const spec = {

function _validateSize (bid) {
if (bid.params.sizes) {
return utils.parseSizesInput(bid.params.sizes).join(',');
return parseSizesInput(bid.params.sizes).join(',');
}
return utils.parseSizesInput(bid.sizes).join(',');
return parseSizesInput(bid.sizes).join(',');
}

function _validateSlot (bid) {
Expand All @@ -161,16 +168,42 @@ 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=' + utils.getTopWindowLocation().host;
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=${utils.getTopWindowLocation().host}`
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]
}

/**
* @param context - the window to determine the innerWidth from. This is purely for test purposes as it should always be the current window
*/
export const _isInbounds = (context = window) => (lowerBound = 0, upperBound = Number.MAX_SAFE_INTEGER) => context.innerWidth >= lowerBound && context.innerWidth < upperBound;

/**
* @param context - the window to determine the innerWidth from. This is purely for test purposes as it should always be the current window
*/
export function _getPlatform(context = window) {
const isInBounds = _isInbounds(context);
const MOBILE_VIEWPORT = {
lt: 768
};
const TABLET_VIEWPORT = {
lt: 992,
ge: 768
};
if (isInBounds(0, MOBILE_VIEWPORT.lt)) {
return 'mobile'
}
if (isInBounds(TABLET_VIEWPORT.ge, TABLET_VIEWPORT.lt)) {
return 'tablet'
}
return 'desktop';
}

registerBidder(spec);
18 changes: 17 additions & 1 deletion test/spec/modules/sonobiBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { spec } from 'modules/sonobiBidAdapter'
import { spec, _getPlatform } from 'modules/sonobiBidAdapter'
import { newBidder } from 'src/adapters/bidderFactory'

describe('SonobiBidAdapter', () => {
Expand Down Expand Up @@ -139,6 +139,7 @@ describe('SonobiBidAdapter', () => {
expect(bidRequests.data.pv).to.equal(bidRequestsPageViewID.data.pv)
expect(bidRequests.data.hfa).to.not.exist
expect(bidRequests.bidderRequests).to.eql(bidRequest);
expect(bidRequests.data.vp).to.equal('tablet');
})

it('should return a properly formatted request with hfa', () => {
Expand All @@ -151,6 +152,10 @@ describe('SonobiBidAdapter', () => {
expect(bidRequests.data.s).not.to.be.empty
expect(bidRequests.data.hfa).to.equal('hfakey')
})
it('should return null if there is nothing to bid on', () => {
const bidRequests = spec.buildRequests([{params: {}}])
expect(bidRequests).to.equal(null);
})
})

describe('.interpretResponse', () => {
Expand Down Expand Up @@ -287,4 +292,15 @@ describe('SonobiBidAdapter', () => {
expect(spec.getUserSyncs({ pixelEnabled: false }, bidResponse)).to.have.length(0);
})
})
describe('_getPlatform', () => {
it('should return mobile', () => {
expect(_getPlatform({innerWidth: 767})).to.equal('mobile')
})
it('should return tablet', () => {
expect(_getPlatform({innerWidth: 800})).to.equal('tablet')
})
it('should return desktop', () => {
expect(_getPlatform({innerWidth: 1000})).to.equal('desktop')
})
})
})