-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tpmnBidAdapter.js
245 lines (218 loc) · 7.76 KB
/
tpmnBidAdapter.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable no-tabs */
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
import { getStorageManager } from '../src/storageManager.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { Renderer } from '../src/Renderer.js';
import { config } from '../src/config.js';
import * as utils from '../src/utils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'tpmn';
const DEFAULT_BID_TTL = 500;
const DEFAULT_CURRENCY = 'USD';
const SUPPORTED_AD_TYPES = [BANNER, VIDEO];
// const BIDDER_ENDPOINT_URL = 'http://localhost:8081/ortb/pbjs_bidder';
const BIDDER_ENDPOINT_URL = 'https://gat.tpmn.io/ortb/pbjs_bidder';
const IFRAMESYNC = 'https://gat.tpmn.io/sync/iframe';
const COMMON_PARAMS = [
'battr'
];
export const VIDEO_RENDERER_URL = 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js';
export const ADAPTER_VERSION = '2.0';
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: SUPPORTED_AD_TYPES,
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
/**
* 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.burl) {
utils.triggerPixel(bid.burl);
}
}
}
function isBidRequestValid(bid) {
return (isValidInventoryId(bid) && (isValidBannerRequest(bid) || isValidVideoRequest(bid)));
}
function isValidInventoryId(bid) {
return 'params' in bid && 'inventoryId' in bid.params && utils.isNumber(bid.params.inventoryId);
}
function isValidBannerRequest(bid) {
const bannerSizes = utils.deepAccess(bid, `mediaTypes.${BANNER}.sizes`);
return utils.isArray(bannerSizes) && bannerSizes.length > 0 && bannerSizes.every(size => utils.isNumber(size[0]) && utils.isNumber(size[1]));
}
function isValidVideoRequest(bid) {
const videoSizes = utils.deepAccess(bid, `mediaTypes.${VIDEO}.playerSize`);
const videoMimes = utils.deepAccess(bid, `mediaTypes.${VIDEO}.mimes`);
const isValidVideoSize = utils.isArray(videoSizes) && videoSizes.length > 0 && videoSizes.every(size => utils.isNumber(size[0]) && utils.isNumber(size[1]));
const isValidVideoMimes = utils.isArray(videoMimes) && videoMimes.length > 0;
return isValidVideoSize && isValidVideoMimes;
}
function buildRequests(validBidRequests, bidderRequest) {
let requests = [];
try {
if (validBidRequests.length === 0 || !bidderRequest) return [];
let bannerBids = validBidRequests.filter(bid => utils.deepAccess(bid, 'mediaTypes.banner'));
let videoBids = validBidRequests.filter(bid => utils.deepAccess(bid, 'mediaTypes.video'));
bannerBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, BANNER));
});
videoBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, VIDEO));
});
} catch (err) {
utils.logWarn('buildRequests', err);
}
return requests;
}
function createRequest(bidRequests, bidderRequest, mediaType) {
const rtbData = CONVERTER.toORTB({ bidRequests, bidderRequest, context: { mediaType } })
const bid = bidRequests.find((b) => b.params.inventoryId)
if (bid.params.inventoryId) rtbData.ext = {};
if (bid.params.inventoryId) rtbData.ext.inventoryId = bid.params.inventoryId
const ortb2Data = bidderRequest?.ortb2 || {};
const bcat = ortb2Data?.bcat || bid.params.bcat || [];
const badv = ortb2Data?.badv || bid.params.badv || [];
const bapp = ortb2Data?.bapp || bid.params.bapp || [];
if (bcat.length > 0) {
rtbData.bcat = bcat;
}
if (badv.length > 0) {
rtbData.badv = badv;
}
if (badv.length > 0) {
rtbData.bapp = bapp;
}
return {
method: 'POST',
url: BIDDER_ENDPOINT_URL + '?v=' + ADAPTER_VERSION,
data: rtbData
}
}
function interpretResponse(response, request) {
return CONVERTER.fromORTB({ request: request.data, response: response.body }).bids;
}
registerBidder(spec);
const CONVERTER = ortbConverter({
context: {
netRevenue: true,
ttl: DEFAULT_BID_TTL,
currency: DEFAULT_CURRENCY
},
imp(buildImp, bidRequest, context) {
let imp = buildImp(bidRequest, context);
if (!imp.bidfloor && bidRequest.params.bidFloor) {
imp.bidfloor = bidRequest.params.bidFloor;
}
[VIDEO, BANNER].forEach(namespace => {
COMMON_PARAMS.forEach(param => {
if (bidRequest.params.hasOwnProperty(param)) {
utils.deepSetValue(imp, `${namespace}.${param}`, bidRequest.params[param])
}
})
})
return imp;
},
bidResponse(buildBidResponse, bid, context) {
const {bidRequest} = context;
const bidResponse = buildBidResponse(bid, context);
if (bidResponse.mediaType === BANNER) {
bidResponse.ad = bid.adm;
} else if (bidResponse.mediaType === VIDEO) {
if (bidRequest.mediaTypes.video.context === 'outstream') {
bidResponse.rendererUrl = VIDEO_RENDERER_URL;
bidResponse.renderer = createRenderer(bidRequest);
}
}
return bidResponse;
},
overrides: {
imp: {
video(orig, imp, bidRequest, context) {
let videoParams = bidRequest.mediaTypes[VIDEO];
if (videoParams) {
videoParams = Object.assign({}, videoParams, bidRequest.params.video);
bidRequest = {...bidRequest, mediaTypes: {[VIDEO]: videoParams}}
}
orig(imp, bidRequest, context);
},
},
}
});
function createRenderer(bid) {
const renderer = Renderer.install({
id: bid.bidId,
url: VIDEO_RENDERER_URL,
config: utils.deepAccess(bid, 'renderer.options'),
loaded: false,
adUnitCode: bid.adUnitCode
});
try {
renderer.setRender(outstreamRender);
} catch (err) {
utils.logWarn('Prebid Error calling setRender on renderer', err);
}
return renderer;
}
function outstreamRender(bid, doc) {
bid.renderer.push(() => {
const win = (doc) ? doc.defaultView : window;
win.ANOutstreamVideo.renderAd({
sizes: [bid.playerWidth, bid.playerHeight],
targetId: bid.adUnitCode,
rendererOptions: bid.renderer.getConfig(),
adResponse: { content: bid.vastXml }
}, handleOutstreamRendererEvents.bind(null, bid));
});
}
function handleOutstreamRendererEvents(bid, id, eventName) {
bid.renderer.handleVideoEvent({ id, eventName });
}
function getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncArr = [];
if (syncOptions.iframeEnabled) {
let policyParam = '';
if (gdprConsent && gdprConsent.consentString) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
policyParam += `&gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
} else {
policyParam += `&gdpr=0&gdpr_consent=${gdprConsent.consentString}`;
}
}
if (uspConsent && uspConsent.consentString) {
policyParam += `&ccpa_consent=${uspConsent.consentString}`;
}
const coppa = config.getConfig('coppa') ? 1 : 0;
policyParam += `&coppa=${coppa}`;
syncArr.push({
type: 'iframe',
url: IFRAMESYNC + '?' + policyParam
});
} else {
syncArr.push({
type: 'image',
url: 'https://x.bidswitch.net/sync?ssp=tpmn'
});
syncArr.push({
type: 'image',
url: 'https://gocm.c.appier.net/tpmn'
});
syncArr.push({
type: 'image',
url: 'https://info.mmnneo.com/getGuidRedirect.info?url=https%3A%2F%2Fad.tpmn.co.kr%2Fcookiesync.tpmn%3Ftpmn_nid%3Dbf91e8b3b9d3f1af3fc1d657f090b4fb%26tpmn_buid%3D'
});
syncArr.push({
type: 'image',
url: 'https://sync.aralego.com/idSync?redirect=https%3A%2F%2Fad.tpmn.co.kr%2FpixelCt.tpmn%3Ftpmn_nid%3Dde91e8b3b9d3f1af3fc1d657f090b815%26tpmn_buid%3DSspCookieUserId'
});
}
return syncArr;
}