From 9df0e9209e049fa9a087fa8c2ff48c2fe5181ffd Mon Sep 17 00:00:00 2001 From: z-sunshine <33084773+z-sunshine@users.noreply.github.com> Date: Fri, 22 Nov 2019 20:19:57 +0800 Subject: [PATCH] Create newborntownWeb adapter (#4455) * Create newborntownWeb adapter * only https protocol --- modules/newborntownWebBidAdapter.js | 156 ++++++++++++++++++ modules/newborntownWebBidAdapter.md | 35 ++++ .../modules/newborntownWebBidAdapter_spec.js | 152 +++++++++++++++++ 3 files changed, 343 insertions(+) create mode 100644 modules/newborntownWebBidAdapter.js create mode 100644 modules/newborntownWebBidAdapter.md create mode 100644 test/spec/modules/newborntownWebBidAdapter_spec.js diff --git a/modules/newborntownWebBidAdapter.js b/modules/newborntownWebBidAdapter.js new file mode 100644 index 00000000000..103fac7ba6a --- /dev/null +++ b/modules/newborntownWebBidAdapter.js @@ -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); diff --git a/modules/newborntownWebBidAdapter.md b/modules/newborntownWebBidAdapter.md new file mode 100644 index 00000000000..f607369ffb6 --- /dev/null +++ b/modules/newborntownWebBidAdapter.md @@ -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 + } + } + ] + } + ]; +``` diff --git a/test/spec/modules/newborntownWebBidAdapter_spec.js b/test/spec/modules/newborntownWebBidAdapter_spec.js new file mode 100644 index 00000000000..d7cf4a7dd95 --- /dev/null +++ b/test/spec/modules/newborntownWebBidAdapter_spec.js @@ -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': "
", + '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'); + } + }); + }) +})