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

HypeLab Bid Adapter: Initial Release #10003

Merged
merged 2 commits into from
Jun 14, 2023
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
153 changes: 153 additions & 0 deletions modules/hypelabBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import { generateUUID } from '../src/utils.js';
import { ajax } from '../src/ajax.js';

export const BIDDER_CODE = 'hypelab';
export const ENDPOINT_URL = 'https://api.hypelab.com';

export const REQUEST_ROUTE = '/v1/prebid_requests';
export const EVENT_ROUTE = '/v1/events';
export const REPORTING_ROUTE = '';

const PREBID_VERSION = '$prebid.version$';
const PROVIDER_NAME = 'prebid';
const PROVIDER_VERSION = '0.0.1';

const url = (route) => ENDPOINT_URL + route;

export function mediaSize(data) {
if (!data || !data.creative_set) return { width: 0, height: 0 };
const media = data.creative_set.video || data.creative_set.image || {};
return { width: media.width, height: media.height };
}

function isBidRequestValid(bidderRequest) {
return (
!!bidderRequest.params?.property_slug &&
!!bidderRequest.params?.placement_slug
);
}

function buildRequests(validBidRequests, bidderRequest) {
const result = validBidRequests.map((request) => {
const uids = (request.userIdAsEids || []).reduce((a, c) => {
const ids = c.uids.map((uid) => uid.id);
return [...a, ...ids];
}, []);

const uuid = uids[0] ? uids[0] : generateTemporaryUUID();

const payload = {
property_slug: request.params.property_slug,
placement_slug: request.params.placement_slug,
provider_version: PROVIDER_VERSION,
provider_name: PROVIDER_NAME,
referrer:
bidderRequest.refererInfo?.page || typeof window != 'undefined'
? window.location.href
: '',
sdk_version: PREBID_VERSION,
sizes: request.sizes,
wids: [],
uuid,
bidRequestsCount: request.bidRequestsCount,
bidderRequestsCount: request.bidderRequestsCount,
bidderWinsCount: request.bidderWinsCount,
wp: {
ada: typeof window != 'undefined' && !!window.cardano,
bnb: typeof window != 'undefined' && !!window.BinanceChain,
eth: typeof window != 'undefined' && !!window.ethereum,
sol: typeof window != 'undefined' && !!window.solana,
tron: typeof window != 'undefined' && !!window.tron,
},
};

return {
method: 'POST',
url: url(REQUEST_ROUTE),
options: { contentType: 'application/json', withCredentials: false },
data: payload,
bidId: request.bidId,
};
});

return result;
}

function generateTemporaryUUID() {
return 'tmp_' + generateUUID();
}

function interpretResponse(serverResponse, bidRequest) {
const { data } = serverResponse.body;

if (!data.cpm || !data.html) return [];

const size = mediaSize(data);

const result = {
requestId: bidRequest.bidId,
cpm: data.cpm,
width: size.width,
height: size.height,
creativeId: data.creative_set_slug,
currency: data.currency,
netRevenue: true,
referrer: bidRequest.data.referrer,
ttl: data.ttl,
ad: data.html,
mediaType: serverResponse.body.data.media_type,
meta: {
advertiserDomains: data.advertiserDomains || [],
},
};

return [result];
}

export function report(eventType, data, route = REPORTING_ROUTE) {
if (!route) return;

const options = {
method: 'POST',
contentType: 'application/json',
withCredentials: true,
};

const request = { type: eventType, data };
ajax(url(route), null, request, options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if a bid adapter is allowed to make external ajax calls. I'm checking with others on this but according to me, having an analytics adapter or a separate integration with an analytics provider is more appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Let me know what you find. Here are a few other places where ajax POST requests are being used to record onBidWon and other callbacks:

postRequest(endpoint, data) {

onBidWon: function (bid) {

send_metrics: function() {

}

function onTimeout(timeoutData) {
this.report('timeout', timeoutData);
}

function onBidWon(bid) {
this.report('bidWon', bid);
}

function onSetTargeting(bid) {
this.report('setTargeting', bid);
}

function onBidderError(errorData) {
this.report('bidderError', errorData);
}

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
aliases: ['hype'],
isBidRequestValid,
buildRequests,
interpretResponse,
onTimeout,
onBidWon,
onSetTargeting,
onBidderError,
report,
REPORTING_ROUTE: 'a',
};

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

```
Module Name: HypeLab Bid Adapter
Module Type: Bidder Adapter
Maintainer: [email protected]
```

# Description

Prebid.JS adapter that connects to HypeLab ad network for bids.
*NOTE*: The HypeLab Adapter requires setup and approval before use. Please reach out to `[email protected]` for more details. To get started, replace the `property_slug` with your property_slug and the `placement_slug` with your placement slug to receive bids. The placement slug will depend on the required size and can be set via the HypeLab interface.

# Test Parameters

## Sample Banner Ad Unit

```js
var adUnits = [
{
code: 'banner-div',
mediaTypes: {
banner: {
sizes: [[728, 90]],
},
},
bids: [
{
bidder: 'hypelab',
params: {
property_slug: 'prebid',
placement_slug: 'test_placement'
}
}
]
}
]
```
Loading