-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Adnow bidder #5738
Merged
Merged
Adnow bidder #5738
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b798f2
Add AdNow bid Adaptor
vingood 8a7f716
Fix problems by PR comments.
vingood 24296be
PR comments:
vingood f314c2a
rename test
vingood 1d3d67e
Restore package-lock.json from master
vingood 2cb1f51
Fix sizes of bid response object for banners.
vingood 720b06c
Fix adapters tests.
vingood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { registerBidder } from '../src/adapters/bidderFactory.js'; | ||
import { NATIVE, BANNER } from '../src/mediaTypes.js'; | ||
import * as utils from '../src/utils.js'; | ||
import includes from 'core-js-pure/features/array/includes.js'; | ||
|
||
const BIDDER_CODE = 'adnow'; | ||
const ENDPOINT = 'https://n.ads3-adnow.com/a'; | ||
|
||
/** | ||
* @typedef {object} CommonBidData | ||
* | ||
* @property {string} requestId The specific BidRequest which this bid is aimed at. | ||
* This should match the BidRequest.bidId which this Bid targets. | ||
* @property {string} currency The currency code for the cpm value | ||
* @property {number} cpm The bid price, in US cents per thousand impressions. | ||
* @property {string} creativeId The id of ad content | ||
* @property {number} ttl Time-to-live - how long (in seconds) Prebid can use this bid. | ||
* @property {boolean} netRevenue Boolean defining whether the bid is Net or Gross. The default is true (Net). | ||
* @property {object} [meta] Object for storing bid meta data | ||
* @property {string} [meta.mediaType] banner or native | ||
*/ | ||
|
||
/** @type {BidderSpec} */ | ||
export const spec = { | ||
code: BIDDER_CODE, | ||
supportedMediaTypes: [ NATIVE, BANNER ], | ||
|
||
/** | ||
* @param {object} bid | ||
* @return {boolean} | ||
*/ | ||
isBidRequestValid(bid) { | ||
if (!bid || !bid.params) return false; | ||
|
||
const codeId = parseInt(bid.params.codeId, 10); | ||
if (!codeId) { | ||
return false; | ||
} | ||
|
||
const mediaType = bid.params.mediaType || NATIVE; | ||
|
||
return includes(this.supportedMediaTypes, mediaType); | ||
}, | ||
|
||
/** | ||
* @param {BidRequest[]} validBidRequests | ||
* @param {*} bidderRequest | ||
* @return {ServerRequest} | ||
*/ | ||
buildRequests(validBidRequests, bidderRequest) { | ||
return validBidRequests.map(req => { | ||
const mediaType = this._isBannerRequest(req) ? BANNER : NATIVE; | ||
const codeId = parseInt(req.params.codeId, 10); | ||
|
||
const data = { | ||
Id: codeId, | ||
mediaType: mediaType, | ||
out: 'prebid', | ||
d_user_agent: navigator.userAgent, | ||
requestid: req.bidId | ||
}; | ||
|
||
if (mediaType === BANNER) { | ||
data.sizes = utils.parseSizesInput( | ||
req.mediaTypes && req.mediaTypes.banner && req.mediaTypes.banner.sizes | ||
).join('|') | ||
} else { | ||
data.width = data.height = 200; | ||
|
||
let sizes = utils.deepAccess(req, 'mediaTypes.native.image.sizes', []); | ||
|
||
if (sizes.length > 0) { | ||
const size = Array.isArray(sizes[0]) ? sizes[0] : sizes; | ||
|
||
data.width = size[0] || data.width; | ||
data.height = size[1] || data.height; | ||
} | ||
} | ||
|
||
/** @type {ServerRequest} */ | ||
return { | ||
method: 'GET', | ||
url: ENDPOINT, | ||
data: utils.parseQueryStringParameters(data), | ||
options: { | ||
withCredentials: false, | ||
crossOrigin: true | ||
}, | ||
bidRequest: req | ||
}; | ||
}); | ||
}, | ||
|
||
/** | ||
* @param {*} response | ||
* @param {ServerRequest} request | ||
* @return {Bid[]} | ||
*/ | ||
interpretResponse(response, request) { | ||
const bidObj = request.bidRequest; | ||
let bid = response.body; | ||
|
||
if (!bid || !bid.currency || !bid.cpm) { | ||
return []; | ||
} | ||
|
||
const mediaType = bid.meta.mediaType || NATIVE; | ||
if (!includes(this.supportedMediaTypes, mediaType)) { | ||
return []; | ||
} | ||
|
||
bid.requestId = bidObj.bidId; | ||
|
||
if (mediaType === BANNER) { | ||
return [ this._getBannerBid(bid) ]; | ||
} | ||
|
||
if (mediaType === NATIVE) { | ||
return [ this._getNativeBid(bid) ]; | ||
} | ||
|
||
return []; | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {object} bid | ||
* @return {CommonBidData} | ||
*/ | ||
_commonBidData(bid) { | ||
return { | ||
requestId: bid.requestId, | ||
currency: bid.currency || 'USD', | ||
cpm: bid.cpm || 0.00, | ||
creativeId: bid.creativeId || 'undefined-creative', | ||
netRevenue: bid.netRevenue || true, | ||
ttl: bid.ttl || 360, | ||
meta: bid.meta || {} | ||
}; | ||
}, | ||
|
||
/** | ||
* @param {BidRequest} req | ||
* @return {boolean} | ||
* @private | ||
*/ | ||
_isBannerRequest(req) { | ||
return !!(req.mediaTypes && req.mediaTypes.banner); | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {object} bid | ||
* @return {Bid} | ||
*/ | ||
_getBannerBid(bid) { | ||
return { | ||
...this._commonBidData(bid), | ||
ad: bid.ad || '<div>Empty Ad</div>' | ||
}; | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {object} bid | ||
* @return {Bid} | ||
*/ | ||
_getNativeBid(bid) { | ||
return { | ||
...this._commonBidData(bid), | ||
native: bid.native || {} | ||
}; | ||
} | ||
} | ||
|
||
registerBidder(spec); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Overview | ||
|
||
``` | ||
Module Name: AdNow Bidder Adapter | ||
Module Type: Bidder Adapter | ||
Maintainer: [email protected] | ||
``` | ||
|
||
# Description | ||
|
||
AdNow Bidder Adapter for Prebid.js. | ||
Banner and Native format are supported. | ||
Please use ```adnow``` as the bidder code. | ||
|
||
# Test Parameters | ||
```javascript | ||
const adUnits = [{ | ||
code: 'test', | ||
mediaTypes: { | ||
banner: { | ||
sizes: [[300, 250]] | ||
} | ||
}, | ||
bids: [{ | ||
bidder: 'adnow', | ||
params: { | ||
codeId: 794934 | ||
} | ||
}] | ||
}, { | ||
code: 'test', | ||
mediaTypes: { | ||
native: { | ||
image: { | ||
sizes: [200, 200] | ||
} | ||
} | ||
}, | ||
bids: [{ | ||
bidder: 'adnow', | ||
params: { | ||
codeId: 794934 | ||
} | ||
}] | ||
}]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { expect } from 'chai'; | ||
import { spec } from 'modules/adnowBidAdapter.js'; | ||
|
||
describe.only('adnowBidAdapter', function () { | ||
describe('isBidRequestValid', function () { | ||
it('Should return true', function() { | ||
expect(spec.isBidRequestValid({ | ||
bidder: 'adnow', | ||
params: { | ||
codeId: 12345 | ||
} | ||
})).to.equal(true); | ||
}); | ||
|
||
it('Should return false when required params is not passed', function() { | ||
expect(spec.isBidRequestValid({ | ||
bidder: 'adnow', | ||
params: {} | ||
})).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('buildRequests', function() { | ||
it('Common settings', function() { | ||
const bidRequestData = [{ | ||
bidId: 'bid12345', | ||
params: { | ||
codeId: 12345 | ||
} | ||
}]; | ||
|
||
const req = spec.buildRequests(bidRequestData); | ||
const reqData = req[0].data; | ||
|
||
expect(reqData) | ||
.to.match(/Id=12345/) | ||
.to.match(/mediaType=native/) | ||
.to.match(/out=prebid/) | ||
.to.match(/requestid=bid12345/) | ||
.to.match(/d_user_agent=.+/); | ||
}); | ||
|
||
it('Banner sizes', function () { | ||
const bidRequestData = [{ | ||
bidId: 'bid12345', | ||
params: { | ||
codeId: 12345 | ||
}, | ||
mediaTypes: { | ||
banner: { | ||
sizes: [[300, 250]] | ||
} | ||
} | ||
}]; | ||
|
||
const req = spec.buildRequests(bidRequestData); | ||
const reqData = req[0].data; | ||
|
||
expect(reqData).to.match(/sizes=300x250/); | ||
}); | ||
|
||
it.only('Native sizes', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the |
||
const bidRequestData = [{ | ||
bidId: 'bid12345', | ||
params: { | ||
codeId: 12345 | ||
}, | ||
mediaTypes: { | ||
native: { | ||
image: { | ||
sizes: [100, 100] | ||
} | ||
} | ||
} | ||
}]; | ||
|
||
const req = spec.buildRequests(bidRequestData); | ||
const reqData = req[0].data; | ||
|
||
expect(reqData) | ||
.to.match(/width=100/) | ||
.to.match(/height=100/); | ||
}); | ||
}); | ||
|
||
describe('interpretResponse', function() { | ||
const request = { | ||
bidRequest: { | ||
bidId: 'bid12345' | ||
} | ||
}; | ||
|
||
it('Response with native bid', function() { | ||
const response = { | ||
currency: 'USD', | ||
cpm: 0.5, | ||
native: { | ||
title: 'Title', | ||
body: 'Body', | ||
sponsoredBy: 'AdNow', | ||
clickUrl: '//click.url', | ||
image: { | ||
url: '//img.url', | ||
height: 200, | ||
width: 200 | ||
} | ||
}, | ||
meta: { | ||
mediaType: 'native' | ||
} | ||
}; | ||
|
||
const bids = spec.interpretResponse({ body: response }, request); | ||
expect(bids).to.be.an('array').that.is.not.empty; | ||
|
||
const bid = bids[0]; | ||
expect(bid).to.have.keys('requestId', 'cpm', 'currency', 'native', 'creativeId', 'netRevenue', 'meta', 'ttl'); | ||
|
||
const nativePart = bid.native; | ||
|
||
expect(nativePart.title).to.be.equal('Title'); | ||
expect(nativePart.body).to.be.equal('Body'); | ||
expect(nativePart.clickUrl).to.be.equal('//click.url'); | ||
expect(nativePart.image.url).to.be.equal('//img.url'); | ||
expect(nativePart.image.height).to.be.equal(200); | ||
expect(nativePart.image.width).to.be.equal(200); | ||
}); | ||
|
||
it('Response with banner bid', function() { | ||
const response = { | ||
currency: 'USD', | ||
cpm: 0.5, | ||
ad: '<div>Banner</div>', | ||
meta: { | ||
mediaType: 'banner' | ||
} | ||
}; | ||
|
||
const bids = spec.interpretResponse({ body: response }, request); | ||
expect(bids).to.be.an('array').that.is.not.empty; | ||
|
||
const bid = bids[0]; | ||
expect(bid).to.have.keys('requestId', 'cpm', 'currency', 'ad', 'creativeId', 'netRevenue', 'meta', 'ttl'); | ||
|
||
expect(bid.ad).to.be.equal('<div>Banner</div>'); | ||
}); | ||
|
||
it('Response with no bid should return an empty array', function() { | ||
const noBidResponses = [ | ||
false, | ||
{}, | ||
{body: false}, | ||
{body: {}} | ||
]; | ||
|
||
noBidResponses.forEach(response => { | ||
return expect(spec.interpretResponse(response, request)).to.be.an('array').that.is.empty; | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the
.only
from your describe function here.