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

Create newborntownWeb adapter #4455

Merged
merged 2 commits into from
Nov 22, 2019
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
156 changes: 156 additions & 0 deletions modules/newborntownWebBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import * as utils from '../src/utils';
import {registerBidder} from '../src/adapters/bidderFactory';
import {BANNER, NATIVE} from '../src/mediaTypes';
const BIDDER_CODE = 'newborntownWeb';

const REQUEST_URL = 'https://us-west.solortb.com/adx/api/rtb?from=4'

function randomn(n) {
return parseInt((Math.random() + 1) * Math.pow(10, n - 1)) + '';
}
function generateGUID() {
var d = new Date().getTime();
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
})
return guid;
}
function _isMobile() {
return (/(ios|ipod|ipad|iphone|android)/i).test(navigator.userAgent);
}
function _isConnectedTV() {
return (/(smart[-]?tv|hbbtv|appletv|googletv|hdmi|netcast\.tv|viera|nettv|roku|\bdtv\b|sonydtv|inettvbrowser|\btv\b)/i).test(navigator.userAgent);
}
function _getDeviceType() {
return _isMobile() ? 1 : _isConnectedTV() ? 3 : 2;
}
var platform = (function getPlatform() {
var ua = navigator.userAgent;
if (ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1) {
return 'Android'
}
if (ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
return 'iOS'
}
return 'windows'
})();
function getLanguage() {
const language = navigator.language ? 'language' : 'userLanguage';
return navigator[language].split('-')[0];
}
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
isBidRequestValid: function(bid) {
return !!(bid.params.publisher_id && bid.params.slot_id && bid.params.bidfloor);
},

buildRequests: function(validBidRequests, bidderRequest) {
let requestArr = []
if (validBidRequests.length === 0) {
return null;
}
var guid;
if (localStorage.getItem('sax_user_id') == null) {
localStorage.setItem('sax_user_id', generateGUID())
}
guid = localStorage.getItem('sax_user_id')
utils._each(validBidRequests, function(bidRequest) {
const bidRequestObj = bidRequest.params
var req = {
id: randomn(12) + randomn(12),
tmax: bidderRequest.timeout,
bidId: bidRequest.bidId,
user: {
id: guid
},
imp: [
{
id: '1',
bidfloor: bidRequestObj.bidfloor,
bidfloorcur: 'USD',
banner: {
w: 0,
h: 0
}
}
],
site: {
domain: window.location.host,
id: bidRequestObj.slot_id,
page: window.location.href,
publisher: {
id: bidRequestObj.publisher_id
},
},
device: {
ip: '',
ua: navigator.userAgent,
os: platform,
geo: {
country: '',
type: 0,
ipservice: 1,
region: '',
city: '',
},
language: getLanguage(),
devicetype: _getDeviceType()
},
ext: {
solomath: {
slotid: bidRequestObj.slot_id
}
}
};
var sizes = bidRequest.sizes;
if (sizes) {
if (sizes && utils.isArray(sizes[0])) {
req.imp[0].banner.w = sizes[0][0];
req.imp[0].banner.h = sizes[0][1];
} else if (sizes && utils.isNumber(sizes[0])) {
req.imp[0].banner.w = sizes[0];
req.imp[0].banner.h = sizes[1];
}
} else {
return false;
}
const options = {
withCredentials: false
}
requestArr.push({
method: 'POST',
url: REQUEST_URL,
data: req,
bidderRequest,
options: options
})
})
return requestArr;
},
interpretResponse: function(serverResponse, request) {
var bidResponses = [];
if (serverResponse.body.seatbid && serverResponse.body.seatbid.length > 0 && serverResponse.body.seatbid[0].bid && serverResponse.body.seatbid[0].bid.length > 0 && serverResponse.body.seatbid[0].bid[0].adm) {
utils._each(serverResponse.body.seatbid[0].bid, function(bodyAds) {
var adstr = '';
adstr = bodyAds.adm;
var bidResponse = {
requestId: request.data.bidId || 0,
cpm: bodyAds.price || 0,
width: bodyAds.w ? bodyAds.w : 0,
height: bodyAds.h ? bodyAds.h : 0,
ad: adstr,
netRevenue: true,
currency: serverResponse.body.cur || 'USD',
ttl: 600,
creativeId: bodyAds.cid
};
bidResponses.push(bidResponse);
});
}
return bidResponses;
}
}
registerBidder(spec);
35 changes: 35 additions & 0 deletions modules/newborntownWebBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Overview

```
Module Name: NewborntownWeb Bidder Adapter
Module Type: Bidder Adapter
Maintainer: zhuyushuang@newborntown.com
```

# Description

Integration for website

