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

Add Meazy bid adapter #4015

Merged
merged 5 commits into from
Aug 20, 2019
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
152 changes: 152 additions & 0 deletions modules/meazyBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import * as utils from '../src/utils';
import { registerBidder } from '../src/adapters/bidderFactory';
import { BANNER } from '../src/mediaTypes';

const BIDDER_CODE = 'meazy';
const PREBID_ENDPOINT = 'rtb-filter.meazy.co';
const SYNC_ENDPOINT = '//sync.meazy.co/sync/iframe';
const ENDPOINT_CONFIG = {
defaultCurrency: ['USD'],
availableSize: ['300x250', '320x480', '160x600']
};

const buildURI = (pid) => {
return `//${PREBID_ENDPOINT}/pbjs?host=${utils.getOrigin()}&api_key=${pid}`;
}

const validateSize = (size) => {
return ENDPOINT_CONFIG.availableSize.indexOf(size.join('x')) !== -1;
}

const buildImpression = (bidRequest) => {
const impression = {
id: utils.getUniqueIdentifierStr(),
tagid: bidRequest.adUnitCode,
banner: {
format: bidRequest.sizes.map(size => ({ w: size[0], h: size[1] }))
}
};

return impression;
}

const buildItems = (bidRequests) => {
const imp = [];
const user = {};

bidRequests.forEach(bidRequest => {
imp.push(buildImpression(bidRequest));
if (utils.deepAccess(bidRequest, 'gdprConsent.gdprApplies')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The gdprConsent object exists in the bidderRequest param of the spec.buildRequests() function, not the bidRequests param. You'll need to pass in the bidderRequest param to this overall function and read that value to make this logic work.

user.ext = {
consent: bidRequest.gdprConsent.consentString,
gdpr: bidRequest.gdprConsent.gdprApplies & 1
}
}
});

return { imp, user };
}

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],

/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!bid.params.pid && bid.sizes.some(validateSize);
},

/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(bidRequests) {
const { user, imp } = buildItems(bidRequests);

const payload = {
imp,
user,
id: bidRequests[0].bidId,
site: {
domain: utils.getOrigin(),
page: utils.getTopWindowUrl(),
ref: utils.getTopWindowReferrer()
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two getTopWindow functions are slated to be deprecated in a future version of Prebid.

There is an alternate property in the bidderRequest object that contains type of information and should be used instead of these functions. The following link provides some additional details on the property:
http://prebid.org/dev-docs/bidder-adaptor.html#referrers

Can you please update these functions?

},
device: {
w: window.screen.width,
h: window.screen.height,
language: navigator.language
},
cur: ENDPOINT_CONFIG.defaultCurrency
};
const payloadString = JSON.stringify(payload);

return {
method: 'POST',
url: buildURI(bidRequests[0].params.pid),
data: payloadString
};
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse) {
const bids = [];

if (!utils.isArray(serverResponse.body.seatbid) || !serverResponse.body.seatbid[0]) {
return bids;
}

serverResponse.body.seatbid[0].bid.forEach(bidResponse => {
const bid = {
requestId: serverResponse.body.id,
cpm: bidResponse.price,
width: bidResponse.w,
height: bidResponse.h,
creativeId: bidResponse.crid,
netRevenue: bidResponse.netRevenue !== undefined ? bidResponse.netRevenue : true,
dealId: bidResponse.dealid,
currency: ENDPOINT_CONFIG.defaultCurrency[0],
ttl: bidResponse.exp || 900,
ad: bidResponse.adm
}

bids.push(bid);
});

return bids;
},

getUserSyncs: function(syncOptions, serverResponses) {
const syncs = [];

if (syncOptions.pixelEnabled) {
syncs.push({
type: 'image',
url: serverResponses[0].body.ext.syncUrl
Copy link
Collaborator

Choose a reason for hiding this comment

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

You may want to check that this property exists before registering the pixel. Otherwise you could wind up with an undefined error.

});
}

if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: SYNC_ENDPOINT
});
}

return syncs;
}
}

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

Module Name: Meazy Bidder Adapter
Module Type: Bidder Adapter
Maintainer: dima@meazy.co

# Description

Module that connects to Meazy demand sources

# Test Parameters
```
var adUnits = [{
code: 'test-div',
sizes: [[300, 250]],
bids: [{
bidder: "meazy",
params: {
pid: '6910b7344ae566a1'
}
}]
}];
```
121 changes: 121 additions & 0 deletions test/spec/modules/meazyBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as utils from 'src/utils';
import { expect } from 'chai';
import { spec } from 'modules/meazyBidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';

const MEAZY_PID = '6910b7344ae566a1'
const VALID_ENDPOINT = `//rtb-filter.meazy.co/pbjs?host=${utils.getOrigin()}&api_key=${MEAZY_PID}`;

