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

Added keywords parameter to TheMediaGrid Bid Adapter #5353

Merged
merged 15 commits into from
Jun 15, 2020
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
33 changes: 33 additions & 0 deletions modules/gridBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as utils from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { Renderer } from '../src/Renderer.js';
import { VIDEO, BANNER } from '../src/mediaTypes.js';
import {config} from '../src/config.js';

const BIDDER_CODE = 'grid';
const ENDPOINT_URL = 'https://grid.bidswitch.net/hb';
Expand Down Expand Up @@ -47,6 +48,7 @@ export const spec = {
const slotsMapByUid = {};
const sizeMap = {};
const bids = validBidRequests || [];
let pageKeywords = null;
let reqId;

bids.forEach(bid => {
Expand All @@ -55,6 +57,10 @@ export const spec = {
auids.push(uid);
const sizesId = utils.parseSizesInput(bid.sizes);

if (!pageKeywords && !utils.isEmpty(bid.params.keywords)) {
pageKeywords = utils.transformBidderParamKeywords(bid.params.keywords);
}

const addedSizes = {};
sizesId.forEach((sizeId) => {
addedSizes[sizeId] = true;
Expand Down Expand Up @@ -94,6 +100,19 @@ export const spec = {
});
});

const configKeywords = utils.transformBidderParamKeywords({
'user': utils.deepAccess(config.getConfig('fpd.user'), 'keywords') || null,
'context': utils.deepAccess(config.getConfig('fpd.context'), 'keywords') || null
});

if (configKeywords.length) {
pageKeywords = (pageKeywords || []).concat(configKeywords);
}

if (pageKeywords && pageKeywords.length > 0) {
pageKeywords.forEach(deleteValues);
}

const payload = {
auids: auids.join(','),
sizes: utils.getKeys(sizeMap).join(','),
Expand All @@ -102,6 +121,10 @@ export const spec = {
wrapperVersion: '$prebid.version$'
};

if (pageKeywords) {
payload.keywords = JSON.stringify(pageKeywords);
}

if (bidderRequest) {
if (bidderRequest.refererInfo && bidderRequest.refererInfo.referer) {
payload.u = bidderRequest.refererInfo.referer;
Expand Down Expand Up @@ -180,6 +203,16 @@ export const spec = {
}
};

function isPopulatedArray(arr) {
return !!(utils.isArray(arr) && arr.length > 0);
}

function deleteValues(keyPairObj) {
if (isPopulatedArray(keyPairObj.value) && keyPairObj.value[0] === '') {
delete keyPairObj.value;
}
}

function _getBidFromResponse(respItem) {
if (!respItem) {
utils.logError(LOG_ERROR_MESS.emptySeatbid);
Expand Down
8 changes: 6 additions & 2 deletions modules/gridBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ Grid bid adapter supports Banner and Video (instream and outstream).
bidder: "grid",
params: {
uid: 2,
priceType: 'gross'
priceType: 'gross',
keywords: {
brandsafety: ['disaster'],
topic: ['stress', 'fear']
}
}
}
]
Expand All @@ -51,4 +55,4 @@ Grid bid adapter supports Banner and Video (instream and outstream).
]
}
];
```
```
95 changes: 95 additions & 0 deletions test/spec/modules/gridBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { spec, resetUserSync, getSyncUrl } from 'modules/gridBidAdapter.js';
import { newBidder } from 'src/adapters/bidderFactory.js';
import { config } from 'src/config.js';

describe('TheMediaGrid Adapter', function () {
const adapter = newBidder(spec);
Expand Down Expand Up @@ -165,6 +166,100 @@ describe('TheMediaGrid Adapter', function () {
const payload = parseRequest(request.data);
expect(payload).to.have.property('us_privacy', '1YNN');
});

it('should convert keyword params to proper form and attaches to request', function () {
const bidRequestWithKeywords = [].concat(bidRequests);
bidRequestWithKeywords[1] = Object.assign({},
bidRequests[1],
{
params: {
uid: '1',
keywords: {
single: 'val',
singleArr: ['val'],
singleArrNum: [3],
multiValMixed: ['value1', 2, 'value3'],
singleValNum: 123,
emptyStr: '',
emptyArr: [''],
badValue: {'foo': 'bar'} // should be dropped
}
}
}
);

const request = spec.buildRequests(bidRequestWithKeywords, bidderRequest);
expect(request.data).to.be.an('string');
const payload = parseRequest(request.data);
expect(payload.keywords).to.be.an('string');
payload.keywords = JSON.parse(payload.keywords);

expect(payload.keywords).to.deep.equal([{
'key': 'single',
'value': ['val']
}, {
'key': 'singleArr',
'value': ['val']
}, {
'key': 'singleArrNum',
'value': ['3']
}, {
'key': 'multiValMixed',
'value': ['value1', '2', 'value3']
}, {
'key': 'singleValNum',
'value': ['123']
}, {
'key': 'emptyStr'
}, {
'key': 'emptyArr'
}]);
});

it('should mix keyword param with keywords from config', function () {
const getConfigStub = sinon.stub(config, 'getConfig').callsFake(
arg => arg === 'fpd.user' ? {'keywords': ['a', 'b']} : arg === 'fpd.context' ? {'keywords': ['any words']} : null);

const bidRequestWithKeywords = [].concat(bidRequests);
bidRequestWithKeywords[1] = Object.assign({},
bidRequests[1],
{
params: {
uid: '1',
keywords: {
single: 'val',
singleArr: ['val'],
multiValMixed: ['value1', 2, 'value3']
}
}
}
);

const request = spec.buildRequests(bidRequestWithKeywords, bidderRequest);
expect(request.data).to.be.an('string');
const payload = parseRequest(request.data);
expect(payload.keywords).to.be.an('string');
payload.keywords = JSON.parse(payload.keywords);

expect(payload.keywords).to.deep.equal([{
'key': 'single',
'value': ['val']
}, {
'key': 'singleArr',
'value': ['val']
}, {
'key': 'multiValMixed',
'value': ['value1', '2', 'value3']
}, {
'key': 'user',
'value': ['a', 'b']
}, {
'key': 'context',
'value': ['any words']
}]);

getConfigStub.restore();
});
});

describe('interpretResponse', function () {
Expand Down