diff --git a/modules/sovrnBidAdapter.js b/modules/sovrnBidAdapter.js index 8b08b3ad6be..935bbad79a7 100644 --- a/modules/sovrnBidAdapter.js +++ b/modules/sovrnBidAdapter.js @@ -1,8 +1,6 @@ import * as utils from '../src/utils' import { registerBidder } from '../src/adapters/bidderFactory' import { BANNER } from '../src/mediaTypes' -const errorUrl = 'https://pcb.aws.lijit.com/c' -let errorpxls = [] export const spec = { code: 'sovrn', @@ -24,14 +22,30 @@ export const spec = { */ buildRequests: function(bidReqs, bidderRequest) { try { - const loc = utils.getTopWindowLocation(); let sovrnImps = []; let iv; + let schain; + let digitrust; + utils._each(bidReqs, function (bid) { + if (!digitrust) { + const bidRequestDigitrust = utils.deepAccess(bid, 'userId.digitrustid.data'); + if (bidRequestDigitrust && (!bidRequestDigitrust.privacy || !bidRequestDigitrust.privacy.optout)) { + digitrust = { + id: bidRequestDigitrust.id, + keyv: bidRequestDigitrust.keyv + } + } + } + if (bid.schain) { + schain = schain || bid.schain; + } iv = iv || utils.getBidIdParameter('iv', bid.params); - bid.sizes = ((utils.isArray(bid.sizes) && utils.isArray(bid.sizes[0])) ? bid.sizes : [bid.sizes]) - bid.sizes = bid.sizes.filter(size => utils.isArray(size)) - const processedSizes = bid.sizes.map(size => ({w: parseInt(size[0], 10), h: parseInt(size[1], 10)})) + + let bidSizes = (bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes) || bid.sizes; + bidSizes = ((utils.isArray(bidSizes) && utils.isArray(bidSizes[0])) ? bidSizes : [bidSizes]) + bidSizes = bidSizes.filter(size => utils.isArray(size)) + const processedSizes = bidSizes.map(size => ({w: parseInt(size[0], 10), h: parseInt(size[1], 10)})) sovrnImps.push({ id: bid.bidId, banner: { @@ -43,15 +57,30 @@ export const spec = { bidfloor: utils.getBidIdParameter('bidfloor', bid.params) }); }); + + const page = bidderRequest.refererInfo.referer + // clever trick to get the domain + const el = document.createElement('a'); + el.href = page; + const domain = el.hostname; + const sovrnBidReq = { id: utils.getUniqueIdentifierStr(), imp: sovrnImps, site: { - domain: loc.host, - page: loc.host + loc.pathname + loc.search + loc.hash + page, + domain } }; + if (schain) { + sovrnBidReq.source = { + ext: { + schain + } + }; + } + if (bidderRequest && bidderRequest.gdprConsent) { sovrnBidReq.regs = { ext: { @@ -63,7 +92,14 @@ export const spec = { }}; } - let url = `//ap.lijit.com/rtb/bid?` + + if (digitrust) { + utils.deepSetValue(sovrnBidReq, 'user.ext.digitrust', { + id: digitrust.id, + keyv: digitrust.keyv + }) + } + + let url = `https://ap.lijit.com/rtb/bid?` + `src=$$REPO_AND_VERSION$$`; if (iv) url += `&iv=${iv}`; @@ -74,7 +110,8 @@ export const spec = { options: {contentType: 'text/plain'} } } catch (e) { - new LogError(e, {bidReqs, bidderRequest}).append() + console.log('error in build:') + console.log(e) } }, @@ -109,74 +146,43 @@ export const spec = { } return sovrnBidResponses } catch (e) { - new LogError(e, {id, seatbid}).append() + console.log('error in interpret:') + console.log(e) } }, getUserSyncs: function(syncOptions, serverResponses, gdprConsent) { try { let tracks = [] - if (serverResponses && serverResponses.length !== 0 && syncOptions.iframeEnabled) { - let iidArr = serverResponses.filter(rsp => rsp.body && rsp.body.ext && rsp.body.ext.iid) - .map(rsp => { return rsp.body.ext.iid }); - let consentString = ''; - if (gdprConsent && gdprConsent.gdprApplies && typeof gdprConsent.consentString === 'string') { - consentString = gdprConsent.consentString + if (serverResponses && serverResponses.length !== 0) { + if (syncOptions.iframeEnabled) { + let iidArr = serverResponses.filter(resp => utils.deepAccess(resp, 'body.ext.iid')) + .map(resp => resp.body.ext.iid); + let consentString = ''; + if (gdprConsent && gdprConsent.gdprApplies && typeof gdprConsent.consentString === 'string') { + consentString = gdprConsent.consentString + } + if (iidArr[0]) { + tracks.push({ + type: 'iframe', + url: '//ap.lijit.com/beacon?informer=' + iidArr[0] + '&gdpr_consent=' + consentString, + }); + } } - if (iidArr[0]) { - tracks.push({ - type: 'iframe', - url: '//ap.lijit.com/beacon?informer=' + iidArr[0] + '&gdpr_consent=' + consentString, - }); + + if (syncOptions.pixelEnabled) { + serverResponses.filter(resp => utils.deepAccess(resp, 'body.ext.sync.pixels')) + .flatMap(resp => resp.body.ext.sync.pixels) + .map(pixel => pixel.url) + .forEach(url => tracks.push({ type: 'image', url })) } } - if (errorpxls.length && syncOptions.pixelEnabled) { - tracks = tracks.concat(errorpxls) - } + return tracks } catch (e) { - if (syncOptions.pixelEnabled) { - return errorpxls - } return [] } }, } -export class LogError { - constructor(e, data) { - utils.logError(e) - this.error = {} - this.error.t = utils.timestamp() - this.error.m = e.message - this.error.s = e.stack - this.error.d = data - this.error.v = $$REPO_AND_VERSION$$ - this.error.u = utils.getTopWindowLocation().href - this.error.ua = navigator.userAgent - } - buildErrorString(obj) { - return errorUrl + '?b=' + btoa(JSON.stringify(obj)) - } - append() { - let errstr = this.buildErrorString(this.error) - if (errstr.length > 2083) { - delete this.error.d - errstr = this.buildErrorString(this.error) - if (errstr.length > 2083) { - delete this.error.s - errstr = this.buildErrorString(this.error) - if (errstr.length > 2083) { - errstr = this.buildErrorString({m: 'unknown error message', t: this.error.t, u: this.error.u}) - } - } - } - let obj = {type: 'image', url: errstr} - errorpxls.push(obj) - } - static getErrPxls() { - return errorpxls - } -} - registerBidder(spec); diff --git a/test/spec/modules/sovrnBidAdapter_spec.js b/test/spec/modules/sovrnBidAdapter_spec.js index 7179ec00bc3..af27e6e74a6 100644 --- a/test/spec/modules/sovrnBidAdapter_spec.js +++ b/test/spec/modules/sovrnBidAdapter_spec.js @@ -1,9 +1,8 @@ -import { expect } from 'chai'; -import { spec, LogError } from 'modules/sovrnBidAdapter'; -import { newBidder } from 'src/adapters/bidderFactory'; -import { SSL_OP_SINGLE_ECDH_USE } from 'constants'; +import {expect} from 'chai'; +import {LogError, spec} from 'modules/sovrnBidAdapter'; +import {newBidder} from 'src/adapters/bidderFactory'; -const ENDPOINT = `//ap.lijit.com/rtb/bid?src=$$REPO_AND_VERSION$$`; +const ENDPOINT = `https://ap.lijit.com/rtb/bid?src=$$REPO_AND_VERSION$$`; describe('sovrnBidAdapter', function() { const adapter = newBidder(spec); @@ -55,8 +54,12 @@ describe('sovrnBidAdapter', function() { 'bidderRequestId': '22edbae2733bf6', 'auctionId': '1d1a030790a475' }]; - - const request = spec.buildRequests(bidRequests); + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + }; + const request = spec.buildRequests(bidRequests, bidderRequest); it('sends bid request to our endpoint via POST', function () { expect(request.method).to.equal('POST'); @@ -73,7 +76,7 @@ describe('sovrnBidAdapter', function() { expect(payload.imp[0].banner.h).to.equal(1) }) - it('accepts a single array as a size', function() { + it('accepts a single array as a size', () => { const singleSize = [{ 'bidder': 'sovrn', 'params': { @@ -85,8 +88,13 @@ describe('sovrnBidAdapter', function() { 'bidId': '30b31c1838de1e', 'bidderRequestId': '22edbae2733bf6', 'auctionId': '1d1a030790a475' - }]; - const request = spec.buildRequests(singleSize) + }] + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + } + const request = spec.buildRequests(singleSize, bidderRequest) const payload = JSON.parse(request.data) expect(payload.imp[0].banner.format).to.deep.equal([{w: 300, h: 250}]) expect(payload.imp[0].banner.w).to.equal(1) @@ -109,7 +117,12 @@ describe('sovrnBidAdapter', function() { 'bidderRequestId': '22edbae2733bf6', 'auctionId': '1d1a030790a475' }]; - const request = spec.buildRequests(ivBidRequests); + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + }; + const request = spec.buildRequests(ivBidRequests, bidderRequest); expect(request.url).to.contain('iv=vet') }); @@ -121,20 +134,22 @@ describe('sovrnBidAdapter', function() { 'auctionId': '1d1a030790a475', 'bidderRequestId': '22edbae2733bf6', 'timeout': 3000, - 'gdprConsent': { + gdprConsent: { consentString: consentString, gdprApplies: true + }, + refererInfo: { + referer: 'http://example.com/page.html', } }; bidderRequest.bids = bidRequests; - const request = spec.buildRequests(bidRequests, bidderRequest); - const payload = JSON.parse(request.data); + const data = JSON.parse(spec.buildRequests(bidRequests, bidderRequest).data); - expect(payload.regs.ext.gdpr).to.exist.and.to.be.a('number'); - expect(payload.regs.ext.gdpr).to.equal(1); - expect(payload.user.ext.consent).to.exist.and.to.be.a('string'); - expect(payload.user.ext.consent).to.equal(consentString); + expect(data.regs.ext.gdpr).to.exist.and.to.be.a('number'); + expect(data.regs.ext.gdpr).to.equal(1); + expect(data.user.ext.consent).to.exist.and.to.be.a('string'); + expect(data.user.ext.consent).to.equal(consentString); }); it('converts tagid to string', function () { @@ -153,10 +168,86 @@ describe('sovrnBidAdapter', function() { 'bidderRequestId': '22edbae2733bf6', 'auctionId': '1d1a030790a475' }]; - const request = spec.buildRequests(ivBidRequests); + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + }; + const request = spec.buildRequests(ivBidRequests, bidderRequest); expect(request.data).to.contain('"tagid":"403370"') - }) + }); + + it('should add schain if present', () => { + const schainRequests = [{ + 'bidder': 'sovrn', + 'params': { + 'tagid': 403370 + }, + 'adUnitCode': 'adunit-code', + 'sizes': [ + [300, 250], + [300, 600] + ], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475', + 'schain': { + 'ver': '1.0', + 'complete': 1, + 'nodes': [ + { + 'asi': 'directseller.com', + 'sid': '00001', + 'rid': 'BidRequest1', + 'hp': 1 + } + ] + } + }].concat(bidRequests); + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + }; + const data = JSON.parse(spec.buildRequests(schainRequests, bidderRequest).data); + + expect(data.source.ext.schain.nodes.length).to.equal(1) + }); + + it('should add digitrust data if present', () => { + const digitrustRequests = [{ + 'bidder': 'sovrn', + 'params': { + 'tagid': 403370 + }, + 'adUnitCode': 'adunit-code', + 'sizes': [ + [300, 250], + [300, 600] + ], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475', + 'userId': { + 'digitrustid': { + 'data': { + 'id': 'digitrust-id-123', + 'keyv': 4 + } + } + } + }].concat(bidRequests); + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', + } + }; + const data = JSON.parse(spec.buildRequests(digitrustRequests, bidderRequest).data); + + expect(data.user.ext.digitrust.id).to.equal('digitrust-id-123'); + expect(data.user.ext.digitrust.keyv).to.equal(4); + }); }); describe('interpretResponse', function () { @@ -254,8 +345,8 @@ describe('sovrnBidAdapter', function() { }); describe('getUserSyncs ', () => { - let syncOptions = {iframeEnabled: true, pixelEnabled: true}; - let iframeDisabledSyncOptions = {iframeEnabled: false, pixelEnabled: true}; + let syncOptions = { iframeEnabled: true, pixelEnabled: false }; + let iframeDisabledSyncOptions = { iframeEnabled: false, pixelEnabled: false }; let serverResponse = [ { 'body': { @@ -287,111 +378,90 @@ describe('sovrnBidAdapter', function() { } ], 'ext': { - 'iid': 13487408 + 'iid': 13487408, + sync: { + pixels: [ + { + url: 'http://idprovider1.com' + }, + { + url: 'http://idprovider2.com' + } + ] + } } }, 'headers': {} } ]; + it('should return if iid present on server response & iframe syncs enabled', () => { - let expectedReturnStatement = [ + const expectedReturnStatement = [ { 'type': 'iframe', 'url': '//ap.lijit.com/beacon?informer=13487408&gdpr_consent=', } - ] - let returnStatement = spec.getUserSyncs(syncOptions, serverResponse); + ]; + const returnStatement = spec.getUserSyncs(syncOptions, serverResponse); expect(returnStatement[0]).to.deep.equal(expectedReturnStatement[0]); - }) + }); it('should not return if iid missing on server response', () => { - let returnStatement = spec.getUserSyncs(syncOptions, []) + const returnStatement = spec.getUserSyncs(syncOptions, []); expect(returnStatement).to.be.empty; - }) + }); it('should not return if iframe syncs disabled', () => { - let returnStatement = spec.getUserSyncs(iframeDisabledSyncOptions, serverResponse) - expect(returnStatement).to.be.empty - }) - }) - describe('LogError', () => { - it('should build and append an error object', () => { - const thrown = { - message: 'message', - stack: 'stack' - } - const data = {name: 'Oscar Hathenswiotch'} - const err = new LogError(thrown, data) - err.append() - const errList = LogError.getErrPxls() - expect(errList.length).to.equal(1) - const errdata = JSON.parse(atob(errList[0].url.split('=')[1])) - expect(errdata.d.name).to.equal('Oscar Hathenswiotch') - }) - it('should drop data when there is too much', () => { - const thrown = { - message: 'message', - stack: 'stack' - } - const tooLong = () => { - let str = '' - for (let i = 0; i < 10000; i++) { - str = str + String.fromCharCode(i % 100) - } - return str - } - const data = {name: 'Oscar Hathenswiotch', tooLong: tooLong()} - const err = new LogError(thrown, data) - err.append() - const errList = LogError.getErrPxls() - expect(errList.length).to.equal(2) - const errdata = JSON.parse(atob(errList[1].url.split('=')[1])) - expect(errdata.d).to.be.an('undefined') - }) - it('should drop data and stack when there is too much', () => { - const thrown = { - message: 'message', - stack: 'stack' - } - const tooLong = () => { - let str = '' - for (let i = 0; i < 10000; i++) { - str = str + String.fromCharCode(i % 100) + const returnStatement = spec.getUserSyncs(iframeDisabledSyncOptions, serverResponse); + expect(returnStatement).to.be.empty; + }); + + it('should include pixel syncs', () => { + let pixelEnabledOptions = { iframeEnabled: false, pixelEnabled: true }; + const returnStatement = spec.getUserSyncs(pixelEnabledOptions, serverResponse); + console.log(returnStatement) + expect(returnStatement.length).to.equal(2); + expect(returnStatement).to.deep.include.members([{ type: 'image', url: 'http://idprovider1.com' }, + { type: 'image', url: 'http://idprovider2.com' }]); + }); + }); + + describe('prebid 3 upgrade', () => { + const bidRequests = [{ + 'bidder': 'sovrn', + 'params': { + 'tagid': '403370' + }, + 'adUnitCode': 'adunit-code', + mediaTypes: { + banner: { + sizes: [ + [300, 250], + [300, 600] + ] } - return str + }, + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475' + }]; + const bidderRequest = { + refererInfo: { + referer: 'http://example.com/page.html', } - const data = {name: 'Oscar Hathenswiotch'} - thrown.stack = tooLong() - const err = new LogError(thrown, data) - err.append() - const errList = LogError.getErrPxls() - expect(errList.length).to.equal(3) - const errdata = JSON.parse(atob(errList[2].url.split('=')[1])) - expect(errdata.d).to.be.an('undefined') - expect(errdata.s).to.be.an('undefined') + }; + const request = spec.buildRequests(bidRequests, bidderRequest); + const payload = JSON.parse(request.data); + + it('gets sizes from mediaTypes.banner', () => { + expect(payload.imp[0].banner.format).to.deep.equal([{w: 300, h: 250}, {w: 300, h: 600}]) + expect(payload.imp[0].banner.w).to.equal(1) + expect(payload.imp[0].banner.h).to.equal(1) }) - it('should drop send a reduced message when other reduction methods fail', () => { - const thrown = { - message: 'message', - stack: 'stack' - } - const tooLong = () => { - let str = '' - for (let i = 0; i < 10000; i++) { - str = str + String.fromCharCode(i % 100) - } - return str - } - const data = {name: 'Oscar Hathenswiotch'} - thrown.message = tooLong() - const err = new LogError(thrown, data) - err.append() - const errList = LogError.getErrPxls() - expect(errList.length).to.equal(4) - const errdata = JSON.parse(atob(errList[3].url.split('=')[1])) - expect(errdata.d).to.be.an('undefined') - expect(errdata.s).to.be.an('undefined') - expect(errdata.m).to.equal('unknown error message') + + it('gets correct site info', () => { + expect(payload.site.page).to.equal('http://example.com/page.html'); + expect(payload.site.domain).to.equal('example.com'); }) }) })