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 ADSpend bidder adapter #3005

Merged
merged 7 commits into from
Oct 1, 2018
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
165 changes: 165 additions & 0 deletions modules/adspendBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import * as utils from 'src/utils';
import { ajax } from 'src/ajax'
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 TTL = 10000;
const RUB = 'RUB';
const FIRST_PRICE = 1;
const NET_REVENUE = true;

const winEventURLs = {};
const placementToBidMap = {};

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

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

ajax(event, null);
},

isBidRequestValid: function(bid) {
const adServerCur = config.getConfig('currency.adServerCurrency') === RUB;

return !!(adServerCur &&
bid.params &&
bid.params.bidfloor &&
bid.crumbs.pubcid &&
utils.checkCookieSupport() &&
utils.cookiesAreEnabled()
);
},

buildRequests: function(bidRequests, bidderRequest) {
const req = bidRequests[Math.floor(Math.random() * bidRequests.length)];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please explain why you are only responding to random bid requests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made some changes. Do you still have questions?

const bidId = req.bidId;
const at = FIRST_PRICE;
const site = { id: req.crumbs.pubcid, domain: document.domain };
const device = { ua: navigator.userAgent, ip: '' };
const user = { id: getUserID() }
const cur = [ RUB ];
const tmax = bidderRequest.timeout;

const imp = bidRequests.map(req => {
const params = req.params;

const tagId = params.tagId;
const id = params.placement;
const banner = { 'format': getFormats(req.sizes) };
const bidfloor = params.bidfloor !== undefined
? Number(params.bidfloor) : 1;
const bidfloorcur = RUB;

placementToBidMap[id] = bidId;

return {
id,
tagId,
banner,
bidfloor,
bidfloorcur,
secure: 0,
};
});

const payload = {
bidId,
at,
site,
device,
user,
imp,
cur,
tmax,
};

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

interpretResponse: function(resp, {bidderRequest}) {
if (resp.body === '') return [];

const bids = resp.body.seatbid[0].bid.map(bid => {
const cpm = bid.price;
const impid = bid.impid;
const requestId = placementToBidMap[impid];
const width = bid.w;
const height = bid.h;
const creativeId = bid.adid;
const dealId = bid.dealid;
const currency = resp.body.cur;
const netRevenue = NET_REVENUE;
const ttl = TTL;
const ad = bid.adm;

return {
cpm,
requestId,
width,
height,
creativeId,
dealId,
currency,
netRevenue,
ttl,
ad,
};
});

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 getFormats = arr => arr.map((s) => {
return { w: s[0], h: s[1] };
});

registerBidder(spec);
60 changes: 60 additions & 0 deletions modules/adspendBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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: 'div-gpt-ad-1460505748561-0',
mediaTypes: {
banner: {
// You can choose one of them
sizes: [
[300, 250],
[300, 600],
[240, 400],
[728, 90],
]
}
},
bids: [
{
bidder: "adspend",
params: {
bidfloor: 1,
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
}
});
});
```

**It's a test banner, so you'll see some errors in console cause it will be trying to call our system's events.**