# Test Parameters
```
var adUnits = [
{
code: '/19968336/header-bid-tag-1',
mediaTypes: {
banner: {
sizes: [[300, 250]]
}
},
bids: [
{
bidder: "newborntownWeb",
params: {
'publisher_id': '1238122',
'slot_id': '123123',
'bidfloor': 0.2
}
}
]
}
];
```
152 changes: 152 additions & 0 deletions test/spec/modules/newborntownWebBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { expect } from 'chai';
import {spec} from 'modules/newborntownWebBidAdapter';
describe('NewborntownWebAdapter', function() {
describe('isBidRequestValid', function () {
let bid = {
'bidder': 'newborntownWeb',
'params': {
'publisher_id': '1238122',
'slot_id': '123123',
'bidfloor': 0.3
},
'adUnitCode': '/19968336/header-bid-tag-1',
'sizes': [[300, 250]],
'bidId': '2e9cf65f23dbd9',
'bidderRequestId': '1f01d9d22ee657',
'auctionId': '2bf455a4-a889-41d5-b48f-9b56b89fbec7',
}
it('should return true where required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});
it('should return false when required params are not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.params = {};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});
})
describe('buildRequests', function () {
let bidderRequest = {
'bidderCode': 'newborntownWeb',
'bidderRequestId': '1f5c279a4c5de3',
'bids': [
{
'bidder': 'newborntownWeb',
'params': {
'publisher_id': '1238122',
'slot_id': '123123',
'bidfloor': 0.3
},
'mediaTypes': {
'banner': {'sizes': [[300, 250]]}
},
'adUnitCode': '/19968336/header-bid-tag-1',
'transactionId': '9b954797-d6f4-4730-9cbe-5a1bc8480f52',
'sizes': [[300, 250]],
'bidId': '215f48d07eb8b8',
'bidderRequestId': '1f5c279a4c5de3',
'auctionId': '5ed4f607-e11c-45b0-aba9-f67768e1f9f4',
'src': 'client',
'bidRequestsCount': 1
}
],
'auctionStart': 1573123289380,
'timeout': 9000,
'start': 1573123289383
}
const request = spec.buildRequests(bidderRequest['bids'], bidderRequest);
it('Returns POST method', function () {
expect(request[0].method).to.equal('POST');
expect(request[0].url.indexOf('//us-west.solortb.com/adx/api/rtb?from=4') !== -1).to.equal(true);
expect(request[0].data).to.exist;
});
it('request params multi size format object check', function () {
let bidderRequest = {
'bidderCode': 'newborntownWeb',
'bidderRequestId': '1f5c279a4c5de3',
'bids': [
{
'bidder': 'newborntownWeb',
'params': {
'publisher_id': '1238122',
'slot_id': '123123',
'bidfloor': 0.3
},
'mediaTypes': {
'native': {'sizes': [[300, 250]]}
},
'adUnitCode': '/19968336/header-bid-tag-1',
'transactionId': '9b954797-d6f4-4730-9cbe-5a1bc8480f52',
'sizes': [300, 250],
'bidId': '215f48d07eb8b8',
'bidderRequestId': '1f5c279a4c5de3',
'auctionId': '5ed4f607-e11c-45b0-aba9-f67768e1f9f4',
'src': 'client',
'bidRequestsCount': 1
}
],
'auctionStart': 1573123289380,
'timeout': 9000,
'start': 1573123289383
}
let requstTest = spec.buildRequests(bidderRequest['bids'], bidderRequest)
expect(requstTest[0].data.imp[0].banner.w).to.equal(300);
expect(requstTest[0].data.imp[0].banner.h).to.equal(250);
});
})
describe('interpretResponse', function () {
let serverResponse;
let bidRequest = {
data: {
bidId: '2d359291dcf53b'
}
};
beforeEach(function () {
serverResponse = {
'body': {
'id': '174548259807190369860081',
'seatbid': [
{
'bid': [
{
'id': '1573540665390298996',
'impid': '1',
'price': 0.3001,
'adid': '1573540665390299172',
'nurl': 'https://us-west.solortb.com/winnotice?price=${AUCTION_PRICE}&ssp=4&req_unique_id=740016d1-175b-4c19-9744-58a59632dabe&unique_id=06b08e40-2489-439a-8f9e-6413f3dd0bc8&isbidder=1&up=bQyvVo7tgbBVW2dDXzTdBP95Mv35YqqEika0T_btI1h6xjqA8GSXQe51_2CCHQcfuwAEOgdwN8u3VgUHmCuqNPKiBmIPaYUOQBBKjJr05zeKtabKnGT7_JJKcurrXqQ5Sl804xJear_qf2-jOaKB4w',
'adm': "<div id='grumi-container'></div>",
'adomain': [
'newborntown.com'
],
'iurl': 'https://sdkvideo.s3.amazonaws.com/4aa1d9533c4ce71bb1cf750ed38e3a58.png',
'cid': '345',
'crid': '41_11113',
'cat': [
''
],
'h': 250,
'w': 300
}
],
'seat': '1'
}
],
'bidid': 'bid1573540665390298585'
},
'headers': {

}
}
});
it('result is correct', function () {
const result = spec.interpretResponse(serverResponse, bidRequest);
if (result && result[0]) {
expect(result[0].requestId).to.equal('2d359291dcf53b');
expect(result[0].cpm).to.equal(0.3001);
expect(result[0].width).to.equal(300);
expect(result[0].height).to.equal(250);
expect(result[0].creativeId).to.equal('345');
}
});
})
})