Skip to content

Commit

Permalink
Added keywords parameter to TheMediaGrid Bid Adapter (prebid#5353)
Browse files Browse the repository at this point in the history
* Added TheMediaGridNM Bid Adapter

* Updated required params for TheMediaGridNM Bid Adapter

* Update TheMediGridNM Bid Adapter

* Fix tests for TheMediaGridNM Bid Adapter

* Fixes after review for TheMediaGridNM Bid Adapter

* Add support of multi-format in TheMediaGrid Bid Adapter

* Update sync url for grid and gridNM Bid Adapters

* TheMediaGrid Bid Adapter: added keywords adUnit parameter

* Update TheMediaGrid Bid Adapter to support keywords from config
  • Loading branch information
TheMediaGrid authored and iggyfisk committed Jun 22, 2020
1 parent 5821377 commit a7ed98a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
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

0 comments on commit a7ed98a

Please sign in to comment.