|
| 1 | +import * as utils from '../src/utils'; |
| 2 | +import { parse as parseUrl } from '../src/url'; |
| 3 | +import { config } from '../src/config'; |
| 4 | +import { registerBidder } from '../src/adapters/bidderFactory'; |
| 5 | +import { VIDEO, BANNER } from '../src/mediaTypes'; |
| 6 | +import find from 'core-js/library/fn/array/find'; |
| 7 | +import includes from 'core-js/library/fn/array/includes'; |
| 8 | + |
| 9 | +const ADAPTER_VERSION = '1.0'; |
| 10 | +const BIDDER_CODE = 'avng'; |
| 11 | + |
| 12 | +export const VIDEO_ENDPOINT = '//nep.advangelists.com/xp/get?pubid=';// 0cf8d6d643e13d86a5b6374148a4afac'; |
| 13 | +export const BANNER_ENDPOINT = '//nep.advangelists.com/xp/get?pubid=';// 0cf8d6d643e13d86a5b6374148a4afac'; |
| 14 | +export const OUTSTREAM_SRC = '//player-cdn.beachfrontmedia.com/playerapi/loader/outstream.js'; |
| 15 | +export const VIDEO_TARGETING = ['mimes', 'playbackmethod', 'maxduration', 'skip']; |
| 16 | +export const DEFAULT_MIMES = ['video/mp4', 'application/javascript']; |
| 17 | + |
| 18 | +let pubid = ''; |
| 19 | + |
| 20 | +export const spec = { |
| 21 | + code: BIDDER_CODE, |
| 22 | + supportedMediaTypes: [BANNER, VIDEO], |
| 23 | + |
| 24 | + isBidRequestValid(bidRequest) { |
| 25 | + if (typeof bidRequest != 'undefined') { |
| 26 | + if (bidRequest.bidder !== BIDDER_CODE && typeof bidRequest.params === 'undefined') { return false; } |
| 27 | + if (bidRequest === '' || bidRequest.params.placement === '' || bidRequest.params.pubid === '') { return false; } |
| 28 | + return true; |
| 29 | + } else { return false; } |
| 30 | + }, |
| 31 | + |
| 32 | + buildRequests(bids, bidderRequest) { |
| 33 | + let requests = []; |
| 34 | + let videoBids = bids.filter(bid => isVideoBidValid(bid)); |
| 35 | + let bannerBids = bids.filter(bid => isBannerBidValid(bid)); |
| 36 | + videoBids.forEach(bid => { |
| 37 | + pubid = getVideoBidParam(bid, 'pubid'); |
| 38 | + requests.push({ |
| 39 | + method: 'POST', |
| 40 | + url: VIDEO_ENDPOINT + pubid, |
| 41 | + data: createVideoRequestData(bid, bidderRequest), |
| 42 | + bidRequest: bid |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + bannerBids.forEach(bid => { |
| 47 | + pubid = getBannerBidParam(bid, 'pubid'); |
| 48 | + requests.push({ |
| 49 | + method: 'POST', |
| 50 | + url: BANNER_ENDPOINT + pubid, |
| 51 | + data: createBannerRequestData(bid, bidderRequest), |
| 52 | + bidRequest: bid |
| 53 | + }); |
| 54 | + }); |
| 55 | + return requests; |
| 56 | + }, |
| 57 | + |
| 58 | + interpretResponse(serverResponse, {bidRequest}) { |
| 59 | + let response = serverResponse.body; |
| 60 | + if (response !== null && utils.isEmpty(response) == false) { |
| 61 | + if (isVideoBid(bidRequest)) { |
| 62 | + let bidResponse = { |
| 63 | + requestId: response.id, |
| 64 | + bidderCode: BIDDER_CODE, |
| 65 | + cpm: response.seatbid[0].bid[0].price, |
| 66 | + width: response.seatbid[0].bid[0].w, |
| 67 | + height: response.seatbid[0].bid[0].h, |
| 68 | + ttl: response.seatbid[0].bid[0].ttl || 60, |
| 69 | + creativeId: response.seatbid[0].bid[0].crid, |
| 70 | + currency: response.cur, |
| 71 | + mediaType: VIDEO, |
| 72 | + netRevenue: true |
| 73 | + } |
| 74 | + |
| 75 | + if (response.seatbid[0].bid[0].adm) { |
| 76 | + bidResponse.vastXml = response.seatbid[0].bid[0].adm; |
| 77 | + bidResponse.adResponse = { |
| 78 | + content: response.seatbid[0].bid[0].adm |
| 79 | + }; |
| 80 | + } else { |
| 81 | + bidResponse.vastUrl = response.seatbid[0].bid[0].nurl; |
| 82 | + } |
| 83 | + |
| 84 | + return bidResponse; |
| 85 | + } else { |
| 86 | + return { |
| 87 | + requestId: response.id, |
| 88 | + bidderCode: BIDDER_CODE, |
| 89 | + cpm: response.seatbid[0].bid[0].price, |
| 90 | + width: response.seatbid[0].bid[0].w, |
| 91 | + height: response.seatbid[0].bid[0].h, |
| 92 | + ad: response.seatbid[0].bid[0].adm, |
| 93 | + ttl: response.seatbid[0].bid[0].ttl || 60, |
| 94 | + creativeId: response.seatbid[0].bid[0].crid, |
| 95 | + currency: response.cur, |
| 96 | + mediaType: BANNER, |
| 97 | + netRevenue: true |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +function isBannerBid(bid) { |
| 105 | + return utils.deepAccess(bid, 'mediaTypes.banner') || !isVideoBid(bid); |
| 106 | +} |
| 107 | + |
| 108 | +function isVideoBid(bid) { |
| 109 | + return utils.deepAccess(bid, 'mediaTypes.video'); |
| 110 | +} |
| 111 | + |
| 112 | +function isVideoBidValid(bid) { |
| 113 | + return isVideoBid(bid) && getVideoBidParam(bid, 'pubid') && getVideoBidParam(bid, 'placement'); |
| 114 | +} |
| 115 | + |
| 116 | +function isBannerBidValid(bid) { |
| 117 | + return isBannerBid(bid) && getBannerBidParam(bid, 'pubid') && getBannerBidParam(bid, 'placement'); |
| 118 | +} |
| 119 | + |
| 120 | +function getVideoBidParam(bid, key) { |
| 121 | + return utils.deepAccess(bid, 'params.video.' + key) || utils.deepAccess(bid, 'params.' + key); |
| 122 | +} |
| 123 | + |
| 124 | +function getBannerBidParam(bid, key) { |
| 125 | + return utils.deepAccess(bid, 'params.banner.' + key) || utils.deepAccess(bid, 'params.' + key); |
| 126 | +} |
| 127 | + |
| 128 | +function isMobile() { |
| 129 | + return (/(ios|ipod|ipad|iphone|android)/i).test(navigator.userAgent); |
| 130 | +} |
| 131 | + |
| 132 | +function isConnectedTV() { |
| 133 | + return (/(smart[-]?tv|hbbtv|appletv|googletv|hdmi|netcast\.tv|viera|nettv|roku|\bdtv\b|sonydtv|inettvbrowser|\btv\b)/i).test(navigator.userAgent); |
| 134 | +} |
| 135 | + |
| 136 | +function getDoNotTrack() { |
| 137 | + return navigator.doNotTrack === '1' || window.doNotTrack === '1' || navigator.msDoNoTrack === '1' || navigator.doNotTrack === 'yes'; |
| 138 | +} |
| 139 | + |
| 140 | +function findAndFillParam(o, key, value) { |
| 141 | + try { |
| 142 | + if (typeof value === 'function') { |
| 143 | + o[key] = value(); |
| 144 | + } else { |
| 145 | + o[key] = value; |
| 146 | + } |
| 147 | + } catch (ex) {} |
| 148 | +} |
| 149 | + |
| 150 | +function getOsVersion() { |
| 151 | + let clientStrings = [ |
| 152 | + { s: 'Android', r: /Android/ }, |
| 153 | + { s: 'iOS', r: /(iPhone|iPad|iPod)/ }, |
| 154 | + { s: 'Mac OS X', r: /Mac OS X/ }, |
| 155 | + { s: 'Mac OS', r: /(MacPPC|MacIntel|Mac_PowerPC|Macintosh)/ }, |
| 156 | + { s: 'Linux', r: /(Linux|X11)/ }, |
| 157 | + { s: 'Windows 10', r: /(Windows 10.0|Windows NT 10.0)/ }, |
| 158 | + { s: 'Windows 8.1', r: /(Windows 8.1|Windows NT 6.3)/ }, |
| 159 | + { s: 'Windows 8', r: /(Windows 8|Windows NT 6.2)/ }, |
| 160 | + { s: 'Windows 7', r: /(Windows 7|Windows NT 6.1)/ }, |
| 161 | + { s: 'Windows Vista', r: /Windows NT 6.0/ }, |
| 162 | + { s: 'Windows Server 2003', r: /Windows NT 5.2/ }, |
| 163 | + { s: 'Windows XP', r: /(Windows NT 5.1|Windows XP)/ }, |
| 164 | + { s: 'UNIX', r: /UNIX/ }, |
| 165 | + { s: 'Search Bot', r: /(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/ } |
| 166 | + ]; |
| 167 | + let cs = find(clientStrings, cs => cs.r.test(navigator.userAgent)); |
| 168 | + return cs ? cs.s : 'unknown'; |
| 169 | +} |
| 170 | + |
| 171 | +function getFirstSize(sizes) { |
| 172 | + return (sizes && sizes.length) ? sizes[0] : { w: undefined, h: undefined }; |
| 173 | +} |
| 174 | + |
| 175 | +function parseSizes(sizes) { |
| 176 | + return utils.parseSizesInput(sizes).map(size => { |
| 177 | + let [ width, height ] = size.split('x'); |
| 178 | + return { |
| 179 | + w: parseInt(width, 10) || undefined, |
| 180 | + h: parseInt(height, 10) || undefined |
| 181 | + }; |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +function getVideoSizes(bid) { |
| 186 | + return parseSizes(utils.deepAccess(bid, 'mediaTypes.video.playerSize') || bid.sizes); |
| 187 | +} |
| 188 | + |
| 189 | +function getBannerSizes(bid) { |
| 190 | + return parseSizes(utils.deepAccess(bid, 'mediaTypes.banner.sizes') || bid.sizes); |
| 191 | +} |
| 192 | + |
| 193 | +function getTopWindowReferrer() { |
| 194 | + try { |
| 195 | + return window.top.document.referrer; |
| 196 | + } catch (e) { |
| 197 | + return ''; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +function getVideoTargetingParams(bid) { |
| 202 | + return Object.keys(Object(bid.params.video)) |
| 203 | + .filter(param => includes(VIDEO_TARGETING, param)) |
| 204 | + .reduce((obj, param) => { |
| 205 | + obj[ param ] = bid.params.video[ param ]; |
| 206 | + return obj; |
| 207 | + }, {}); |
| 208 | +} |
| 209 | + |
| 210 | +function createVideoRequestData(bid, bidderRequest) { |
| 211 | + let topLocation = getTopWindowLocation(bidderRequest); |
| 212 | + let topReferrer = getTopWindowReferrer(); |
| 213 | + |
| 214 | + let sizes = getVideoSizes(bid); |
| 215 | + let firstSize = getFirstSize(sizes); |
| 216 | + |
| 217 | + let video = getVideoTargetingParams(bid); |
| 218 | + const o = { |
| 219 | + 'device': { |
| 220 | + 'langauge': (global.navigator.language).split('-')[0], |
| 221 | + 'dnt': (global.navigator.doNotTrack === 1 ? 1 : 0), |
| 222 | + 'devicetype': isMobile() ? 4 : isConnectedTV() ? 3 : 2, |
| 223 | + 'js': 1, |
| 224 | + 'os': getOsVersion() |
| 225 | + }, |
| 226 | + 'at': 2, |
| 227 | + 'site': {}, |
| 228 | + 'tmax': 3000, |
| 229 | + 'cur': ['USD'], |
| 230 | + 'id': bid.bidId, |
| 231 | + 'imp': [], |
| 232 | + 'regs': { |
| 233 | + 'ext': { |
| 234 | + } |
| 235 | + }, |
| 236 | + 'user': { |
| 237 | + 'ext': { |
| 238 | + } |
| 239 | + } |
| 240 | + }; |
| 241 | + |
| 242 | + o.site['page'] = topLocation.href; |
| 243 | + o.site['domain'] = topLocation.hostname; |
| 244 | + o.site['search'] = topLocation.search; |
| 245 | + o.site['domain'] = topLocation.hostname; |
| 246 | + o.site['ref'] = topReferrer; |
| 247 | + o.site['mobile'] = isMobile() ? 1 : 0; |
| 248 | + const secure = topLocation.protocol.indexOf('https') === 0 ? 1 : 0; |
| 249 | + |
| 250 | + o.device['dnt'] = getDoNotTrack() ? 1 : 0; |
| 251 | + |
| 252 | + findAndFillParam(o.site, 'name', function() { |
| 253 | + return global.top.document.title; |
| 254 | + }); |
| 255 | + |
| 256 | + findAndFillParam(o.device, 'h', function() { |
| 257 | + return global.screen.height; |
| 258 | + }); |
| 259 | + findAndFillParam(o.device, 'w', function() { |
| 260 | + return global.screen.width; |
| 261 | + }); |
| 262 | + |
| 263 | + let placement = getVideoBidParam(bid, 'placement'); |
| 264 | + |
| 265 | + for (let j = 0; j < sizes.length; j++) { |
| 266 | + o.imp.push({ |
| 267 | + 'id': '' + j, |
| 268 | + 'displaymanager': '' + BIDDER_CODE, |
| 269 | + 'displaymanagerver': '' + ADAPTER_VERSION, |
| 270 | + 'tagId': placement, |
| 271 | + 'bidfloor': 2.0, |
| 272 | + 'bidfloorcur': 'USD', |
| 273 | + 'secure': secure, |
| 274 | + 'video': Object.assign({ |
| 275 | + 'id': utils.generateUUID(), |
| 276 | + 'pos': 0, |
| 277 | + 'w': firstSize.w, |
| 278 | + 'h': firstSize.h, |
| 279 | + 'mimes': DEFAULT_MIMES |
| 280 | + }, video) |
| 281 | + |
| 282 | + }); |
| 283 | + } |
| 284 | + |
| 285 | + if (bidderRequest && bidderRequest.gdprConsent) { |
| 286 | + let { gdprApplies, consentString } = bidderRequest.gdprConsent; |
| 287 | + o.regs.ext = {'gdpr': gdprApplies ? 1 : 0}; |
| 288 | + o.user.ext = {'consent': consentString}; |
| 289 | + } |
| 290 | + |
| 291 | + return o; |
| 292 | +} |
| 293 | + |
| 294 | +function getTopWindowLocation(bidderRequest) { |
| 295 | + let url = bidderRequest && bidderRequest.refererInfo && bidderRequest.refererInfo.referer; |
| 296 | + return parseUrl(config.getConfig('pageUrl') || url, { decodeSearchAsString: true }); |
| 297 | +} |
| 298 | + |
| 299 | +function createBannerRequestData(bid, bidderRequest) { |
| 300 | + let topLocation = getTopWindowLocation(bidderRequest); |
| 301 | + let topReferrer = getTopWindowReferrer(); |
| 302 | + |
| 303 | + let sizes = getBannerSizes(bid); |
| 304 | + |
| 305 | + const o = { |
| 306 | + 'device': { |
| 307 | + 'langauge': (global.navigator.language).split('-')[0], |
| 308 | + 'dnt': (global.navigator.doNotTrack === 1 ? 1 : 0), |
| 309 | + 'devicetype': isMobile() ? 4 : isConnectedTV() ? 3 : 2, |
| 310 | + 'js': 1 |
| 311 | + }, |
| 312 | + 'at': 2, |
| 313 | + 'site': {}, |
| 314 | + 'tmax': 3000, |
| 315 | + 'cur': ['USD'], |
| 316 | + 'id': bid.bidId, |
| 317 | + 'imp': [], |
| 318 | + 'regs': { |
| 319 | + 'ext': { |
| 320 | + } |
| 321 | + }, |
| 322 | + 'user': { |
| 323 | + 'ext': { |
| 324 | + } |
| 325 | + } |
| 326 | + }; |
| 327 | + |
| 328 | + o.site['page'] = topLocation.href; |
| 329 | + o.site['domain'] = topLocation.hostname; |
| 330 | + o.site['search'] = topLocation.search; |
| 331 | + o.site['domain'] = topLocation.hostname; |
| 332 | + o.site['ref'] = topReferrer; |
| 333 | + o.site['mobile'] = isMobile() ? 1 : 0; |
| 334 | + const secure = topLocation.protocol.indexOf('https') === 0 ? 1 : 0; |
| 335 | + |
| 336 | + o.device['dnt'] = getDoNotTrack() ? 1 : 0; |
| 337 | + |
| 338 | + findAndFillParam(o.site, 'name', function() { |
| 339 | + return global.top.document.title; |
| 340 | + }); |
| 341 | + |
| 342 | + findAndFillParam(o.device, 'h', function() { |
| 343 | + return global.screen.height; |
| 344 | + }); |
| 345 | + findAndFillParam(o.device, 'w', function() { |
| 346 | + return global.screen.width; |
| 347 | + }); |
| 348 | + |
| 349 | + let placement = getBannerBidParam(bid, 'placement'); |
| 350 | + for (let j = 0; j < sizes.length; j++) { |
| 351 | + let size = sizes[j]; |
| 352 | + |
| 353 | + o.imp.push({ |
| 354 | + 'id': '' + j, |
| 355 | + 'displaymanager': '' + BIDDER_CODE, |
| 356 | + 'displaymanagerver': '' + ADAPTER_VERSION, |
| 357 | + 'tagId': placement, |
| 358 | + 'bidfloor': 2.0, |
| 359 | + 'bidfloorcur': 'USD', |
| 360 | + 'secure': secure, |
| 361 | + 'banner': { |
| 362 | + 'id': utils.generateUUID(), |
| 363 | + 'pos': 0, |
| 364 | + 'w': size['w'], |
| 365 | + 'h': size['h'] |
| 366 | + } |
| 367 | + }); |
| 368 | + } |
| 369 | + |
| 370 | + if (bidderRequest && bidderRequest.gdprConsent) { |
| 371 | + let { gdprApplies, consentString } = bidderRequest.gdprConsent; |
| 372 | + o.regs.ext = {'gdpr': gdprApplies ? 1 : 0}; |
| 373 | + o.user.ext = {'consent': consentString}; |
| 374 | + } |
| 375 | + |
| 376 | + return o; |
| 377 | +} |
| 378 | + |
| 379 | +registerBidder(spec); |
0 commit comments