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

Audiencerun Bid Adapter 1.1.0 #6919

Merged
merged 6 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
119 changes: 96 additions & 23 deletions modules/audiencerunBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,52 @@ import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';

const BIDDER_CODE = 'audiencerun';
const ENDPOINT_URL = 'https://d.audiencerun.com/prebid';
const BASE_URL = 'https://d.audiencerun.com';
const AUCTION_URL = `${BASE_URL}/prebid`;
const TIMEOUT_EVENT_URL = `${BASE_URL}/ps/pbtimeout`;
const DEFAULT_CURRENCY = 'USD';

let requestedBids = [];

/**
* Gets bidder request referer
*
* @param {Object} bidderRequest
* @return {string}
*/
function getPageUrl(bidderRequest) {
return (
config.getConfig('pageUrl') ||
utils.deepAccess(bidderRequest, 'refererInfo.referer') ||
null
);
}

/**
* Returns bidfloor through floors module if available
*
* @param {Object} bid
* @returns {number}
*/
function getBidFloor(bid) {
if (!utils.isFn(bid.getFloor)) {
return utils.deepAccess(bid, 'params.bidfloor', 0);
}

try {
const bidFloor = bid.getFloor({
currency: DEFAULT_CURRENCY,
mediaType: BANNER,
size: '*',
});
return bidFloor.floor;
} catch (_) {
return 0
}
}

export const spec = {
version: '1.0.0',
version: '1.1.0',
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],

