Skip to content

Commit

Permalink
Smartico Bid Adapter: add new bid adapter (prebid#6486)
Browse files Browse the repository at this point in the history
* Adding smartico adapter

* bug prebid#6486 fix, added maintainer email

* bug prebid#6486 fix, modified test parameters

* bug prebid#6486 fix, modified test parameters prebid#2

* prebid#6486 applied review related updates & fixes

* prebid#6486 applied review related updates & fixes prebid#2

* prebid#6486 applied review related updates & fixes prebid#3

Co-authored-by: Dmitri <dmitri@atomdev.net>
  • Loading branch information
2 people authored and umakajan committed May 6, 2021
1 parent 4e0a72d commit 328a87b
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
116 changes: 116 additions & 0 deletions modules/smarticoBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import find from 'core-js-pure/features/array/find.js';

const SMARTICO_CONFIG = {
bidRequestUrl: 'https://trmads.eu/preBidRequest',
widgetUrl: 'https://trmads.eu/get',
method: 'POST'
}

const BIDDER_CODE = 'smartico';

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: function (bid) {
return !!(bid && bid.params && bid.params.token && bid.params.placementId);
},
buildRequests: function (validBidRequests, bidderRequest) {
var i
var j
var bid
var bidParam
var bidParams = []
var sizes
var frameWidth = Math.round(window.screen.width)
var frameHeight = Math.round(window.screen.height)
for (i = 0; i < validBidRequests.length; i++) {
bid = validBidRequests[i]
if (bid.sizes) {
sizes = bid.sizes
} else if (typeof (BANNER) != 'undefined' && bid.mediaTypes && bid.mediaTypes[BANNER] && bid.mediaTypes[BANNER].sizes) {
sizes = bid.mediaTypes[BANNER].sizes
} else if (frameWidth && frameHeight) {
sizes = [[frameWidth, frameHeight]]
} else {
sizes = []
}
for (j = 0; j < sizes.length; j++) {
bidParam = {
token: bid.params.token || '',
bidId: bid.bidId,
'banner-format-width': sizes[j][0],
'banner-format-height': sizes[j][1]
}
if (bid.params.bannerFormat) {
bidParam['banner-format'] = bid.params.bannerFormat
}
if (bid.params.language) {
bidParam.language = bid.params.language
}
if (bid.params.region) {
bidParam.region = bid.params.region
}
if (bid.params.regions && (bid.params.regions instanceof String || (bid.params.regions instanceof Array && bid.params.regions.length))) {
bidParam.regions = bid.params.regions
if (bidParam.regions instanceof Array) {
bidParam.regions = bidParam.regions.join(',')
}
}
bidParams.push(bidParam)
}
}

var ServerRequestObjects = {
method: SMARTICO_CONFIG.method,
url: SMARTICO_CONFIG.bidRequestUrl,
bids: validBidRequests,
data: {bidParams: bidParams, auctionId: bidderRequest.auctionId, origin: window.location.origin}
}

return ServerRequestObjects;
},
interpretResponse: function (serverResponse, bidRequest) {
var i
var bid
var bidObject
var url
var html
var ad
var token
var language
var scriptId
var bidResponses = []

for (i = 0; i < serverResponse.length; i++) {
ad = serverResponse[i];
bid = find(bidRequest.bids, bid => bid.bidId === ad.bidId)
if (bid) {
token = bid.params.token || ''

language = bid.params.language || SMARTICO_CONFIG.language || ''

scriptId = encodeURIComponent('smartico-widget-' + bid.params.placementId + '-' + i)

url = SMARTICO_CONFIG.widgetUrl + '?token=' + encodeURIComponent(token) + '&auction-id=' + encodeURIComponent(bid.auctionId) + '&from-auction-buffer=1&own_session=1&ad=' + encodeURIComponent(ad.id) + '&scriptid=' + scriptId + (ad.bannerFormatAlias ? '&banner-format=' + encodeURIComponent(ad.bannerFormatAlias) : '') + (language ? '&language=' + encodeURIComponent(language) : '')

html = '<script id="' + scriptId + '" async defer type="text/javascript" src="' + url + '"><\/script>'

bidObject = {
requestId: bid.bidId,
cpm: ad.cpm,
width: parseInt(ad.bannerFormatWidth),
height: parseInt(ad.bannerFormatHeight),
creativeId: ad.id,
netRevenue: false, // gross
ttl: ad.ttl,
ad: html
}
bidResponses.push(bidObject);
}
}
return bidResponses;
}
}
registerBidder(spec)
32 changes: 32 additions & 0 deletions modules/smarticoBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Overview

Module Name: Smartico Bidder Adapter
Module Type: Bidder Adapter
Maintainer: sk@smartico.eu

# Description

Module that connects to Smartico's demand sources.

# Test Parameters

