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

Smartyads Adapter #895

Merged
merged 9 commits into from
Jan 18, 2017
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
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"pulsepoint",
"rhythmone",
"rubicon",
"smartyads",
"smartadserver",
"sekindoUM",
"sonobi",
Expand Down
8 changes: 7 additions & 1 deletion integrationExamples/gpt/pbjs_example_gpt.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,17 @@
}
},
{
bidder: 'widespace',
bidder: 'widespace',
params: {
sid: '7b6589bf-95c8-4656-90b9-af9737bb9ad3',
cur: 'EUR'
}
},
{
bidder: 'smartyads',
params: {
banner_id: 0
}
}
]
}, {
Expand Down
191 changes: 191 additions & 0 deletions src/adapters/smartyads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import * as Adapter from './adapter.js';
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import {ajax} from 'src/ajax';
import {STATUS} from 'src/constants';

const SMARTYADS_BIDDER_CODE = 'smartyads';

var sizeMap = {
1: '468x60',
2: '728x90',
8: '120x600',
9: '160x600',
10: '300x600',
15: '300x250',
16: '336x280',
19: '300x100',
43: '320x50',
44: '300x50',
48: '300x300',
54: '300x1050',
55: '970x90',
57: '970x250',
58: '1000x90',
59: '320x80',
61: '1000x1000',
65: '640x480',
67: '320x480',
68: '1800x1000',
72: '320x320',
73: '320x160',
83: '480x300',
94: '970x310',
96: '970x210',
101: '480x320',
102: '768x1024',
113: '1000x300',
117: '320x100',
125: '800x250',
126: '200x600'
};

utils._each(sizeMap, (item, key) => sizeMap[item] = key);

function SmartyadsAdapter() {


function _callBids(bidderRequest) {

var bids = bidderRequest.bids || [];

bids.forEach((bid) => {
try {
ajax(buildOptimizedCall(bid), bidCallback, undefined, {withCredentials: true});
} catch (err) {
utils.logError('Error sending smartyads request for placement code ' + bid.placementCode, null, err);
}

function bidCallback(responseText) {

try {
utils.logMessage('XHR callback function called for ad ID: ' + bid.bidId);
handleRpCB(responseText, bid);
} catch (err) {
if (typeof err === "string") {
utils.logWarn(`${err} when processing smartyads response for placement code ${bid.placementCode}`);
} else {
utils.logError('Error processing smartyads response for placement code ' + bid.placementCode, null, err);
}

//indicate that there is no bid for this placement
let badBid = bidfactory.createBid(STATUS.NO_BID, bid);
badBid.bidderCode = bid.bidder;
badBid.error = err;
bidmanager.addBidResponse(bid.placementCode, badBid);
}
}
});
}


function buildOptimizedCall(bid) {

bid.startTime = new Date().getTime();

// use smartyads sizes if provided, otherwise adUnit.sizes
var parsedSizes = SmartyadsAdapter.masSizeOrdering(
Array.isArray(bid.params.sizes) ? bid.params.sizes.map(size => (sizeMap[size] || '').split('x')) : bid.sizes
);

if (parsedSizes.length < 1) {
throw "no valid sizes";
}

var secure;
if (window.location.protocol !== 'http:') {
secure = 1;
} else {
secure = 0;
}

var host = window.location.host,
page = window.location.pathname,
language = navigator.language,
deviceWidth = window.screen.width,
deviceHeight = window.screen.height;

var queryString = [
'banner_id', bid.params.banner_id,
'size_ad', parsedSizes[0],
'alt_size_ad', parsedSizes.slice(1).join(',') || undefined,
'host', host,
"page", page,
"language", language,
"deviceWidth", deviceWidth,
"deviceHeight", deviceHeight,
"secure", secure,
"bidId", bid.bidId,
"checkOn", 'rf'
];

return queryString.reduce(
(memo, curr, index) =>
index % 2 === 0 && queryString[index + 1] !== undefined ?
memo + curr + '=' + encodeURIComponent(queryString[index + 1]) + '&'
: memo,
'//ssp-nj.webtradehub.com/?'
).slice(0, -1);

}

function handleRpCB(responseText, bidRequest) {

let ad = JSON.parse(responseText); // can throw

var bid = bidfactory.createBid(STATUS.GOOD, bidRequest);
bid.creative_id = ad.ad_id;
bid.bidderCode = bidRequest.bidder;
bid.cpm = ad.cpm || 0;
bid.ad = ad.adm;
bid.width = ad.width;
bid.height = ad.height;
bid.dealId = ad.deal;

bidmanager.addBidResponse(bidRequest.placementCode, bid);
}

return Object.assign(Adapter.createNew(SMARTYADS_BIDDER_CODE), { // SMARTYADS_BIDDER_CODE smartyads
callBids: _callBids,
createNew: SmartyadsAdapter.createNew
});
}

SmartyadsAdapter.masSizeOrdering = function (sizes) {

const MAS_SIZE_PRIORITY = [15, 2, 9];

return utils.parseSizesInput(sizes)
// map sizes while excluding non-matches
.reduce((result, size) => {
let mappedSize = parseInt(sizeMap[size], 10);
if (mappedSize) {
result.push(mappedSize);
}
return result;
}, [])
.sort((first, second) => {
// sort by MAS_SIZE_PRIORITY priority order
let firstPriority = MAS_SIZE_PRIORITY.indexOf(first),
secondPriority = MAS_SIZE_PRIORITY.indexOf(second);

if (firstPriority > -1 || secondPriority > -1) {
if (firstPriority === -1) {
return 1;
}
if (secondPriority === -1) {
return -1;
}
return firstPriority - secondPriority;
}

return first - second;
});
};

SmartyadsAdapter.createNew = function () {
return new SmartyadsAdapter();
};

module.exports = SmartyadsAdapter;
131 changes: 131 additions & 0 deletions test/spec/adapters/smartyads_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { expect } from 'chai';
import Adapter from '../../../src/adapters/smartyads';
import adapterManager from 'src/adaptermanager';
import bidManager from 'src/bidmanager';
import CONSTANTS from 'src/constants.json';

describe('Smartyads adapter tests', function () {

let sandbox;
const adUnit = { // TODO CHANGE
code: 'smartyads',
sizes: [[300, 250], [300,600], [320, 80]],
bids: [{
bidder: 'smartyads',
params: {
banner_id: 0
}
}]
};

const response = {
ad_id: 0,
adm: "<span>Test Response</span>",
cpm: 0.5,
deal: "bf063e2e025c",
height: 240,
width: 360
};

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('Smartyads callBids validation', () => {

let bids,
server;

beforeEach(() => {
bids = [];
server = sinon.fakeServer.create();

sandbox.stub(bidManager, 'addBidResponse', (elemId, bid) => {
bids.push(bid);
});
});

afterEach(() => {
server.restore();
});

let adapter = adapterManager.bidderRegistry['smartyads'];

it('Valid bid-request', () => {
sandbox.stub(adapter, 'callBids');
adapterManager.callBids({
adUnits: [clone(adUnit)]
});

let bidderRequest = adapter.callBids.getCall(0).args[0];

expect(bidderRequest).to.have.property('bids')
.that.is.an('array')
.with.lengthOf(1);

expect(bidderRequest).to.have.deep.property('bids[0]')
.to.have.property('bidder', 'smartyads');

expect(bidderRequest).to.have.deep.property('bids[0]')
.with.property('sizes')
.that.is.an('array')
.with.lengthOf(3)
.that.deep.equals(adUnit.sizes);
expect(bidderRequest).to.have.deep.property('bids[0]')
.with.property('params')
.to.have.property('banner_id', 0);
});

it('Valid bid-response', ()=>{
server.respondWith(JSON.stringify(
response
));
adapterManager.callBids({
adUnits: [clone(adUnit)]
});
server.respond();

expect(bids).to.be.lengthOf(1);
expect(bids[0].getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD);
expect(bids[0].bidderCode).to.equal("smartyads");
expect(bids[0].width).to.equal(360);
expect(bids[0].height).to.equal(240);
expect(bids[0].cpm).to.equal(0.5);
expect(bids[0].dealId).to.equal("bf063e2e025c");
});
});

describe('MAS mapping / ordering', () => {

let masSizeOrdering = Adapter.masSizeOrdering;

it('should not include values without a proper mapping', () => {
let ordering = masSizeOrdering([[320, 50], [42, 42], [300, 250], [640, 480], [1, 1], [336, 280]]);
expect(ordering).to.deep.equal([15, 16, 43, 65]);
});

it('should sort values without any MAS priority sizes in regular ascending order', () => {
let ordering = masSizeOrdering([[320, 50], [640, 480], [336, 280], [200, 600]]);
expect(ordering).to.deep.equal([16, 43, 65, 126]);
});

it('should sort MAS priority sizes in the proper order w/ rest ascending', () => {
let ordering = masSizeOrdering([[320, 50], [160,600], [640, 480], [300, 250],[336, 280], [200, 600]]);
expect(ordering).to.deep.equal([15, 9, 16, 43, 65, 126]);

ordering = masSizeOrdering([[320, 50], [300, 250], [160,600], [640, 480],[336, 280], [200, 600], [728, 90]]);
expect(ordering).to.deep.equal([15, 2, 9, 16, 43, 65, 126]);

ordering = masSizeOrdering([[120, 600], [320, 50], [160,600], [640, 480],[336, 280], [200, 600], [728, 90]]);
expect(ordering).to.deep.equal([2, 9, 8, 16, 43, 65, 126]);
})
});
});

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}