Expand All @@ -33,50 +75,53 @@ export const spec = {
* @param {*} bidderRequest
* @return {ServerRequest} Info describing the request to the server.
*/
buildRequests: function(bidRequests, bidderRequest) {
const bids = bidRequests.map(bid => {
buildRequests: function (bidRequests, bidderRequest) {
const bids = bidRequests.map((bid) => {
const sizes = utils.deepAccess(bid, 'mediaTypes.banner.sizes', []);
return {
zoneId: utils.getValue(bid.params, 'zoneId'),
sizes: sizes.map(size => ({
sizes: sizes.map((size) => ({
w: size[0],
h: size[1]
h: size[1],
})),
bidfloor: bid.params.bidfloor || 0.0,
bidfloor: getBidFloor(bid),
bidId: bid.bidId,
bidderRequestId: utils.getBidIdParameter('bidderRequestId', bid),
adUnitCode: utils.getBidIdParameter('adUnitCode', bid),
auctionId: utils.getBidIdParameter('auctionId', bid),
transactionId: utils.getBidIdParameter('transactionId', bid)
transactionId: utils.getBidIdParameter('transactionId', bid),
};
});

const payload = {
libVersion: this.version,
referer: bidderRequest.refererInfo ? bidderRequest.refererInfo.referer || null : null,
referer: getPageUrl(bidderRequest),
currencyCode: config.getConfig('currency.adServerCurrency'),
timeout: config.getConfig('bidderTimeout'),
bids
bids,
};

if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdpr = {
consent: bidderRequest.gdprConsent.consentString,
applies: bidderRequest.gdprConsent.gdprApplies
applies: bidderRequest.gdprConsent.gdprApplies,
version: bidderRequest.gdprConsent.apiVersion,
};
} else {
payload.gdpr = {
consent: ''
}
consent: '',
};
}

requestedBids = bids;

return {
method: 'POST',
url: ENDPOINT_URL,
url: AUCTION_URL,
data: JSON.stringify(payload),
options: {
withCredentials: true
}
withCredentials: true,
},
};
},

Expand All @@ -100,15 +145,22 @@ export const spec = {

// Common properties
bid.requestId = bidObject.bidId;
bid.adId = bidObject.zoneId;
bid.cpm = parseFloat(bidObject.cpm);
bid.creativeId = bidObject.crid;
bid.currency = bidObject.currency ? bidObject.currency.toUpperCase() : 'USD';
bid.currency = bidObject.currency
? bidObject.currency.toUpperCase()
: DEFAULT_CURRENCY;

bid.height = bidObject.h;
bid.width = bidObject.w;
bid.netRevenue = bidObject.isNet ? bidObject.isNet : false;
bid.ttl = 300;
bid.meta = {
advertiserDomains:
bidObject.adomain && Array.isArray(bidObject.adomain)
? bidObject.adomain
: [],
};

bids.push(bid);
});
Expand All @@ -122,21 +174,42 @@ export const spec = {
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function(syncOptions, serverResponses) {
getUserSyncs: function (syncOptions, serverResponses) {
if (!serverResponses || !serverResponses.length) return [];

const syncs = [];
serverResponses.forEach(response => {
response.body.bid.forEach(bidObject => {
serverResponses.forEach((response) => {
response.body.bid.forEach((bidObject) => {
syncs.push({
type: 'iframe',
url: bidObject.syncUrl
url: bidObject.syncUrl,
});
});
});

return syncs;
}
},

/**
* Register bidder specific code, which will execute if bidder timed out after an auction
*
* @param {Array} timeoutData timeout specific data
*/
onTimeout: function (timeoutData) {
if (!utils.isArray(timeoutData)) {
return;
}

timeoutData.forEach((bid) => {
const bidOnTimeout = requestedBids.find((requestedBid) => requestedBid.bidId === bid.bidId);

if (bidOnTimeout) {
utils.triggerPixel(
`${TIMEOUT_EVENT_URL}/${bidOnTimeout.zoneId}/${bidOnTimeout.bidId}`
);
}
});
},
};

registerBidder(spec);
2 changes: 1 addition & 1 deletion modules/audiencerunBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**Module Name**: AudienceRun Bidder Adapter
**Module Type**: Bidder Adapter
**Maintainer**: prebid@audiencerun.com
**Maintainer**: github@audiencerun.com

# Description

Expand Down
53 changes: 47 additions & 6 deletions test/spec/modules/audiencerunBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const BID_SERVER_RESPONSE = {
{
'bidId': '51ef8751f9aead',
'zoneId': '12345abcde',
'adId': '1234',
'crid': '5678',
'cpm': 8.021951999999999999,
'currency': 'USD',
Expand All @@ -19,7 +18,8 @@ const BID_SERVER_RESPONSE = {
'isNet': false,
'buying_type': 'rtb',
'syncUrl': 'https://ac.audiencerun.com/f/sync.html',
'adm': '<!-- test creative -->'
'adm': '<!-- test creative -->',
'adomain': ['example.com']
}
]
}
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('AudienceRun bid adapter tests', function() {
});

describe('buildRequests', function() {
let bidRequests = [
const bidRequests = [
{
'bidder': 'audiencerun',
'bidId': '51ef8751f9aead',
Expand All @@ -96,6 +96,7 @@ describe('AudienceRun bid adapter tests', function() {
'bidRequestsCount': 1
}
];
const bidRequest = bidRequests[0];

it('sends a valid bid request to ENDPOINT via POST', function() {
const request = spec.buildRequests(bidRequests, {
Expand Down Expand Up @@ -156,12 +157,43 @@ describe('AudienceRun bid adapter tests', function() {
expect(payload2.gdpr.consent).to.equal(consentString);
expect(payload2.gdpr.applies).to.equal(false);
});

it('should use a bidfloor with a 0 value', function() {
const bid = Object.assign({}, bidRequest);
const request = spec.buildRequests([bid]);
const payload = JSON.parse(request.data);
expect(payload.bids[0].bidfloor).to.exist.and.to.equal(0);
})

it('should use bidfloor param value', function () {
const bid = Object.assign({}, bidRequest, {
params: {
'bidfloor': 0.2
}
})
const request = spec.buildRequests([bid]);
const payload = JSON.parse(request.data);
expect(payload.bids[0].bidfloor).to.exist.and.to.equal(0.2);
});

it('should use floors module value', function () {
const bid = Object.assign({}, bidRequest, {
params: {
'bidfloor': 0.5
}
})
bid.getFloor = () => {
return { floor: 1, currency: 'USD' }
}
const request = spec.buildRequests([bid]);
const payload = JSON.parse(request.data);
expect(payload.bids[0].bidfloor).to.exist.and.to.equal(1);
});
});

describe('interpretResponse', function () {
const expectedResponse = [{
'requestId': '51ef8751f9aead',
'adId': '12345abcde',
'cpm': 8.021951999999999999,
'width': '728',
'height': '90',
Expand All @@ -170,15 +202,18 @@ describe('AudienceRun bid adapter tests', function() {
'netRevenue': false,
'ttl': 300,
'ad': '<!-- test creative -->',
'mediaType': 'banner'
'mediaType': 'banner',
'meta': {
'advertiserDomains': ['example.com']
}
}];

it('should get the correct bid response by display ad', function () {
let result = spec.interpretResponse(BID_SERVER_RESPONSE);
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});

it('handles empty bid response', function () {
it('should handle empty bid response', function () {
const response = {
body: {}
};
Expand All @@ -201,4 +236,10 @@ describe('AudienceRun bid adapter tests', function() {
expect(syncs).to.deep.equal([{type: 'iframe', url: 'https://ac.audiencerun.com/f/sync.html'}])
});
});

describe('onTimeout', function () {
it('should exists and be a function', () => {
expect(spec.onTimeout).to.exist.and.to.be.a('function');
});
});
});