const bidRequest = {
bidder: 'meazy',
adUnitCode: 'test-div',
sizes: [[300, 250], [300, 600]],
params: {
pid: MEAZY_PID
},
bidId: '30b31c1838de1e',
bidderRequestId: '22edbae2733bf6',
auctionId: '1d1a030790a475',
};

const bidResponse = {
body: {
'id': '30b31c1838de1e',
'bidid': '9780a52ff05c0e92780f5baf9cf3f4e8',
'cur': 'USD',
'seatbid': [{
'bid': [{
'id': 'ccf05fb8effb3d02',
'impid': 'B19C34BBD69DAF9F',
'burl': 'https://track.meazy.co/imp?bidid=9780a52ff05c0e92780f5baf9cf3f4e8&user=fdc401a2-92f1-42bd-ac22-d570520ad0ec&burl=1&ssp=5&project=2&cost=${AUCTION_PRICE}',
'adm': '<iframe src="https://ads.meazy.co/ad?u=fdc401a2-92f1-42bd-ac22-d570520ad0ec&s=6&p=2&g=e97b3549dc62c94d7e6b1d6db50917f9&gu=%252Fsergi-s-podvesnoy-chastyu%252Fserebryanye-serezhki-s-fianitami-302-00130-16373%252F&r=9780a52ff05c0e92780f5baf9cf3f4e8&si=75&sz=300x250&ssp=5&ts=1563820889&iph=6712b60796a2ed8ce4da786a7715ff2c25295aea&pl=ukr.net&bp=1.5&sp=${AUCTION_PRICE}" width="300" height="250" scrolling="no" style="overflow:hidden" frameBorder="0"></iframe>',
'adid': 'ad-2.6.75.300x250',
'price': 1.5,
'w': 300,
'h': 250,
'cid': '2.6.75',
'crid': '2.6.75.300x250',
'dealid': 'default'
}],
'seat': '2'
}],
'ext': {
'syncUrl': '//sync.meazy.co/sync/img?api_key=6910b7344ae566a1'
}
}
};

const noBidResponse = { body: {'nbr': 2} };

describe('meazyBidAdapter', function () {
const adapter = newBidder(spec);

describe('inherited functions', function () {
it('exists and is a function', function () {
expect(adapter.callBids).to.exist.and.to.be.a('function');
});
});

describe('isBidRequestValid', function () {
it('should return false', function () {
let bid = Object.assign({}, bidRequest);
bid.params = {};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('should return true', function () {
expect(spec.isBidRequestValid(bidRequest)).to.equal(true);
});
});

describe('buildRequests', function () {
it('should format valid url', function () {
const request = spec.buildRequests([bidRequest]);
expect(request.url).to.equal(VALID_ENDPOINT);
});

it('should format valid url', function () {
const request = spec.buildRequests([bidRequest]);
expect(request.url).to.equal(VALID_ENDPOINT);
});

it('should format valid request body', function () {
const request = spec.buildRequests([bidRequest]);
const payload = JSON.parse(request.data);
expect(payload.id).to.exist;
expect(payload.imp).to.exist;
expect(payload.imp[0]).to.exist;
expect(payload.imp[0].banner).to.exist;
expect(payload.imp[0].banner.format).to.exist;
expect(payload.device).to.exist;
expect(payload.site).to.exist;
expect(payload.site.domain).to.exist;
expect(payload.cur).to.exist;
});
});

describe('interpretResponse', function () {
it('should get correct bid response', function () {
const result = spec.interpretResponse(bidResponse);
const validResponse = [{
requestId: '30b31c1838de1e',
cpm: 1.5,
width: 300,
height: 250,
creativeId: '2.6.75.300x250',
netRevenue: true,
dealId: 'default',
currency: 'USD',
ttl: 900,
ad: '<iframe src="https://ads.meazy.co/ad?u=fdc401a2-92f1-42bd-ac22-d570520ad0ec&s=6&p=2&g=e97b3549dc62c94d7e6b1d6db50917f9&gu=%252Fsergi-s-podvesnoy-chastyu%252Fserebryanye-serezhki-s-fianitami-302-00130-16373%252F&r=9780a52ff05c0e92780f5baf9cf3f4e8&si=75&sz=300x250&ssp=5&ts=1563820889&iph=6712b60796a2ed8ce4da786a7715ff2c25295aea&pl=ukr.net&bp=1.5&sp=${AUCTION_PRICE}" width="300" height="250" scrolling="no" style="overflow:hidden" frameBorder="0"></iframe>'
}];

expect(result).to.deep.equal(validResponse);
});

it('handles nobid responses', function () {
let result = spec.interpretResponse(noBidResponse);
expect(result.length).to.equal(0);
});
});
});