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

Pub-X Bid Adapter: add adomain support #7103

Merged
merged 2 commits into from
Jun 29, 2021
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
99 changes: 99 additions & 0 deletions modules/pubxBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { registerBidder } from '../src/adapters/bidderFactory.js';
import * as utils from '../src/utils.js';

const BIDDER_CODE = 'pubx';
const BID_ENDPOINT = 'https://api.primecaster.net/adlogue/api/slot/bid';
const USER_SYNC_URL = 'https://api.primecaster.net/primecaster_dmppv.html'
export const spec = {
code: BIDDER_CODE,
isBidRequestValid: function(bid) {
if (!(bid.params.sid)) {
return false;
} else { return true }
},
buildRequests: function(validBidRequests) {
return validBidRequests.map(bidRequest => {
const bidId = bidRequest.bidId;
const params = bidRequest.params;
const sid = params.sid;
const payload = {
sid: sid
};
return {
id: bidId,
method: 'GET',
url: BID_ENDPOINT,
data: payload,
}
});
},
interpretResponse: function(serverResponse, bidRequest) {
const body = serverResponse.body;
const bidResponses = [];
if (body.cid) {
const bidResponse = {
requestId: bidRequest.id,
cpm: body.cpm,
currency: body.currency,
width: body.width,
height: body.height,
creativeId: body.cid,
netRevenue: true,
ttl: body.TTL,
ad: body.adm
};
if (body.adomains) {
utils.deepSetValue(bidResponse, 'meta.advertiserDomains', Array.isArray(body.adomains) ? body.adomains : [body.adomains]);
}
bidResponses.push(bidResponse);
} else {};
return bidResponses;
},
/**
* Determine which user syncs should occur
* @param {object} syncOptions
* @param {array} serverResponses
* @returns {array} User sync pixels
*/
getUserSyncs: function (syncOptions, serverResponses) {
const kwTag = document.getElementsByName('keywords');
let kwString = '';
let kwEnc = '';
let titleContent = !!document.title && document.title;
let titleEnc = '';
let descContent = !!document.getElementsByName('description') && !!document.getElementsByName('description')[0] && document.getElementsByName('description')[0].content;
let descEnc = '';
const pageUrl = location.href.replace(/\?.*$/, '');
const pageEnc = encodeURIComponent(pageUrl);
const refUrl = document.referrer.replace(/\?.*$/, '');
const refEnc = encodeURIComponent(refUrl);
if (kwTag.length) {
const kwContents = kwTag[0].content;
if (kwContents.length > 20) {
const kwArray = kwContents.substr(0, 20).split(',');
kwArray.pop();
kwString = kwArray.join();
} else {
kwString = kwContents;
}
kwEnc = encodeURIComponent(kwString)
} else { }
if (titleContent) {
if (titleContent.length > 30) {
titleContent = titleContent.substr(0, 30);
} else {};
titleEnc = encodeURIComponent(titleContent);
} else { };
if (descContent) {
if (descContent.length > 60) {
descContent = descContent.substr(0, 60);
} else {};
descEnc = encodeURIComponent(descContent);
} else { };
return (syncOptions.iframeEnabled) ? [{
type: 'iframe',
url: USER_SYNC_URL + '?pkw=' + kwEnc + '&pd=' + descEnc + '&pu=' + pageEnc + '&pref=' + refEnc + '&pt=' + titleEnc
}] : [];
}
}
registerBidder(spec);
199 changes: 199 additions & 0 deletions test/spec/modules/pubxBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import {expect} from 'chai';
import {spec} from 'modules/pubxBidAdapter.js';
import {newBidder} from 'src/adapters/bidderFactory.js';
import * as utils from 'src/utils.js';

