Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifestreet Bid adapter: initial release #8340

Merged
merged 6 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions modules/lifestreetBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { isInteger } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';

const BIDDER_CODE = 'lifestreet';
const ADAPTER_VERSION = '$prebid.version$';

const urlTemplate = template`https://ads.lfstmedia.com/gate/${'adapter'}/${'slot'}?adkey=${'adkey'}&ad_size=${'ad_size'}&__location=${'location'}&__referrer=${'referrer'}&__wn=${'wn'}&__sf=${'sf'}&__fif=${'fif'}&__if=${'if'}&__stamp=${'stamp'}&__pp=1&__hb=1&_prebid_json=1&__gz=1&deferred_format=vast_2_0,vast_3_0&__hbver=${'hbver'}`;

/**
* A helper function for template to generate string from boolean
*/
function boolToString(value) {
return value ? '1' : '0';
}

/**
* A helper function to form URL from the template
*/
function template(strings, ...keys) {
return function(...values) {
let dict = values[values.length - 1] || {};
let result = [strings[0]];
keys.forEach(function(key, i) {
let value = isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
};
}

/**
* Creates a bid requests for a given bid.
*
* @param {BidRequest} bid The bid params to use for formatting a request
*/
function formatBidRequest(bid, bidderRequest = {}) {
const {params} = bid;
const {referer} = (bidderRequest.refererInfo || {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. refererInfo was updated/refactored in Prebid 7 with #8450 . Not sure if you are expecting refererInfo to act as previous versions.

let url = urlTemplate({
adapter: 'prebid',
slot: params.slot,
adkey: params.adkey,
ad_size: params.ad_size,
location: referer,
referrer: referer,
wn: boolToString(/fb_http/i.test(window.name)),
sf: boolToString(window['sfAPI'] || window['$sf']),
fif: boolToString(window['inDapIF'] === true),
if: boolToString(window !== window.top),
stamp: new Date().getTime(),
hbver: ADAPTER_VERSION
});

if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.gdprApplies !== undefined) {
const gdpr = '&__gdpr=' + (bidderRequest.gdprConsent.gdprApplies ? '1' : '0');
url += gdpr;
}
if (bidderRequest.gdprConsent.consentString !== undefined) {
url += `&__consent=${bidderRequest.gdprConsent.consentString}`;
}
}

// ccpa support
if (bidderRequest.uspConsent) {
url += `&__us_privacy=${bidderRequest.uspConsent}`
}

return {
method: 'GET',
url: url,
bidId: bid.bidId
};
}

function isResponseValid(response) {
return !/^\s*\{\s*"advertisementAvailable"\s*:\s*false/i.test(response.content) &&
response.content.indexOf('<VAST version="2.0"></VAST>') === -1 && /* (typeof response.cpm !== 'undefined') && */
response.status === 1;
}

export const spec = {
code: BIDDER_CODE,
aliases: ['lsm'],
supportedMediaTypes: [BANNER, VIDEO],

isBidRequestValid: (bid = {}) => {
const {params = {}} = bid;
return !!(params.slot && params.adkey && params.ad_size);
},

buildRequests: (validBidRequests, bidderRequest) => {
return validBidRequests.map(bid => {
return formatBidRequest(bid, bidderRequest)
});
},

interpretResponse: (serverResponse, bidRequest) => {
const bidResponses = [];
let response = serverResponse.body;
if (!isResponseValid(response)) {
return bidResponses;
}

const isVideo = response.content_type.indexOf('vast') > -1;
const mediaType = isVideo ? VIDEO : BANNER;

const bidResponse = {
requestId: bidRequest.bidId,
cpm: response.cpm,
currency: response.currency ? response.currency : 'USD',
width: response.width,
height: response.height,
creativeId: response.creativeId,
netRevenue: response.netRevenue ? response.netRevenue : true,
ttl: response.ttl ? response.ttl : 86400,
mediaType,
meta: {
mediaType,
advertiserDomains: response.advertiserDomains
}
};

if (response.hasOwnProperty('dealId')) {
bidResponse.dealId = response.dealId;
}
if (isVideo) {
if (typeof response.vastUrl !== 'undefined') {
bidResponse.vastUrl = response.vastUrl;
} else {
bidResponse.vastXml = response.content;
}
} else {
bidResponse.ad = response.content;
}

bidResponses.push(bidResponse);
return bidResponses;
}
};

registerBidder(spec);
232 changes: 232 additions & 0 deletions test/spec/modules/lifestreetBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import { expect } from 'chai';
import { BANNER, VIDEO } from 'src/mediaTypes.js';
import { spec } from 'modules/lifestreetBidAdapter.js';

describe('lifestreetBidAdapter', function() {
let bidRequests;
let videoBidRequests;
let bidResponses;
let videoBidResponses;
beforeEach(function() {
bidRequests = [
{
bidder: 'lifestreet',
params: {
slot: 'slot166704',
adkey: '78c',
ad_size: '160x600'
},
mediaTypes: {
banner: {
sizes: [
[160, 600],
[300, 600]
]
}
},
sizes: [
[160, 600],
[300, 600]
]
}
];

bidResponses = {
body: {
cpm: 0.1,
netRevenue: true,
content_type: 'display_flash',
width: 160,
currency: 'USD',
ttl: 86400,
content: '<iframe src=\'https://ads.lfstmedia.com/displayAd?__ads=ip37346-u0T8r2uJ8245jv6i2QE8Qu&adt=5748927953915945176&seq=2&plc=K73sUSWIH9WwpIQiGybtg8lYdYFld5NvswMmfGBMz6U&__df=true&clickUrl=https%3A%2F%2Fads.lfstmedia.com%2Fclick%2Fcmp15299%2F5748927953915945176%2F2%3F__ads%3Dip37346-u0T8r2uJ8245jv6i2QE8Qu%26adkey%3D6bd%26slot%3Dslot166704%26__stamp%3D1583331395140%26ad%3Dcrv197050%26_cx%3D%24%24CX%24%24%26_cy%3D%24%24CY%24%24%26_celt%3D%24%24ELT-ID%24%24%26redirectURL%3D\' width=\'160\' height=\'600\' frameBorder=\'0\' scrolling=\'no\' framespacing=\'0\' marginheight=\'0\' marginwidth=\'0\'><!--test--><\/iframe>\n',
creativeId: 7985076,
height: 600,
status: 1
}
};
videoBidRequests = [
{
bidder: 'lifestreet',
params: {
slot: 'slot1227631',
adkey: 'a98',
ad_size: '640x480'
},
mediaTypes: {
video: {
sizes: [
[640, 480],
]
}
}
}
]

videoBidResponses = {
body: {
cpm: 0.1,
netRevenue: true,
content_type: 'vast_3_0',
width: 640,
currency: 'USD',
ttl: 86400,
content: '<iframe src="https://ads.lfstmedia.com/displayAd?__ads=ip38770-u0T8r2uJ8245jv6i2QE8Qu&adt=7734389071973843496&seq=2&plc=QEw02HSSpFZCrGaMUP5rN01QQtwPM5iEI5EQzt1nV4E&__df=true&clickUrl=https%3A%2F%2Fads.lfstmedia.com%2Fclick%2Fcmp20228%2F7734389071973843496%2F2%3F__ads%3Dip38770-u0T8r2uJ8245jv6i2QE8Qu%26adkey%3Dcdc%26slot%3Dslot1227631%26__stamp%3D1583766419183%26ad%3Dcrv225652%26_cx%3D%24%24CX%24%24%26_cy%3D%24%24CY%24%24%26_celt%3D%24%24ELT-ID%24%24%26redirectURL%3D" width="640" height="480" frameBorder="0" scrolling="no" framespacing="0" marginheight="0" marginwidth="0"><!--test--><\/iframe>\n',
creativeId: 108323592,
height: 480,
status: 1
}
};
});
describe('implementation', function() {
describe('Bid validations', function() {
it('valid bid case', function() {
const validBid = {
bidder: 'lifestreet',
params: {
slot: 'slot166704',
adkey: '78c',
ad_size: '160x600'
}
};
expect(spec.isBidRequestValid(validBid)).to.equal(true);
});

it('invalid bid case', function() {
expect(spec.isBidRequestValid()).to.equal(false);
});

it('invalid bid case: slot not passed', function() {
var validBid = {
bidder: 'lifestreet',
params: {
adkey: '78c',
ad_size: '160x600'
}
};
expect(spec.isBidRequestValid(validBid)).to.equal(false);
});

it('invalid bid case: adkey not passed', function() {
const validBid = {
bidder: 'lifestreet',
params: {
slot: 'slot166704',
ad_size: '160x600'
}
};
expect(spec.isBidRequestValid(validBid)).to.equal(false);
});

it('invalid bid case: ad_size is not passed', function() {
const validBid = {
bidder: 'lifestreet',
params: {
slot: 'slot166704',
adkey: '78c'
}
};
expect(spec.isBidRequestValid(validBid)).to.equal(false);
});
});

describe('buildRequests spec method', function () {
it('method exists and is a function', function () {
expect(spec.buildRequests).to.exist.and.to.be.a('function');
});

it('should not return request when no bids are present', function () {
const [request] = spec.buildRequests([]);
expect(request).to.be.undefined;
});

it('should return an url and request method ', function () {
const [request] = spec.buildRequests(bidRequests);
expect(request.method).to.equal('GET');
expect(request.url).to.be.a('string');
});

it('should return an url that contains all required fields', function () {
const [request] = spec.buildRequests(bidRequests);
expect(request.url).to.have.string('adkey');
expect(request.url).to.have.string('slot');
expect(request.url).to.have.string('ad_size');
});

it('should add GDPR consent information to the request', function () {
let consentString = 'BOJ8RZsOJ8RZsABAB8AAAAAZ+A==';
let bidderRequest = {
bidderCode: 'lifestreet',
auctionId: '1d1a030790a875',
bidderRequestId: '22edbae2744bf6',
timeout: 3000,
gdprConsent: {
consentString: consentString,
gdprApplies: true
}
};
bidderRequest.bids = bidRequests;

const [request] = spec.buildRequests(bidRequests, bidderRequest);
expect(request.url).to.have.string('__gdpr=1');
expect(request.url).to.have.string(`__consent=${consentString}`);
});

it('should add US privacy string to request', function() {
let consentString = '1YA-';
let bidderRequest = {
bidderCode: 'lifestreet',
auctionId: '1d1a030790a875',
bidderRequestId: '22edbae2744bf6',
timeout: 3000,
uspConsent: consentString
};
bidderRequest.bids = bidRequests;

const [request] = spec.buildRequests(bidRequests, bidderRequest);
expect(request.url).to.have.string(`__us_privacy=${consentString}`);
});
});

describe('server response', function() {
it('should return valid proper values', function() {
const request = spec.buildRequests(bidRequests);
const response = spec.interpretResponse(bidResponses, request);
expect(response).to.be.an('array').with.length.above(0);
expect(response[0].cpm).to.equal(bidResponses.body.cpm);
expect(response[0].width).to.equal(bidResponses.body.width);
expect(response[0].height).to.equal(bidResponses.body.height);
expect(response[0].creativeId).to.equal(bidResponses.body.creativeId);
expect(response[0].currency).to.equal('USD');
expect(response[0].netRevenue).to.equal(true);
expect(response[0].ttl).to.equal(bidResponses.body.ttl);
});

it('should return proper mediaType for BANNER', function() {
const request = spec.buildRequests(bidRequests);
const [response] = spec.interpretResponse(bidResponses, request);
expect(response.mediaType).to.equal(BANNER);
});

it('should return proper mediaType for VIDEO', function() {
const request = spec.buildRequests(videoBidRequests);
const [response] = spec.interpretResponse(videoBidResponses, request);
expect(response.mediaType).to.equal(VIDEO);
});

it('should return a VAST XML for VIDEO', function() {
const request = spec.buildRequests(videoBidRequests);
const [response] = spec.interpretResponse(videoBidResponses, request);
expect(response.vastXml).to.be.a('string');
expect(response.vastXml).to.have.string('iframe');
});

it('should return an ad content for BANNER', function() {
const request = spec.buildRequests(bidRequests);
const [response] = spec.interpretResponse(bidResponses, request);
expect(response.ad).to.be.a('string');
expect(response.ad).to.have.string('iframe');
});
});
});
});