Skip to content

Commit

Permalink
add UNICORN bid adapter (prebid#4917)
Browse files Browse the repository at this point in the history
* add UNICORN bid adapter

* fix not using flatMap

* fix symbol error

* care lint & empty response

* care lint & add tests
  • Loading branch information
ctylim authored and rjvelicaria committed Apr 9, 2020
1 parent 2bccacc commit d1d144f
Show file tree
Hide file tree
Showing 3 changed files with 638 additions and 0 deletions.
144 changes: 144 additions & 0 deletions modules/unicornBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as utils from '../src/utils.js';
import { BANNER } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
const BIDDER_CODE = 'unicorn';
const UNICORN_ENDPOINT = 'https://ds.uncn.jp/pb/0/bid.json';
const UNICORN_DEFAULT_CURRENCY = 'JPY';
const UNICORN_PB_COOKIE_KEY = '__pb_unicorn_aud';

/**
* Placement ID and Account ID are required.
* @param {BidRequest} bidRequest
* @returns {boolean}
*/
const isBidRequestValid = bidRequest => {
return !!bidRequest.adUnitCode && !!bidRequest.params.accountId;
};

/**
* @param {Array<BidRequest>} validBidRequests
* @param {any} bidderRequest
* @returns {ServerRequest}
*/
export const buildRequests = (validBidRequests, bidderRequest) => {
return {
method: 'POST',
url: UNICORN_ENDPOINT,
data: buildOpenRtbBidRequestPayload(validBidRequests, bidderRequest)
};
};

/**
* Transform BidRequest to OpenRTB-formatted BidRequest Object
* @param {Array<BidRequest>} validBidRequests
* @param {any} bidderRequest
* @returns {string}
*/
function buildOpenRtbBidRequestPayload(validBidRequests, bidderRequest) {
utils.logInfo(
'[UNICORN] buildOpenRtbBidRequestPayload.validBidRequests:',
validBidRequests
);
utils.logInfo(
'[UNICORN] buildOpenRtbBidRequestPayload.bidderRequest:',
bidderRequest
);
const imp = validBidRequests.map(br => {
const sizes = utils.parseSizesInput(br.sizes)[0];
return {
id: br.bidId,
banner: {
w: sizes.split('x')[0],
h: sizes.split('x')[1]
},
tagid: utils.deepAccess(br, 'params.placementId') || br.adUnitCode,
secure: 1,
bidfloor: parseFloat(utils.deepAccess(br, 'params.bidfloorCpm') || 0)
};
});
const request = {
id: bidderRequest.auctionId,
at: 1,
imp,
cur: UNICORN_DEFAULT_CURRENCY,
site: {
id: window.location.hostname,
publisher: {
id: utils.deepAccess(validBidRequests[0], 'params.accountId')
},
domain: window.location.hostname,
page: window.location.href,
ref: bidderRequest.refererInfo.referer
},
device: {
language: navigator.language,
ua: navigator.userAgent
},
user: {
id: getUid()
},
bcat: utils.deepAccess(validBidRequests[0], 'params.bcat') || [],
source: {
ext: {
stype: 'prebid_uncn',
bidder: BIDDER_CODE
}
}
};
utils.logInfo('[UNICORN] OpenRTB Formatted Request:', request);
return JSON.stringify(request);
}

const interpretResponse = (serverResponse, request) => {
utils.logInfo('[UNICORN] interpretResponse.serverResponse:', serverResponse);
utils.logInfo('[UNICORN] interpretResponse.request:', request);
const res = serverResponse.body;
var bids = []
if (res) {
res.seatbid.forEach(sb => {
sb.bid.forEach(b => {
bids.push({
requestId: b.impid,
cpm: b.price || 0,
width: b.w,
height: b.h,
ad: b.adm,
ttl: 1000,
creativeId: b.crid,
netRevenue: false,
currency: res.cur
})
})
});
}
utils.logInfo('[UNICORN] interpretResponse bids:', bids);
return bids;
};

/**
* Get or Create Uid for First Party Cookie
*/
const getUid = () => {
const ck = utils.getCookie(UNICORN_PB_COOKIE_KEY);
if (ck) {
return JSON.parse(ck)['uid'];
} else {
const newCk = {
uid: utils.generateUUID()
};
const expireIn = new Date(Date.now() + 24 * 60 * 60 * 10000).toUTCString();
utils.setCookie(UNICORN_PB_COOKIE_KEY, JSON.stringify(newCk), expireIn);
return newCk.uid;
}
};

export const spec = {
code: BIDDER_CODE,
aliases: ['uncn'],
supportedMediaTypes: [BANNER],
isBidRequestValid,
buildRequests,
interpretResponse,
};

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

**Module Name**: UNICORN Bid Adapter
**Module Type**: Bidder Adapter
**Maintainer**: service+prebid.js@bulbit.jp

# Description

Module that connects to UNICORN.

# Test Parameters

```js
const adUnits = [{
code: 'test-adunit1', // REQUIRED: adunit code
mediaTypes: {
banner: {
sizes: [[300, 250]] // a banner size
}
},
bids: [{
bidder: 'unicorn',
params: {
placementId: 'rectangle-ad-1', // OPTIONAL: If placementId is empty, adunit code will be used as placementId.
bidfloorCpm: 0.2, // OPTIONAL: Floor CPM (JPY) defaults to 0
accountId: 12345, // REQUIRED: Account ID for charge request
bcat: ['IAB-1', 'IAB-2'] // OPTIONAL: blocked IAB categories
}
}]
}];
```
Loading

0 comments on commit d1d144f

Please sign in to comment.