forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adstirBidAdapter.js
92 lines (79 loc) · 2.89 KB
/
adstirBidAdapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as utils from '../src/utils.js';
import { config } from '../src/config.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
const BIDDER_CODE = 'adstir';
const ENDPOINT = 'https://ad.ad-stir.com/prebid'
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: function (bid) {
return !!(utils.isStr(bid.params.appId) && !utils.isEmptyStr(bid.params.appId) && utils.isInteger(bid.params.adSpaceNo));
},
buildRequests: function (validBidRequests, bidderRequest) {
const sua = utils.deepAccess(validBidRequests[0], 'ortb2.device.sua', null);
const requests = validBidRequests.map((r) => {
return {
method: 'POST',
url: ENDPOINT,
data: JSON.stringify({
appId: r.params.appId,
adSpaceNo: r.params.adSpaceNo,
auctionId: r.auctionId,
transactionId: r.transactionId,
bidId: r.bidId,
mediaTypes: r.mediaTypes,
sizes: r.sizes,
ref: {
page: bidderRequest.refererInfo.page,
tloc: bidderRequest.refererInfo.topmostLocation,
referrer: bidderRequest.refererInfo.ref,
topurl: config.getConfig('pageUrl') ? false : bidderRequest.refererInfo.reachedTop,
},
sua,
user: utils.deepAccess(r, 'ortb2.user', null),
gdpr: utils.deepAccess(bidderRequest, 'gdprConsent.gdprApplies', false),
usp: (bidderRequest.uspConsent || '1---') !== '1---',
eids: utils.deepAccess(r, 'userIdAsEids', []),
schain: serializeSchain(utils.deepAccess(r, 'schain', null)),
pbVersion: '$prebid.version$',
}),
}
});
return requests;
},
interpretResponse: function (serverResponse, bidRequest) {
const seatbid = serverResponse.body.seatbid;
if (!utils.isArray(seatbid)) {
return [];
}
const bids = [];
seatbid.forEach((b) => {
const bid = b.bid || null;
if (!bid) {
return;
}
bids.push(bid);
});
return bids;
},
}
function serializeSchain(schain) {
if (!schain) {
return null;
}
let serializedSchain = `${schain.ver},${schain.complete}`;
schain.nodes.map(node => {
serializedSchain += `!${encodeURIComponentForRFC3986(node.asi || '')},`;
serializedSchain += `${encodeURIComponentForRFC3986(node.sid || '')},`;
serializedSchain += `${encodeURIComponentForRFC3986(node.hp || '')},`;
serializedSchain += `${encodeURIComponentForRFC3986(node.rid || '')},`;
serializedSchain += `${encodeURIComponentForRFC3986(node.name || '')},`;
serializedSchain += `${encodeURIComponentForRFC3986(node.domain || '')}`;
});
return serializedSchain;
}
function encodeURIComponentForRFC3986(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16)}`);
}
registerBidder(spec);