Skip to content

Commit

Permalink
Unicorn BidAdapter: add support for meta.advertiserDomain for Prebid 5 (
Browse files Browse the repository at this point in the history
#7241)

* Readd unicornBidAdapter

* remove adomain from test code, because not support adomain

* remove bidFloorCpm because not support

* Support adomain
  • Loading branch information
faithnh committed Jul 30, 2021
1 parent 09ea8e2 commit 3e71ab4
Show file tree
Hide file tree
Showing 3 changed files with 696 additions and 1 deletion.
160 changes: 160 additions & 0 deletions modules/unicornBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import * as utils from '../src/utils.js';
import {BANNER} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getStorageManager} from '../src/storageManager.js';

const storage = getStorageManager();
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';
const UNICORN_PB_VERSION = '1.0';

/**
* 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 => {
return {
id: br.bidId,
banner: {
format: makeFormat(br.sizes),
w: br.sizes[0][0],
h: br.sizes[0][1]
},
tagid: utils.deepAccess(br, 'params.placementId') || br.adUnitCode,
secure: 1,
bidfloor: parseFloat(0)
};
});
const request = {
id: bidderRequest.auctionId,
at: 1,
imp,
cur: UNICORN_DEFAULT_CURRENCY,
site: {
id: utils.deepAccess(validBidRequests[0], 'params.mediaId') || '',
publisher: {
id: utils.deepAccess(validBidRequests[0], 'params.publisherId') || 0
},
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,
prebid_version: UNICORN_PB_VERSION
}
},
ext: {
accountId: utils.deepAccess(validBidRequests[0], 'params.accountId')
}
};
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 => {
var bid = {
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
}

if (b.adomain != undefined || b.adomain != null) {
bid.meta = { advertiserDomains: b.adomain };
}

bids.push(bid)
})
});
}
utils.logInfo('[UNICORN] interpretResponse bids:', bids);
return bids;
};

/**
* Get or Create Uid for First Party Cookie
*/
const getUid = () => {
const ck = storage.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();
storage.setCookie(UNICORN_PB_COOKIE_KEY, JSON.stringify(newCk), expireIn);
return newCk.uid;
}
};

/**
* Make imp.banner.format
* @param {Array<Number>} arr
*/
const makeFormat = arr => arr.map((s) => {
return {w: s[0], h: s[1]};
});

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

registerBidder(spec);
1 change: 0 additions & 1 deletion modules/unicornBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Module that connects to UNICORN.
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
publisherId: 99999 // OPTIONAL: Account specific publisher id
mediaId: "uc" // OPTIONAL: Publisher specific media id
accountId: 12345, // REQUIRED: Account ID for charge request
Expand Down
Loading

0 comments on commit 3e71ab4

Please sign in to comment.