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

Caroda Bid Adapter: initial adapter release #8845

Merged
merged 22 commits into from
Sep 15, 2022
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
217 changes: 217 additions & 0 deletions modules/carodaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// jshint esversion: 6, es3: false, node: true
'use strict'

import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import {
deepAccess,
deepSetValue,
logError,
mergeDeep,
parseSizesInput
} from '../src/utils.js';
import { config } from '../src/config.js';

const { getConfig } = config;

const BIDDER_CODE = 'caroda';
const GVLID = 954;

// some state info is required to synchronize with Caroda ad server
const topUsableWindow = getTopUsableWindow();

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid: bid => {
const params = bid.params || {};
const { ctok, placementId, priceType } = params;
return typeof ctok === 'string' && (
typeof placementId === 'string' ||
typeof placementId === 'undefined'
) && (
typeof priceType === 'undefined' ||
priceType === 'gross' ||
priceType === 'net'
);
},
buildRequests: (validBidRequests, bidderRequest) => {
topUsableWindow.carodaPageViewId = topUsableWindow.carodaPageViewId || Math.floor(Math.random() * 1e9);
const pageViewId = topUsableWindow.carodaPageViewId;
const ortbCommon = getORTBCommon(bidderRequest);
const priceType =
getFirstWithKey(validBidRequests, 'params.priceType') ||
lksharma marked this conversation as resolved.
Show resolved Hide resolved
'net';
const test = getFirstWithKey(validBidRequests, 'params.test');
const currency = getConfig('currency.adServerCurrency');
const eids = getFirstWithKey(validBidRequests, 'userIdAsEids');
const schain = getFirstWithKey(validBidRequests, 'schain');
const request = {
auctionId: bidderRequest.auctionId,
currency,
hb_version: '$prebid.version$',
...ortbCommon,
price_type: priceType
};
if (test) {
request.test = 1;
}
if (schain) {
request.schain = schain;
}
if (config.getConfig('coppa')) {
deepSetValue(request, 'privacy.coppa', 1);
}
if (deepAccess(bidderRequest, 'gdprConsent.gdprApplies') !== undefined) {
deepSetValue(
request,
'privacy.gdpr_consent',
bidderRequest.gdprConsent.consentString
);
deepSetValue(
request,
'privacy.gdpr',
bidderRequest.gdprConsent.gdprApplies & 1
);
}
if (bidderRequest.uspConsent) {
deepSetValue(request, 'privacy.us_privacy', bidderRequest.uspConsent);
}
if (eids) {
deepSetValue(request, 'user.eids', eids);
}
return getImps(validBidRequests, request).map(imp => ({
method: 'POST',
url: 'https://prebid.caroda.io/api/hb?entry_id=' + pageViewId,
data: JSON.stringify(imp)
}));
},
interpretResponse: (serverResponse) => {
if (!serverResponse.body) {
return;
}
const { ok, error } = serverResponse.body
if (error) {
logError(BIDDER_CODE, ': server caught', error.message);
return;
}
try {
return JSON.parse(ok.value)
.map((bid) => {
const ret = {
requestId: bid.bid_id,
cpm: bid.cpm,
creativeId: bid.creative_id,
ttl: 300,
netRevenue: true,
currency: bid.currency,
width: bid.w,
height: bid.h,
meta: {
advertiserDomains: bid.adomain || []
},
ad: bid.ad,
placementId: bid.placement_id
}
if (bid.adserver_targeting) {
ret.adserverTargeting = bid.adserver_targeting
}
return ret
})
.filter(Boolean);
} catch (e) {
logError(BIDDER_CODE, ': caught', e);
}
}
}

registerBidder(spec)

function getFirstWithKey (collection, key) {
for (let i = 0, result; i < collection.length; i++) {
result = deepAccess(collection[i], key);
if (result) {
return result;
}
}
}

function getTopUsableWindow () {
let res = window;
try {
while (window.top !== res && res.parent.location.href.length) {
res = res.parent;
}
} catch (e) {}
return res;
}

function getORTBCommon (bidderRequest) {
let app, site;
const commonFpd = bidderRequest.ortb2 || {};
lksharma marked this conversation as resolved.
Show resolved Hide resolved
let { user } = commonFpd;
if (typeof getConfig('app') === 'object') {
app = getConfig('app') || {}
if (commonFpd.app) {
mergeDeep(app, commonFpd.app);
}
} else {
site = getConfig('site') || {};
if (commonFpd.site) {
mergeDeep(site, commonFpd.site);
}
if (!site.page) {
site.page = bidderRequest.refererInfo.page;
}
}
const device = getConfig('device') || {};
device.w = device.w || window.innerWidth;
device.h = device.h || window.innerHeight;
device.ua = device.ua || navigator.userAgent;
return {
app,
site,
user,
device
};
}

function getImps (validBidRequests, common) {
return validBidRequests.map((bid) => {
const floorInfo = bid.getFloor
? bid.getFloor({ currency: common.currency || 'EUR' })
: {};
const bidfloor = floorInfo.floor;
const bidfloorcur = floorInfo.currency;
const { ctok, placementId } = bid.params;
const imp = {
bid_id: bid.bidId,
ctok,
bidfloor,
bidfloorcur,
...common
};
const bannerParams = deepAccess(bid, 'mediaTypes.banner');
if (bannerParams && bannerParams.sizes) {
const sizes = parseSizesInput(bannerParams.sizes);
const format = sizes.map(size => {
const [width, height] = size.split('x');
const w = parseInt(width, 10);
const h = parseInt(height, 10);
return { w, h };
});
imp.banner = {
format
};
}
if (placementId) {
imp.placement_id = placementId;
}
const videoParams = deepAccess(bid, 'mediaTypes.video');
if (videoParams) {
imp.video = videoParams;
}
return imp;
})
}
43 changes: 43 additions & 0 deletions modules/carodaBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Overview

Module Name: Caroda Adapter
Module Type: Bidder Adapter
Maintainer: dev@caroda.io

# Description

Module that connects to Caroda demand sources to fetch bids.
Banner and video formats are supported.
Use `caroda` as bidder.

# Test Parameters
```
var adUnits = [{
code: '/19968336/prebid_banner_example_1',
mediaTypes: {
banner: {
sizes: [[ 300, 250 ]]
}
}
bids: [{
bidder: 'caroda',
params: {
ctok: '230ce9490c5434354'
}
}]
}, {
code: '/19968336/prebid_video_example_1',
mediaTypes: {
video: {
context: 'outstream',
mimes: ['video/mp4']
}
}
bids: [{
bidder: 'caroda',
params: {
ctok: '230ce9490c5434354'
}
}]
}];
```
Loading