Skip to content

Commit

Permalink
Added myTarget Adapter (prebid#3599)
Browse files Browse the repository at this point in the history
* Add myTargetBidAdapter

* myTargetBidAdapter: replace legacy substr function
  • Loading branch information
vfedoseev authored and Isaac Dettman committed Mar 5, 2019
1 parent 9d5b9b4 commit 35c58e0
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 0 deletions.
108 changes: 108 additions & 0 deletions modules/mytargetBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import * as utils from '../src/utils';
import * as url from '../src/url';
import { config } from '../src/config';
import { registerBidder } from '../src/adapters/bidderFactory';

const BIDDER_CODE = 'mytarget';
const BIDDER_URL = '//ad.mail.ru/hbid_prebid/';
const DEFAULT_CURRENCY = 'RUB';
const DEFAULT_TTL = 180;

function buildPlacement(bidRequest) {
let { bidId, params } = bidRequest;
let { placementId, position, response, bidfloor } = params;
let placement = {
placementId,
id: bidId,
position: position || 0,
response: response || 0
};

if (typeof bidfloor !== 'undefined') {
placement.bidfloor = bidfloor;
}

return placement;
}

function getSiteName(referrer) {
let sitename = config.getConfig('mytarget.sitename');

if (!sitename) {
sitename = url.parse(referrer).hostname;
}

return sitename;
}

function generateRandomId() {
return Math.random().toString(16).substring(2);
}

export const spec = {
code: BIDDER_CODE,

isBidRequestValid: function(bid) {
return !!bid.params.placementId;
},

buildRequests: function(validBidRequests, bidderRequest) {
let referrer = '';

if (bidderRequest && bidderRequest.refererInfo) {
referrer = bidderRequest.refererInfo.referer;
}

const payload = {
places: utils._map(validBidRequests, buildPlacement),
site: {
sitename: getSiteName(referrer),
page: referrer
},
settings: {
currency: DEFAULT_CURRENCY,
windowSize: {
width: window.screen.width,
height: window.screen.height
}
}
};

return {
method: 'POST',
url: BIDDER_URL,
data: payload,
};
},

interpretResponse: function(serverResponse, bidRequest) {
let { body } = serverResponse;

if (body.bids) {
return utils._map(body.bids, (bid) => {
let bidResponse = {
requestId: bid.id,
cpm: bid.price,
width: bid.size.width,
height: bid.size.height,
ttl: bid.ttl || DEFAULT_TTL,
currency: bid.currency || DEFAULT_CURRENCY,
creativeId: bid.creativeId || generateRandomId(),
netRevenue: true
}

if (bid.adm) {
bidResponse.ad = bid.adm;
} else {
bidResponse.adUrl = bid.displayUrl;
}

return bidResponse;
});
}

return [];
}
}

registerBidder(spec);
40 changes: 40 additions & 0 deletions modules/mytargetBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Overview

```
Module Name: myTarget Bidder Adapter
Module Type: Bidder Adapter
Maintainer: support_target@corp.my.com
```

# Description

Module that connects to myTarget demand sources.

# Test Parameters

```
var adUnits = [{
code: 'placementCode',
mediaTypes: {
banner: {
sizes: [[240, 400]],
}
},
bids: [{
bidder: 'mytarget',
params: {
placementId: '379783',
// OPTIONAL: custom bid floor
bidfloor: 10000,
// OPTIONAL: if you know the ad position on the page, specify it here
// (this corresponds to "Ad Position" in OpenRTB 2.3, section 5.4)
position: 0,
// OPTIONAL: bid response type: 0 - ad url (default), 1 - ad markup
response: 0
}
}]
}];
```
199 changes: 199 additions & 0 deletions test/spec/modules/mytargetBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { expect } from 'chai';
import { spec } from 'modules/mytargetBidAdapter';

describe('MyTarget Adapter', function() {
describe('isBidRequestValid', function () {
it('should return true when required params found', function () {
let validBid = {
bidder: 'mytarget',
params: {
placementId: '1'
}
};

expect(spec.isBidRequestValid(validBid)).to.equal(true);
});

it('should return false for when required params are not passed', function () {
let invalidBid = {
bidder: 'mytarget',
params: {}
};

expect(spec.isBidRequestValid(invalidBid)).to.equal(false);
});
});

describe('buildRequests', function () {
let bidRequests = [
{
bidId: 'bid1',
bidder: 'mytarget',
params: {
placementId: '1'
}
},
{
bidId: 'bid2',
bidder: 'mytarget',
params: {
placementId: '2',
position: 1,
response: 1,
bidfloor: 10000
}
}
];
let bidderRequest = {
refererInfo: {
referer: 'https://example.com?param=value'
}
};

let bidRequest = spec.buildRequests(bidRequests, bidderRequest);

it('should build single POST request for multiple bids', function() {
expect(bidRequest.method).to.equal('POST');
expect(bidRequest.url).to.equal('//ad.mail.ru/hbid_prebid/');
expect(bidRequest.data).to.be.an('object');
expect(bidRequest.data.places).to.be.an('array');
expect(bidRequest.data.places).to.have.lengthOf(2);
});

it('should pass bid parameters', function() {
let place1 = bidRequest.data.places[0];
let place2 = bidRequest.data.places[1];

expect(place1.placementId).to.equal('1');
expect(place2.placementId).to.equal('2');
expect(place1.id).to.equal('bid1');
expect(place2.id).to.equal('bid2');
});

it('should pass default position and response type', function() {
let place = bidRequest.data.places[0];

expect(place.position).to.equal(0);
expect(place.response).to.equal(0);
});

it('should pass provided position and response type', function() {
let place = bidRequest.data.places[1];

expect(place.position).to.equal(1);
expect(place.response).to.equal(1);
});

it('should not pass default bidfloor', function() {
let place = bidRequest.data.places[0];

expect(place.bidfloor).not.to.exist;
});

it('should not pass provided bidfloor', function() {
let place = bidRequest.data.places[1];

expect(place.bidfloor).to.exist;
expect(place.bidfloor).to.equal(10000);
});

it('should pass site parameters', function() {
let site = bidRequest.data.site;

expect(site).to.be.an('object');
expect(site.sitename).to.equal('example.com');
expect(site.page).to.equal('https://example.com?param=value');
});

it('should pass settings', function() {
let settings = bidRequest.data.settings;

expect(settings).to.be.an('object');
expect(settings.currency).to.equal('RUB');
expect(settings.windowSize).to.be.an('object');
expect(settings.windowSize.width).to.equal(window.screen.width);
expect(settings.windowSize.height).to.equal(window.screen.height);
});
});

describe('interpretResponse', function () {
let serverResponse = {
body: {
'bidder_status':
[
{
'bidder': 'mail.ru',
'response_time_ms': 100,
'num_bids': 2
}
],
'bids':
[
{
'displayUrl': 'https://ad.mail.ru/hbid_imp/12345',
'size':
{
'height': '400',
'width': '240'
},
'id': '1',
'currency': 'RUB',
'price': 100,
'ttl': 360,
'creativeId': '123456'
},
{
'adm': '<p>Ad</p>',
'size':
{
'height': '250',
'width': '300'
},
'id': '2',
'price': 200
}
]
}
};

let bids = spec.interpretResponse(serverResponse);

it('should return empty array for response with no bids', function() {
let emptyBids = spec.interpretResponse({ body: {} });

expect(emptyBids).to.have.lengthOf(0);
});

it('should parse all bids from response', function() {
expect(bids).to.have.lengthOf(2);
});

it('should parse bid with ad url', function() {
expect(bids[0].requestId).to.equal('1');
expect(bids[0].cpm).to.equal(100);
expect(bids[0].width).to.equal('240');
expect(bids[0].height).to.equal('400');
expect(bids[0].ttl).to.equal(360);
expect(bids[0].currency).to.equal('RUB');
expect(bids[0]).to.have.property('creativeId');
expect(bids[0].creativeId).to.equal('123456');
expect(bids[0].netRevenue).to.equal(true);
expect(bids[0].adUrl).to.equal('https://ad.mail.ru/hbid_imp/12345');
expect(bids[0]).to.not.have.property('ad');
});

it('should parse bid with ad markup', function() {
expect(bids[1].requestId).to.equal('2');
expect(bids[1].cpm).to.equal(200);
expect(bids[1].width).to.equal('300');
expect(bids[1].height).to.equal('250');
expect(bids[1].ttl).to.equal(180);
expect(bids[1].currency).to.equal('RUB');
expect(bids[1]).to.have.property('creativeId');
expect(bids[1].creativeId).not.to.equal('123456');
expect(bids[1].netRevenue).to.equal(true);
expect(bids[1].ad).to.equal('<p>Ad</p>');
expect(bids[1]).to.not.have.property('adUrl');
});
});
});

0 comments on commit 35c58e0

Please sign in to comment.