var adUnits = [
{
code: 'medium_rectangle',
mediaTypes: {
banner: {
sizes: [[300, 250]], // a display size
}
},
bids: [
{
bidder: 'smartico',
params: {
placementId: 'testPlacementId',
token: "FNVzUGZn9ebpIOoheh3kEJ2GQ6H6IyMH39sHXaya"
}
}
]
}
];

118 changes: 118 additions & 0 deletions test/spec/modules/smarticoBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import {expect} from 'chai';
import {spec} from 'modules/smarticoBidAdapter.js';
import {newBidder} from 'src/adapters/bidderFactory.js';

describe('smarticoBidAdapter', function () {
const adapter = newBidder(spec);
let bid = {
adUnitCode: 'adunit-code',
auctionId: '5kaj89l8-3456-2s56-c455-4g6h78jsdfgf',
bidRequestsCount: 1,
bidder: 'smartico',
bidderRequestId: '24081afs940568',
bidderRequestsCount: 1,
bidderWinsCount: 0,
bidId: '22499d052045',
mediaTypes: {banner: {sizes: [[300, 250]]}},
params: {
token: 'FNVzUGZn9ebpIOoheh3kEJ2GQ6H6IyMH39sHXaya',
placementId: 'testPlacementId'
},
sizes: [
[300, 250]
],
transactionId: '34562345-4dg7-46g7-4sg6-45gdsdj8fd56'
}
let bidderRequests = {
auctionId: 'b06c5141-fe8f-4cdf-9d7d-54415490a917',
auctionStart: 1579746300522,
bidderCode: 'myBidderCode',
bidderRequestId: '15246a574e859f',
bids: [bid],
refererInfo: {
canonicalUrl: '',
numIframes: 0,
reachedTop: true
}
}
describe('isBidRequestValid', function () {
it('should return true where required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});
});
describe('buildRequests', function () {
let bidRequests = [ bid ];
let request = spec.buildRequests(bidRequests, bidderRequests);
it('sends bid request via POST', function () {
expect(request.method).to.equal('POST');
});
it('must contain token', function() {
expect(request.data.bidParams[0].token).to.equal('FNVzUGZn9ebpIOoheh3kEJ2GQ6H6IyMH39sHXaya');
});
it('must contain auctionId', function() {
expect(request.data.auctionId).to.exist.and.to.be.a('string')
});
it('must contain valid width and height', function() {
expect(request.data.bidParams[0]['banner-format-width']).to.exist.and.to.be.a('number')
expect(request.data.bidParams[0]['banner-format-height']).to.exist.and.to.be.a('number')
});
});

describe('interpretResponse', function () {
let bidRequest = {
method: 'POST',
url: 'https://trmads.eu/preBidRequest',
bids: [bid],
data: [{
token: 'FNVzUGZn9ebpIOoheh3kEJ2GQ6H6IyMH39sHXaya',
bidId: '22499d052045',
'banner-format-width': 300,
'banner-format-height': 250,
placementId: 'testPlacementId',
}]
};
let serverResponse = [{
bidId: '22499d052045',
id: 987654,
cpm: 10,
ttl: 30,
bannerFormatWidth: 300,
bannerFormatHeight: 250,
bannerFormatAlias: 'medium_rectangle'
}];
let expectedResponse = [{
requestId: bid.bidId,
cpm: 10,
width: 300,
height: 250,
creativeId: 987654,
netRevenue: false, // gross
ttl: 30,
ad: '<script id="smartico-widget-testPlacementId-0" async defer type="text/javascript" src="https://trmads.eu/get?token=FNVzUGZn9ebpIOoheh3kEJ2GQ6H6IyMH39sHXaya&auction-id=5kaj89l8-3456-2s56-c455-4g6h78jsdfgf&from-auction-buffer=1&own_session=1&ad=987654&scriptid=smartico-widget-testPlacementId-0&banner-format=medium_rectangle"><\/script>'}];
let result = spec.interpretResponse(serverResponse, bidRequest);
it('should contain correct creativeId', function () {
expect(result[0].creativeId).to.equal(expectedResponse[0].creativeId)
});
it('should contain correct cpm', function () {
expect(result[0].cpm).to.equal(expectedResponse[0].cpm)
});
it('should contain correct width', function () {
expect(result[0].width).to.equal(expectedResponse[0].width)
});
it('should contain correct height', function () {
expect(result[0].height).to.equal(expectedResponse[0].height)
});
it('should contain correct requestId', function () {
expect(result[0].requestId).to.equal(expectedResponse[0].requestId)
});
it('should contain correct ttl', function () {
expect(result[0].ttl).to.equal(expectedResponse[0].ttl)
});
it('should contain correct netRevenue', function () {
expect(result[0].netRevenue).to.equal(expectedResponse[0].netRevenue)
});
it('should contain correct ad content', function () {
expect(result[0].ad).to.equal(expectedResponse[0].ad)
});
});
});

0 comments on commit 328a87b

Please sign in to comment.