forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowheroes-bsBidAdapter.js
170 lines (149 loc) · 4.19 KB
/
showheroes-bsBidAdapter.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
deepAccess,
deepSetValue,
triggerPixel,
isFn,
logInfo
} from '../src/utils.js';
import { Renderer } from '../src/Renderer.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { VIDEO } from '../src/mediaTypes.js';
const ENDPOINT = 'https://ads.viralize.tv/openrtb2/auction/';
const BIDDER_CODE = 'showheroes-bs';
const TTL = 300;
const converter = ortbConverter({
context: {
netRevenue: true,
ttl: TTL,
currency: 'EUR',
mediaType: VIDEO,
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
const videoContext = deepAccess(bidRequest, 'mediaTypes.video.context');
deepSetValue(imp, 'video.ext.context', videoContext);
imp.ext = imp.ext || {};
imp.ext.params = bidRequest.params;
imp.ext.adUnitCode = bidRequest.adUnitCode;
if (!imp.displaymanager) {
imp.displaymanager = 'Prebid.js';
imp.displaymanagerver = '$prebid.version$'; // prebid version
}
if (!isFn(bidRequest.getFloor)) {
return imp
}
let floor = bidRequest.getFloor({
currency: 'EUR',
mediaType: '*',
size: '*',
});
if (!isNaN(floor?.floor) && floor?.currency === 'EUR') {
imp.bidfloor = floor.floor;
imp.bidfloorcur = 'EUR';
}
return imp;
},
bidResponse(buildBidResponse, bid, context) {
const bidResponse = buildBidResponse(bid, context);
if (context.imp?.video?.ext?.context === 'outstream') {
const renderConfig = {
rendererUrl: bid.ext?.rendererConfig?.rendererUrl,
renderFunc: bid.ext?.rendererConfig?.renderFunc,
renderOptions: bid.ext?.rendererConfig?.renderOptions,
};
if (renderConfig.renderFunc && renderConfig.rendererUrl) {
bidResponse.renderer = createRenderer(bidResponse, renderConfig);
}
}
bidResponse.callbacks = bid.ext?.callbacks;
bidResponse.extra = bid.ext?.extra;
return bidResponse;
},
})
const GVLID = 111;
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
aliases: ['showheroesBs'],
supportedMediaTypes: [VIDEO],
isBidRequestValid: (bid) => {
return !!bid.params.unitId;
},
buildRequests: (bidRequests, bidderRequest) => {
const QA = bidRequests[0].params.qa;
const ortbData = converter.toORTB({ bidRequests, bidderRequest })
return {
url: QA?.endpoint || ENDPOINT,
method: 'POST',
data: ortbData,
};
},
interpretResponse: (response, request) => {
if (!response.body) {
return [];
}
const bids = converter.fromORTB({response: response.body, request: request.data}).bids;
return bids;
},
getUserSyncs: (syncOptions, serverResponses) => {
const syncs = [];
if (!serverResponses.length || !serverResponses[0].body?.ext?.userSync) {
return syncs;
}
const userSync = serverResponses[0].body.ext.userSync;
if (syncOptions.iframeEnabled && userSync?.iframes?.length) {
userSync.iframes.forEach(url => {
syncs.push({
type: 'iframe',
url
});
});
}
if (syncOptions.pixelEnabled) {
(userSync.pixels || []).forEach(url => {
syncs.push({
type: 'image',
url
});
});
}
return syncs;
},
onBidWon(bid) {
if (bid.callbacks) {
triggerPixel(bid.callbacks.won);
}
logInfo(
`Showheroes adapter won the auction. Bid id: ${bid.bidId || bid.requestId}`
);
},
};
function outstreamRender(response, renderConfig) {
response.renderer.push(() => {
const func = deepAccess(window, renderConfig.renderFunc);
if (!isFn(func)) {
return;
}
const renderPayload = { ...renderConfig.renderOptions };
if (response.vastXml) {
renderPayload.adResponse = {
content: response.vastXml,
};
}
func(renderPayload);
});
}
function createRenderer(bid, renderConfig) {
const renderer = Renderer.install({
id: bid.id,
url: renderConfig.rendererUrl,
loaded: false,
adUnitCode: bid.adUnitCode,
});
renderer.setRender((render) => {
return outstreamRender(render, renderConfig);
});
return renderer;
}
registerBidder(spec);