forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadgenerationBidAdapter.js
321 lines (294 loc) · 10.7 KB
/
adgenerationBidAdapter.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { escapeUnsafeChars } from '../libraries/htmlEscape/htmlEscape.js';
import { getCurrencyFromBidderRequest } from '../libraries/ortb2Utils/currency.js';
import { tryAppendQueryString } from '../libraries/urlUtils/urlUtils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE } from '../src/mediaTypes.js';
import { getBidIdParameter, deepSetValue, prefixLog } from '../src/utils.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
const adgLogger = prefixLog('Adgeneration: ');
/**
* @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 ADG_BIDDER_CODE = 'adgeneration';
const ADGENE_PREBID_VERSION = '1.6.4';
const DEBUG_URL = 'https://api-test.scaleout.jp/adgen/prebid';
const URL = 'https://d.socdm.com/adgen/prebid';
const converter = ortbConverter({
context: {
// `netRevenue` and `ttl` are required properties of bid responses - provide a default for them
netRevenue: true, // or false if your adapter should set bidResponse.netRevenue = false
ttl: 30// default bidResponse.ttl (when not specified in ORTB response.seatbid[].bid[].exp)
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
deepSetValue(imp, 'ext.params', bidRequest.params);
deepSetValue(imp, 'ext.mediaTypes', bidRequest.mediaTypes);
deepSetValue(imp, 'ext.novatiqSyncResponse', bidRequest?.userId?.novatiq?.snowflake?.syncResponse);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
return request;
},
bidResponse(buildBidResponse, bid, context) {
return buildBidResponse(bid, context)
}
});
export const spec = {
code: ADG_BIDDER_CODE,
aliases: ['adg'], // short code
supportedMediaTypes: [BANNER, NATIVE],
/**
* 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.params.id);
},
/**
* Make a server request from the list of BidRequests.
* @param validBidRequests
* @param bidderRequest
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
const ortbObj = converter.toORTB({bidRequests: validBidRequests, bidderRequest});
adgLogger.logInfo('ortbObj', ortbObj);
const {imp, ...rest} = ortbObj
const requests = imp.map((impObj) => {
const customParams = impObj?.ext?.params;
const id = getBidIdParameter('id', customParams);
const additionalParams = JSON.parse(JSON.stringify(rest));
// hyperIDが有効ではない場合、パラメータから削除する
if (!impObj?.ext?.novatiqSyncResponse || impObj?.ext?.novatiqSyncResponse !== 1) {
if (additionalParams?.user?.ext?.eids && Array.isArray(additionalParams?.user?.ext?.eids)) {
additionalParams.user.ext.eids = additionalParams?.user?.ext?.eids.filter((eid) => eid?.source !== 'novatiq.com');
}
}
let urlParams = ``;
urlParams = tryAppendQueryString(urlParams, 'id', id);
urlParams = tryAppendQueryString(urlParams, 'posall', 'SSPLOC');// not reaquired
urlParams = tryAppendQueryString(urlParams, 'sdktype', '0');
// remove the trailing "&"
if (urlParams.lastIndexOf('&') === urlParams.length - 1) {
urlParams = urlParams.substring(0, urlParams.length - 1);
}
const urlBase = customParams.debug ? (customParams.debug_url ? customParams.debug_url : DEBUG_URL) : URL
const url = `${urlBase}?${urlParams}`;
let data = {
currency: getCurrencyType(bidderRequest),
pbver: '$prebid.version$',
sdkname: 'prebidjs',
adapterver: ADGENE_PREBID_VERSION,
ortb: {
imp: [impObj],
...additionalParams
}
}
// native以外にvideo等の対応が入った場合は要修正
if (!impObj?.ext?.mediaTypes || !impObj?.ext?.mediaTypes.native) {
data.imark = 1;
}
return {
method: 'POST',
url: url,
data,
options: {
withCredentials: true,
crossOrigin: true
},
}
})
return requests;
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {BidRequest} bidRequests
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequests) {
adgLogger.logInfo('serverResponse', JSON.parse(JSON.stringify(serverResponse)));
const body = serverResponse.body;
if (!body.results || body.results.length < 1) {
return [];
}
if (!bidRequests?.data?.ortb?.imp || bidRequests?.data?.ortb?.imp.length < 1) {
return [];
}
const adResult = body?.results[0];
const targetImp = bidRequests?.data?.ortb?.imp[0];
const requestId = targetImp?.id;
const bidResponse = {
requestId: requestId,
cpm: adResult.cpm || 0,
width: adResult.w ? adResult.w : 1,
height: adResult.h ? adResult.h : 1,
creativeId: adResult.creativeid || '',
dealId: adResult.dealid || '',
currency: getCurrencyType(bidRequests.bidderRequest),
netRevenue: true,
ttl: adResult.ttl || 10,
};
if (adResult.adomain && Array.isArray(adResult.adomain) && adResult.adomain.length) {
bidResponse.meta = {
advertiserDomains: adResult.adomain
}
}
if (isNative(adResult)) {
bidResponse.native = createNativeAd(adResult.native, adResult.beaconurl);
bidResponse.mediaType = NATIVE;
} else {
// banner
bidResponse.ad = createAd(adResult, body?.location_params, targetImp.ext.params, requestId);
}
return [bidResponse];
},
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = [];
return syncs;
}
};
function createAd(adResult, locationPrams, bidParams, requestId) {
adgLogger.logInfo('params', bidParams);
let ad = adResult.ad;
if (adResult.vastxml && adResult.vastxml.length > 0) {
if (isUpperBillboard(locationPrams)) {
const marginTop = bidParams.marginTop ? bidParams.marginTop : '0';
ad = `<body>${createADGBrowserMTag()}${insertVASTMethodForADGBrowserM(adResult.vastxml, marginTop)}</body>`;
} else {
ad = `<body><div id="apvad-${requestId}"></div>${createAPVTag()}${insertVASTMethodForAPV(requestId, adResult.vastxml)}</body>`;
}
}
ad = appendChildToBody(ad, adResult.beacon);
if (removeWrapper(ad)) return removeWrapper(ad);
return ad;
}
function isUpperBillboard(locationParams) {
if (locationParams && locationParams.option && locationParams.option.ad_type) {
return locationParams.option.ad_type === 'upper_billboard';
}
return false;
}
function isNative(adResult) {
if (!adResult) return false;
return adResult.native && adResult.native.assets.length > 0;
}
function createNativeAd(nativeAd, beaconUrl) {
let native = {};
if (nativeAd && nativeAd.assets.length > 0) {
const assets = nativeAd.assets;
for (let i = 0, len = assets.length; i < len; i++) {
switch (assets[i].id) {
case 1:
native.title = assets[i].title.text;
break;
case 2:
native.image = {
url: assets[i].img.url,
height: assets[i].img.h,
width: assets[i].img.w,
};
break;
case 3:
native.icon = {
url: assets[i].img.url,
height: assets[i].img.h,
width: assets[i].img.w,
};
break;
case 4:
native.sponsoredBy = assets[i].data.value;
break;
case 5:
native.body = assets[i].data.value;
break;
case 6:
native.cta = assets[i].data.value;
break;
case 502:
native.privacyLink = encodeURIComponent(assets[i].data.value);
break;
}
}
native.clickUrl = nativeAd.link.url;
native.clickTrackers = nativeAd.link.clicktrackers || [];
native.impressionTrackers = nativeAd.imptrackers || [];
if (beaconUrl && beaconUrl != '') {
native.impressionTrackers.push(beaconUrl);
}
}
return native;
}
function appendChildToBody(ad, data) {
return ad.replace(/<\/\s?body>/, `${data}</body>`);
}
/**
* create APVTag
* @return {string}
*/
function createAPVTag() {
const APVURL = 'https://cdn.apvdr.com/js/VideoAd.min.js';
return `<script type="text/javascript" id="apv" src="${APVURL}"></script>`
}
/**
* create ADGBrowserMTag
* @return {string}
*/
function createADGBrowserMTag() {
const ADGBrowserMURL = 'https://i.socdm.com/sdk/js/adg-browser-m.js';
return `<script type="text/javascript" src="${ADGBrowserMURL}"></script>`;
}
/**
* create APVTag & insertVast
* @param targetId
* @param vastXml
* @return {string}
*/
function insertVASTMethodForAPV(targetId, vastXml) {
let apvVideoAdParam = {
s: targetId
};
return `<script type="text/javascript">(function(){ new APV.VideoAd(${escapeUnsafeChars(JSON.stringify(apvVideoAdParam))}).load('${vastXml.replace(/\r?\n/g, '')}'); })();</script>`
}
/**
* create ADGBrowserMTag & insertVast
* @param vastXml
* @param marginTop
* @return {string}
*/
function insertVASTMethodForADGBrowserM(vastXml, marginTop) {
return `<script type="text/javascript">window.ADGBrowserM.init({vastXml: '${vastXml.replace(/\r?\n/g, '')}', marginTop: '${marginTop}'});</script>`
}
/**
*
* @param ad
*/
function removeWrapper(ad) {
const bodyIndex = ad.indexOf('<body>');
const lastBodyIndex = ad.lastIndexOf('</body>');
if (bodyIndex === -1 || lastBodyIndex === -1) return false;
return ad.substr(bodyIndex, lastBodyIndex).replace('<body>', '').replace('</body>', '');
}
/**
* @return {?string} USD or JPY
*/
function getCurrencyType(bidderRequest) {
const adServerCurrency = getCurrencyFromBidderRequest(bidderRequest) || ''
return adServerCurrency.toUpperCase() === 'USD' ? 'USD' : 'JPY'
}
registerBidder(spec);