Skip to content

Commit

Permalink
Prebid 3.0 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aprakash-sovrn committed Oct 16, 2019
1 parent 0dd6eb5 commit 1bbab9e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 140 deletions.
73 changes: 19 additions & 54 deletions modules/sovrnBidAdapter.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -24,7 +22,6 @@ export const spec = {
*/
buildRequests: function(bidReqs, bidderRequest) {
try {
const loc = utils.getTopWindowLocation();
let sovrnImps = [];
let iv;
let schain;
Expand All @@ -44,9 +41,11 @@ export const spec = {
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: {
Expand All @@ -58,12 +57,19 @@ 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
}
};

Expand Down Expand Up @@ -95,7 +101,7 @@ export const spec = {
}
}

let url = `//ap.lijit.com/rtb/bid?` +
let url = `https://ap.lijit.com/rtb/bid?` +
`src=$$REPO_AND_VERSION$$`;
if (iv) url += `&iv=${iv}`;

Expand All @@ -106,7 +112,8 @@ export const spec = {
options: {contentType: 'text/plain'}
}
} catch (e) {
new LogError(e, {bidReqs, bidderRequest}).append()
console.log('error in build:')
console.log(e)
}
},

Expand Down Expand Up @@ -141,7 +148,8 @@ export const spec = {
}
return sovrnBidResponses
} catch (e) {
new LogError(e, {id, seatbid}).append()
console.log('error in interpret:')
console.log(e)
}
},

Expand Down Expand Up @@ -169,57 +177,14 @@ export const spec = {
.flatMap(resp => resp.body.ext.sync.pixels)
.map(pixel => pixel.url)
.forEach(url => tracks.push({ type: 'image', url }))

if (errorpxls.length) {
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);
162 changes: 76 additions & 86 deletions test/spec/modules/sovrnBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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);
Expand Down Expand Up @@ -54,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');
Expand All @@ -72,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': {
Expand All @@ -84,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)
Expand All @@ -108,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')
});
Expand All @@ -120,9 +134,12 @@ 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;
Expand Down Expand Up @@ -151,7 +168,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.data).to.contain('"tagid":"403370"')
});
Expand Down Expand Up @@ -183,7 +205,12 @@ describe('sovrnBidAdapter', function() {
]
}
}].concat(bidRequests);
const data = JSON.parse(spec.buildRequests(schainRequests).data);
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)
});
Expand Down Expand Up @@ -211,7 +238,12 @@ describe('sovrnBidAdapter', function() {
}
}
}].concat(bidRequests);
const data = JSON.parse(spec.buildRequests(digitrustRequests).data);
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);
Expand Down Expand Up @@ -394,84 +426,42 @@ describe('sovrnBidAdapter', function() {
});
});

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)
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');
})
})
})

0 comments on commit 1bbab9e

Please sign in to comment.