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

TrueReach Bidder Adapter #5423

Merged
merged 9 commits into from
Jul 17, 2020
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
128 changes: 128 additions & 0 deletions modules/truereachBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as utils from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER } from '../src/mediaTypes.js';

const SUPPORTED_AD_TYPES = [BANNER];
const BIDDER_CODE = 'truereach';
const BIDDER_URL = 'https://ads.momagic.com/exchange/openrtb25/';

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: SUPPORTED_AD_TYPES,

isBidRequestValid: function (bidRequest) {
return (bidRequest.params.site_id && bidRequest.params.bidfloor &&
utils.deepAccess(bidRequest, 'mediaTypes.banner') && (utils.deepAccess(bidRequest, 'mediaTypes.banner.sizes.length') > 0));
},

buildRequests: function (validBidRequests, bidderRequest) {
if (validBidRequests.length === 0) {
return [];
}

let queryParams = buildCommonQueryParamsFromBids(validBidRequests, bidderRequest);

let siteId = utils.deepAccess(validBidRequests[0], 'params.site_id');

let url = BIDDER_URL + siteId + '?hb=1&transactionId=' + validBidRequests[0].transactionId;

return {
method: 'POST',
url: url,
data: queryParams,
options: { withCredentials: true }
};
},

interpretResponse: function ({ body: serverResponse }, serverRequest) {
const bidResponses = [];

if ((!serverResponse || !serverResponse.id) ||
(!serverResponse.seatbid || serverResponse.seatbid.length === 0 || !serverResponse.seatbid[0].bid || serverResponse.seatbid[0].bid.length === 0)) {
return bidResponses;
}

let adUnits = serverResponse.seatbid[0].bid;
let bidderBid = adUnits[0];

let responseCPM = parseFloat(bidderBid.price);
if (responseCPM === 0) {
return bidResponses;
}

let responseAd = bidderBid.adm;

if (bidderBid.nurl) {
let responseNurl = '<img src="' + bidderBid.nurl + '" height="0px" width="0px">';
responseAd += responseNurl;
}

const bidResponse = {
requestId: bidderBid.impid,
cpm: responseCPM,
currency: serverResponse.cur || 'USD',
width: parseInt(bidderBid.w),
height: parseInt(bidderBid.h),
ad: decodeURIComponent(responseAd),
ttl: 180,
creativeId: bidderBid.crid,
netRevenue: false
};
if (bidderBid.adomain && bidderBid.adomain.length) {
bidResponse.meta = {
advertiserDomains: bidderBid.adomain,
};
}

bidResponses.push(bidResponse);

return bidResponses;
},

};

function buildCommonQueryParamsFromBids(validBidRequests, bidderRequest) {
let adW = 0;
let adH = 0;
let adSizes = Array.isArray(validBidRequests[0].params.sizes) ? validBidRequests[0].params.sizes : validBidRequests[0].sizes;
let sizeArrayLength = adSizes.length;
if (sizeArrayLength === 2 && typeof adSizes[0] === 'number' && typeof adSizes[1] === 'number') {
adW = adSizes[0];
adH = adSizes[1];
} else {
adW = adSizes[0][0];
adH = adSizes[0][1];
}

let bidFloor = Number(utils.deepAccess(validBidRequests[0], 'params.bidfloor'));

let domain = window.location.host;
let page = window.location.host + window.location.pathname + location.search + location.hash;

let defaultParams = {
id: utils.getUniqueIdentifierStr(),
imp: [
{
id: validBidRequests[0].bidId,
banner: {
w: adW,
h: adH
},
bidfloor: bidFloor
}
],
site: {
domain: domain,
page: page
},
device: {
ua: window.navigator.userAgent
},
tmax: config.getConfig('bidderTimeout')
};

return defaultParams;
}

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

```
Module Name: TrueReach Bidder Adapter
Module Type: Bidder Adapter
Maintainer: [email protected]
```

# Description

Module that connects to TrueReach's demand sources

# Test Parameters
```
var adUnits = [{
code: 'test-banner',
mediaTypes: {
banner: {
sizes: [[300, 250]]
}
},
bids: [{
bidder: 'truereach',
params: {
site_id: '0142010a-8400-1b01-72cb-a553b9000009',
bidfloor: 0.1
}
}]
}];
```

# Bid Parameters

`mediaTypes -> banner -> sizes` must be `defined`.

Also, the following parameters are `required` to be set-

| Name | Type | Description
| ---- | ---- | -----------
| `site_id` | String | TrueReach provided site ID
| `bidfloor` | Number | Minimum price (CPM) in USD. Must be greater than 0.

# Additional Details
[TrueReach Ads](http://doc.truereach.co.in/docs/prebid/js-bidder-adapter.html)
84 changes: 84 additions & 0 deletions test/spec/modules/truereachBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'chai';
import { spec } from 'modules/truereachBidAdapter.js';

describe('truereachBidAdapterTests', function () {
it('validate_pub_params', function () {
expect(spec.isBidRequestValid({
mediaTypes: {
banner: {
sizes: [[300, 250]]
}
},
bidder: 'truereach',
params: {
site_id: '0142010a-8400-1b01-72cb-a553b9000009',
bidfloor: 0.1
}
})).to.equal(true);
});

it('validate_generated_params', function () {
let bidRequestData = [{
bidId: '34ce3f3b15190a',
mediaTypes: {
banner: {
sizes: [[300, 250]]
}
},
bidder: 'truereach',
params: {
site_id: '0142010a-8400-1b01-72cb-a553b9000009',
bidfloor: 0.1
},
sizes: [[300, 250]]
}];

let request = spec.buildRequests(bidRequestData, {});
let req_data = request.data;

expect(request.method).to.equal('POST');
expect(req_data.imp[0].id).to.equal('34ce3f3b15190a');
expect(req_data.imp[0].banner.w).to.equal(300);
expect(req_data.imp[0].banner.h).to.equal(250);
expect(req_data.imp[0].bidfloor).to.equal(0.1);
});

it('validate_response_params', function () {
let serverResponse = {
body: {
'id': '34ce3f3b15190a',
'seatbid': [{
'bid': [{
'id': '0142010a-8400-0801-72dc-04a99e6f7fc0:1ed',
'impid': '34ce3f3b15190a',
'price': 2.55,
'adm': '<html></html>',
'adid': '493',
'adomain': ['https://www.momagic.com/'],
'iurl': '',
'cid': '260',
'crid': '0142010a-8400-1b01-72cb-afb296000012',
'w': 300,
'h': 250
}]
}],
'bidid': '0142010a-8400-0801-72dc-04a99e6f7fc1',
'cur': 'USD'
}
};

let bids = spec.interpretResponse(serverResponse, {});
expect(bids).to.have.lengthOf(1);
let bid = bids[0];
expect(bid.requestId).to.equal('34ce3f3b15190a');
expect(bid.cpm).to.equal(2.55);
expect(bid.currency).to.equal('USD');
expect(bid.width).to.equal(300);
expect(bid.height).to.equal(250);
expect(bid.ad).to.equal('<html></html>');
expect(bid.ttl).to.equal(180);
expect(bid.creativeId).to.equal('0142010a-8400-1b01-72cb-afb296000012');
expect(bid.netRevenue).to.equal(false);
expect(bid.meta.advertiserDomains[0]).to.equal('https://www.momagic.com/');
});
});