forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtbsapeBidAdapter.js
153 lines (135 loc) · 4.78 KB
/
rtbsapeBidAdapter.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { deepAccess, triggerPixel } from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {OUTSTREAM} from '../src/video.js';
import {Renderer} from '../src/Renderer.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
*/
const BIDDER_CODE = 'rtbsape';
const ENDPOINT = 'https://ssp-rtb.sape.ru/prebid';
const RENDERER_SRC = 'https://cdn-rtb.sape.ru/js/player.js';
const MATCH_SRC = 'https://www.acint.net/mc/?dp=141';
export const spec = {
code: BIDDER_CODE,
aliases: ['sape'],
supportedMediaTypes: [BANNER, VIDEO],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
return !!(bid && bid.mediaTypes && (bid.mediaTypes.banner || bid.mediaTypes.video) && bid.params && bid.params.placeId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests an array of bids
* @param bidderRequest
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
let tz = (new Date()).getTimezoneOffset()
let padInt = (v) => (v < 10 ? '0' + v : '' + v);
return {
url: ENDPOINT,
method: 'POST',
data: {
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: bidderRequest.auctionId,
requestId: bidderRequest.bidderRequestId,
bids: validBidRequests,
timezone: (tz > 0 ? '-' : '+') + padInt(Math.floor(Math.abs(tz) / 60)) + ':' + padInt(Math.abs(tz) % 60),
// TODO: please do not send internal data structures over the network
refererInfo: bidderRequest.refererInfo.legacy
},
}
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {{data: {bids: [{mediaTypes: {banner: boolean}}]}}} bidRequest Info describing the request to the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
if (!(serverResponse.body && Array.isArray(serverResponse.body.bids))) {
return [];
}
let bids = {};
bidRequest.data.bids.forEach(bid => bids[bid.bidId] = bid);
return serverResponse.body.bids
.filter(bid => typeof (bid.meta || {}).advertiserDomains !== 'undefined')
.map(bid => {
let requestBid = bids[bid.requestId];
let context = deepAccess(requestBid, 'mediaTypes.video.context');
if (context === OUTSTREAM && (bid.vastUrl || bid.vastXml)) {
let renderer = Renderer.install({
id: bid.requestId,
url: RENDERER_SRC,
loaded: false
});
let muted = deepAccess(requestBid, 'params.video.playerMuted');
if (typeof muted === 'undefined') {
muted = true;
}
bid.playerMuted = muted;
bid.renderer = renderer
renderer.setRender(setOutstreamRenderer);
}
return bid;
});
},
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function (syncOptions) {
const sync = [];
if (syncOptions.iframeEnabled) {
sync.push({
type: 'iframe',
url: MATCH_SRC
});
}
return sync;
},
/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
* @param {Bid} bid The bid that won the auction
*/
onBidWon: function(bid) {
if (bid.nurl) {
triggerPixel(bid.nurl);
}
}
}
/**
* Initialize RtbSape outstream player
*
* @param bid
*/
function setOutstreamRenderer(bid) {
let props = {};
if (bid.vastUrl) {
props.url = bid.vastUrl;
}
if (bid.vastXml) {
props.xml = bid.vastXml;
}
bid.renderer.push(() => {
let player = window.sapeRtbPlayerHandler(bid.adUnitCode, bid.width, bid.height, bid.playerMuted, {singleton: true});
props.onComplete = () => player.destroy();
props.onError = () => player.destroy();
player.addSlot(props);
});
}
registerBidder(spec);