Skip to content

Commit

Permalink
Add ADSpend bidder adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
loorke committed Aug 22, 2018
1 parent e1e03db commit 5da6ee3
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
158 changes: 158 additions & 0 deletions modules/adspendBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import * as utils from 'src/utils';
import { config } from 'src/config';
import { registerBidder } from 'src/adapters/bidderFactory';
import { BANNER } from 'src/mediaTypes';

const BIDDER_CODE = 'adspend';
const BID_URL = '//rtb.com.ru/headerbidding-bid';
const SYNC_URL = '//rtb.com.ru/headerbidding-sync?uid={UUID}';
const COOKIE_NAME = 'hb-adspend-id';
const UUID_LEN = 36;
const COOKIE_EXPIRE = 600;

export const spec = {
code: BIDDER_CODE,
aliases: ['adspend'],
supportedMediaTypes: [BANNER],

onBidWon: function(winObj) {
const requestId = winObj.requestId;
const cpm = winObj.cpm;
const event = getWinEvent(requestId).replace(/\$\{AUCTION_PRICE\}/, cpm);

fetch(event);
},

isBidRequestValid: function(bid) {
const conf = config.getConfig();
return !!(conf.currency &&
conf.currency.adServerCurrency &&
bid.crumbs.pubcid &&
utils.checkCookieSupport() &&
utils.cookiesAreEnabled()
);
},

buildRequests: function(bidRequests, bidderRequest) {
console.log('DEBUG 1');
console.log(bidRequests);
console.log(bidderRequest);

const req = bidRequests[Math.floor(Math.random() * bidRequests.length)];

const conf = config.getConfig();
const cur = conf.currency.adServerCurrency;

const bidfloor = req.params.bidfloor;

const payload = {
'id': req.bidId,
'site': {
'id': req.crumbs.pubcid,
'domain': document.domain
},
'device': {
'ua': navigator.userAgent,
'ip': ''
},
'user': { 'id': getUserID(document.cookie) },
'imp': [
{
'id': req.params.placement,
'tagId': req.params.tagId,
'banner': { 'format': getFormats(req.sizes) },
'bidfloor': bidfloor !== undefined ? Number(bidfloor) : 1,
'bidfloorcur': cur,
'secure': 0
}
],
'cur': [
cur
],
'tmax': bidderRequest.timeout
};

return {
method: 'POST',
url: BID_URL,
data: JSON.stringify(payload),
};
},

interpretResponse: function(resp, {bidderRequest}) {
alert('Hello, world!');
console.log('DEBUG 2');
console.log(resp);

const bids = [];

if (resp.body === '') return bids;

const respBid = resp.body.seatbid[0].bid[0];
const requestId = resp.body.id;

document.cookie =
`on_win_event__${requestId}=${respBid.nurl}; max-age=${COOKIE_EXPIRE}`;

const bid = {
cpm: respBid.price,
requestId: resp.body.id,
width: 300,
height: 250,
creativeId: respBid.adid,
dealId: respBid.dealid,
currency: resp.body.cur,
netRevenue: true,
ttl: 10000,
ad: respBid.adm
};

bids.push(bid);
return bids;
},

getUserSyncs: function(syncOptions, resps) {
let syncs = [];

resps.forEach(resp => {
if (syncOptions.pixelEnabled && resp.body === '') {
const uuid = getUserID();
syncs.push({
type: 'image',
url: SYNC_URL.replace('{UUID}', uuid),
});
}
});

return syncs
}
}

const getUserID = () => {
const i = document.cookie.indexOf(COOKIE_NAME);

if (i === -1) {
const uuid = utils.generateUUID();
document.cookie = `${COOKIE_NAME}=${uuid}; path=/`;
return uuid;
}

const j = i + COOKIE_NAME.length + 1;
return document.cookie.substring(j, j + UUID_LEN);
};

const getWinEvent = (requestId) => {
const cookieName = `on_win_event__${requestId}`;
const event = document.cookie
.split(';')
.find(item => item.includes(cookieName))
.split(`${cookieName}=`)[1];

return event;
};

const getFormats = arr => arr.map((s) => {
return { w: s[0], h: s[1] };
});

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

```
Module Name: AdSpend Bidder Adapter
Module Type: Bidder Adapter
Maintainer: gaffoonster@gmail.com
```

# Description

Connects to AdSpend bidder.
AdSpend adapter supports only Banner at the moment. Video and Native will be add soon.

# Test Parameters
```
var adUnits = [
// Banner
{
code: 'any_code',
mediaTypes: {
banner: {
// You can choose one of them
sizes: [
[300, 250],
[300, 600],
[240, 400],
[728, 90],
]
}
},
bids: [
{
bidder: "adspend",
params: {
// These params is required for getting test banner
placement: 'test',
tagId: 'test-ad',
}
}
]
}
];
pbjs.que.push(() => {
pbjs.setConfig({
userSync: {
syncEnabled: true,
enabledBidders: ['adspend'],
pixelEnabled: true,
syncsPerBidder: 200,
syncDelay: 100,
},
currency: {
adServerCurrency: 'RUB' // We work only with rubles for now
}
});
});
```

0 comments on commit 5da6ee3

Please sign in to comment.