describe('pubxAdapter', function () {
const adapter = newBidder(spec);
const ENDPOINT = 'https://api.primecaster.net/adlogue/api/slot/bid';

describe('inherited functions', function () {
it('exists and is a function', function () {
expect(adapter.callBids).to.exist.and.to.be.a('function');
});
});

describe('isBidRequestValid', function () {
const bid = {
bidder: 'pubx',
params: {
sid: '12345abc'
}
};

it('should return true when required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

it('should return false when required params are not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.params = {};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});
});

describe('buildRequests', function () {
const bidRequests = [
{
id: '26c1ee0038ac11',
params: {
sid: '12345abc'
}
}
];

const data = {
banner: {
sid: '12345abc'
}
};

it('sends bid request to ENDPOINT via GET', function () {
const request = spec.buildRequests(bidRequests)[0];
expect(request.url).to.equal(ENDPOINT);
expect(request.method).to.equal('GET');
});

it('should attach params to the banner request', function () {
const request = spec.buildRequests(bidRequests)[0];
expect(request.data).to.deep.equal(data.banner);
});
});

describe('getUserSyncs', function () {
const sandbox = sinon.sandbox.create();

const keywordsText = 'meta1,meta2,meta3,meta4,meta5';
const descriptionText = 'description1description2description3description4description5description';

let documentStubMeta;

beforeEach(function () {
documentStubMeta = sandbox.stub(document, 'getElementsByName');
const metaElKeywords = document.createElement('meta');
metaElKeywords.setAttribute('name', 'keywords');
metaElKeywords.setAttribute('content', keywordsText);
documentStubMeta.withArgs('keywords').returns([metaElKeywords]);

const metaElDescription = document.createElement('meta');
metaElDescription.setAttribute('name', 'description');
metaElDescription.setAttribute('content', descriptionText);
documentStubMeta.withArgs('description').returns([metaElDescription]);
});

afterEach(function () {
documentStubMeta.restore();
});

let kwString = '';
let kwEnc = '';
let descContent = '';
let descEnc = '';

it('returns empty sync array when iframe is not enabled', function () {
const syncOptions = {};
expect(spec.getUserSyncs(syncOptions)).to.deep.equal([]);
});

it('returns kwEnc when there is kwTag with more than 20 length', function () {
const kwArray = keywordsText.substr(0, 20).split(',');
kwArray.pop();
kwString = kwArray.join();
kwEnc = encodeURIComponent(kwString);
const syncs = spec.getUserSyncs({ iframeEnabled: true });
expect(syncs[0].url).to.include(`pkw=${kwEnc}`);
});

it('returns kwEnc when there is kwTag with more than 60 length', function () {
descContent = descContent.substr(0, 60);
descEnc = encodeURIComponent(descContent);
const syncs = spec.getUserSyncs({ iframeEnabled: true });
expect(syncs[0].url).to.include(`pkw=${descEnc}`);
});

it('returns titleEnc when there is titleContent with more than 30 length', function () {
let titleText = 'title1title2title3title4title5title';
const documentStubTitle = sandbox.stub(document, 'title').value(titleText);

if (titleText.length > 30) {
titleText = titleText.substr(0, 30);
}

const syncs = spec.getUserSyncs({ iframeEnabled: true });
expect(syncs[0].url).to.include(`pt=${encodeURIComponent(titleText)}`);
});
});

describe('interpretResponse', function () {
const serverResponse = {
body: {
TTL: 300,
adm: '<div>some creative</div>',
cid: 'TKmB',
cpm: 500,
currency: 'JPY',
height: 250,
width: 300,
adomains: [
'test.com'
],
}
}

const bidRequests = [
{
id: '26c1ee0038ac11',
params: {
sid: '12345abc'
}
}
];

const bidResponses = [
{
requestId: '26c1ee0038ac11',
cpm: 500,
currency: 'JPY',
width: 300,
height: 250,
creativeId: 'TKmB',
netRevenue: true,
ttl: 300,
ad: '<div>some creative</div>',
meta: {
advertiserDomains: [
'test.com'
]
},
}
];
it('should return empty array when required param is empty', function () {
const serverResponseWithCidEmpty = {
body: {
TTL: 300,
adm: '<div>some creative</div>',
cid: '',
cpm: '',
currency: 'JPY',
height: 250,
width: 300,
}
}
const result = spec.interpretResponse(serverResponseWithCidEmpty, bidRequests[0]);
expect(result).to.be.empty;
});
it('handles banner responses', function () {
const result = spec.interpretResponse(serverResponse, bidRequests[0])[0];
expect(result.requestId).to.equal(bidResponses[0].requestId);
expect(result.width).to.equal(bidResponses[0].width);
expect(result.height).to.equal(bidResponses[0].height);
expect(result.creativeId).to.equal(bidResponses[0].creativeId);
expect(result.currency).to.equal(bidResponses[0].currency);
expect(result.netRevenue).to.equal(bidResponses[0].netRevenue);
expect(result.ttl).to.equal(bidResponses[0].ttl);
expect(result.ad).to.equal(bidResponses[0].ad);
expect(result.meta.advertiserDomains).deep.to.equal(bidResponses[0].meta.advertiserDomains);
});
});
});