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

Mytarget Bid Adapter : update adapter to comply with Prebid 5 #7397

Merged
merged 6 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
109 changes: 109 additions & 0 deletions modules/mytargetBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as utils from '../src/utils.js';
Copy link
Collaborator

@ChrisHuie ChrisHuie Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please just import the function needed from utils here? I think you are just using _map from utils. We went through all adapters this week to eliminate the need for utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisHuie i fixed this import but tests failed again, local test OK!
In First I merge topical master

import { config } from '../src/config.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';

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) {
const parsed = document.createElement('a');
parsed.href = decodeURIComponent(referrer);
sitename = parsed.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);
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');
});
});
});