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

add UNICORN bid adapter #4917

Merged
merged 5 commits into from
Mar 12, 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
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