@@ -2,13 +2,15 @@ import * as utils from '../src/utils.js';
2
2
import { registerBidder } from '../src/adapters/bidderFactory.js' ;
3
3
import { config } from '../src/config.js' ;
4
4
import find from 'core-js-pure/features/array/find.js' ;
5
+ import { BANNER , NATIVE } from '../src/mediaTypes.js' ;
5
6
6
7
const VERSION = '1.0' ;
7
8
const BIDDER_CODE = 'adyoulike' ;
8
9
const DEFAULT_DC = 'hb-api' ;
9
10
10
11
export const spec = {
11
12
code : BIDDER_CODE ,
13
+ supportedMediaTypes : [ BANNER , NATIVE ] ,
12
14
aliases : [ 'ayl' ] , // short code
13
15
/**
14
16
* Determines whether or not the given bid request is valid.
@@ -18,10 +20,11 @@ export const spec = {
18
20
*/
19
21
isBidRequestValid : function ( bid ) {
20
22
const sizes = getSize ( getSizeArray ( bid ) ) ;
21
- if ( ! bid . params || ! bid . params . placement || ! sizes . width || ! sizes . height ) {
22
- return false ;
23
- }
24
- return true ;
23
+ const sizeValid = sizes . width > 0 && sizes . height > 0 ;
24
+
25
+ // allows no size fornative only
26
+ return ( bid . params && bid . params . placement &&
27
+ ( sizeValid || ( bid . mediaTypes && bid . mediaTypes . native ) ) ) ;
25
28
} ,
26
29
/**
27
30
* Make a server request from the list of BidRequests.
@@ -41,6 +44,7 @@ export const spec = {
41
44
accumulator [ bid . bidId ] . Width = size . width ;
42
45
accumulator [ bid . bidId ] . Height = size . height ;
43
46
accumulator [ bid . bidId ] . AvailableSizes = sizesArray . join ( ',' ) ;
47
+ if ( bid . mediaTypes && bid . mediaTypes . native ) accumulator [ bid . bidId ] . Native = bid . mediaTypes . native ;
44
48
return accumulator ;
45
49
} , { } ) ,
46
50
PageRefreshed : getPageRefreshed ( )
@@ -171,9 +175,9 @@ function createEndpointQS(bidderRequest) {
171
175
}
172
176
173
177
function getSizeArray ( bid ) {
174
- let inputSize = bid . sizes ;
178
+ let inputSize = bid . sizes || [ ] ;
175
179
if ( bid . mediaTypes && bid . mediaTypes . banner ) {
176
- inputSize = bid . mediaTypes . banner . sizes ;
180
+ inputSize = bid . mediaTypes . banner . sizes || [ ] ;
177
181
}
178
182
179
183
return utils . parseSizesInput ( inputSize ) ;
@@ -203,34 +207,179 @@ function getSize(sizesArray) {
203
207
return parsed ;
204
208
}
205
209
210
+ function getInternalImgUrl ( uid ) {
211
+ if ( ! uid ) return '' ;
212
+ return 'https://blobs.omnitagjs.com/blobs/' + uid . substr ( 16 , 2 ) + '/' + uid . substr ( 16 ) + '/' + uid ;
213
+ }
214
+
215
+ function getImageUrl ( config , resource , width , height ) {
216
+ let url = '' ;
217
+
218
+ switch ( resource . Kind ) {
219
+ case 'INTERNAL' :
220
+ url = getInternalImgUrl ( resource . Data . Internal . BlobReference . Uid ) ;
221
+
222
+ break ;
223
+
224
+ case 'EXTERNAL' :
225
+ const dynPrefix = config . DynamicPrefix ;
226
+ let extUrl = resource . Data . External . Url ;
227
+ extUrl = extUrl . replace ( / \[ h e i g h t \] / i, '' + height ) ;
228
+ extUrl = extUrl . replace ( / \[ w i d t h \] / i, '' + width ) ;
229
+
230
+ if ( extUrl . indexOf ( dynPrefix ) >= 0 ) {
231
+ const urlmatch = ( / .* u r l = ( [ ^ & ] * ) / gm) . exec ( extUrl ) ;
232
+ url = urlmatch ? urlmatch [ 1 ] : '' ;
233
+ if ( ! url ) {
234
+ url = getInternalImgUrl ( ( / .* k e y = ( [ ^ & ] * ) / gm) . exec ( extUrl ) [ 1 ] ) ;
235
+ }
236
+ } else {
237
+ url = extUrl ;
238
+ }
239
+
240
+ break ;
241
+ }
242
+
243
+ return url ;
244
+ }
245
+
246
+ function getTrackers ( eventsArray , jsTrackers ) {
247
+ const result = [ ] ;
248
+
249
+ if ( ! eventsArray ) return result ;
250
+
251
+ eventsArray . map ( ( item , index ) => {
252
+ if ( ( jsTrackers && item . Kind === 'JAVASCRIPT_URL' ) ||
253
+ ( ! jsTrackers && item . Kind === 'PIXEL_URL' ) ) {
254
+ result . push ( item . Url ) ;
255
+ }
256
+ } ) ;
257
+ return result ;
258
+ }
259
+
260
+ function getNativeAssets ( response , nativeConfig ) {
261
+ const native = { } ;
262
+
263
+ var adJson = { } ;
264
+ var textsJson = { } ;
265
+ if ( typeof response . Ad === 'string' ) {
266
+ adJson = JSON . parse ( response . Ad . match ( / \/ \* P R E B I D \* \/ ( .* ) \/ \* P R E B I D \* \/ / ) [ 1 ] ) ;
267
+ textsJson = adJson . Content . Preview . Text ;
268
+
269
+ var impressionUrl = adJson . TrackingPrefix +
270
+ '/pixel?event_kind=IMPRESSION&attempt=' + adJson . Attempt ;
271
+
272
+ if ( adJson . Campaign ) {
273
+ impressionUrl += '&campaign=' + adJson . Campaign ;
274
+ }
275
+
276
+ native . clickUrl = adJson . TrackingPrefix + '/ar?event_kind=CLICK&attempt=' + adJson . Attempt +
277
+ '&campaign=' + adJson . Campaign + '&url=' + encodeURIComponent ( adJson . Content . Landing . Url ) ;
278
+
279
+ native . clickTrackers = getTrackers ( adJson . OnEvents [ 'CLICK' ] ) ;
280
+ native . impressionTrackers = getTrackers ( adJson . OnEvents [ 'IMPRESSION' ] ) ;
281
+ native . impressionTrackers . push ( impressionUrl ) ;
282
+ native . javascriptTrackers = getTrackers ( adJson . OnEvents [ 'IMPRESSION' ] , true ) ;
283
+ }
284
+
285
+ Object . keys ( nativeConfig ) . map ( function ( key , index ) {
286
+ if ( typeof response . Native === 'object' ) {
287
+ native [ key ] = response . Native [ key ] ;
288
+ } else {
289
+ switch ( key ) {
290
+ case 'title' :
291
+ native [ key ] = textsJson . TITLE ;
292
+ break ;
293
+ case 'body' :
294
+ native [ key ] = textsJson . DESCRIPTION ;
295
+ break ;
296
+ case 'cta' :
297
+ native [ key ] = textsJson . CALLTOACTION ;
298
+ break ;
299
+ case 'sponsoredBy' :
300
+ native [ key ] = adJson . Content . Preview . Sponsor . Name ;
301
+ break ;
302
+ case 'image' :
303
+ // main image requested size
304
+ const imgSize = nativeConfig . image . sizes || [ ] ;
305
+ if ( ! imgSize . length ) {
306
+ imgSize [ 0 ] = response . Width || 300 ;
307
+ imgSize [ 1 ] = response . Height || 250 ;
308
+ }
309
+
310
+ native [ key ] = {
311
+ url : getImageUrl ( adJson , adJson . Content . Preview . Thumbnail . Image , imgSize [ 0 ] , imgSize [ 1 ] ) ,
312
+ width : imgSize [ 0 ] ,
313
+ height : imgSize [ 1 ]
314
+ } ;
315
+ break ;
316
+ case 'icon' :
317
+ if ( adJson . HasSponsorImage ) {
318
+ // icon requested size
319
+ const iconSize = nativeConfig . icon . sizes || [ ] ;
320
+ if ( ! iconSize . length ) {
321
+ iconSize [ 0 ] = 50 ;
322
+ iconSize [ 1 ] = 50 ;
323
+ }
324
+
325
+ native [ key ] = {
326
+ url : getImageUrl ( adJson , adJson . Content . Preview . Sponsor . Logo . Resource , iconSize [ 0 ] , iconSize [ 1 ] ) ,
327
+ width : iconSize [ 0 ] ,
328
+ height : iconSize [ 1 ]
329
+ } ;
330
+ }
331
+ break ;
332
+ case 'privacyIcon' :
333
+ native [ key ] = getImageUrl ( adJson , adJson . Content . Preview . Credit . Logo . Resource , 25 , 25 ) ;
334
+ break ;
335
+ case 'privacyLink' :
336
+ native [ key ] = adJson . Content . Preview . Credit . Url ;
337
+ break ;
338
+ }
339
+ }
340
+ } ) ;
341
+
342
+ return native ;
343
+ }
344
+
206
345
/* Create bid from response */
207
346
function createBid ( response , bidRequests ) {
208
- if ( ! response || ! response . Ad ) {
347
+ if ( ! response || ( ! response . Ad && ! response . Native ) ) {
209
348
return
210
349
}
211
350
351
+ const request = bidRequests && bidRequests [ response . BidID ] ;
352
+
212
353
// In case we don't retreive the size from the adserver, use the given one.
213
- if ( bidRequests && bidRequests [ response . BidID ] ) {
354
+ if ( request ) {
214
355
if ( ! response . Width || response . Width === '0' ) {
215
- response . Width = bidRequests [ response . BidID ] . Width ;
356
+ response . Width = request . Width ;
216
357
}
217
358
218
359
if ( ! response . Height || response . Height === '0' ) {
219
- response . Height = bidRequests [ response . BidID ] . Height ;
360
+ response . Height = request . Height ;
220
361
}
221
362
}
222
363
223
- return {
364
+ const bid = {
224
365
requestId : response . BidID ,
225
- width : response . Width ,
226
- height : response . Height ,
227
- ad : response . Ad ,
228
366
ttl : 3600 ,
229
367
creativeId : response . CreativeID ,
230
368
cpm : response . Price ,
231
369
netRevenue : true ,
232
370
currency : 'USD'
233
371
} ;
372
+
373
+ if ( request && request . Native ) {
374
+ bid . native = getNativeAssets ( response , request . Native ) ;
375
+ bid . mediaType = 'native' ;
376
+ } else {
377
+ bid . width = response . Width ;
378
+ bid . height = response . Height ;
379
+ bid . ad = response . Ad ;
380
+ }
381
+
382
+ return bid ;
234
383
}
235
384
236
385
registerBidder ( spec ) ;
0 commit comments