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

PBjs Core Targeting: allow non-string (eg. numeric) targeting segments #7160

Merged
merged 1 commit into from
Aug 12, 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
4 changes: 3 additions & 1 deletion src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@ export function newTargeting(auctionManager) {

return Object.keys(aut)
.map(function(key) {
return {[key]: utils.isArray(aut[key]) ? aut[key] : aut[key].split(',')};
if (utils.isStr(aut[key])) aut[key] = aut[key].split(',');
if (!utils.isArray(aut[key])) aut[key] = [ aut[key] ];
return { [key]: aut[key] };
});
}

Expand Down
47 changes: 46 additions & 1 deletion test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { targeting as targetingInstance, filters, getHighestCpmBidsFromBidPool, sortByDealAndPriceBucketOrCpm } from 'src/targeting.js';
import { config } from 'src/config.js';
import { getAdUnits, createBidReceived } from 'test/fixtures/fixtures.js';
import { createBidReceived } from 'test/fixtures/fixtures.js';
import CONSTANTS from 'src/constants.json';
import { auctionManager } from 'src/auctionManager.js';
import * as utils from 'src/utils.js';
Expand Down Expand Up @@ -280,6 +280,51 @@ describe('targeting tests', function () {
bidExpiryStub.restore();
});

describe('when handling different adunit targeting value types', function () {
const adUnitCode = '/123456/header-bid-tag-0';
const adServerTargeting = {};

let getAdUnitsStub;

before(function() {
getAdUnitsStub = sandbox.stub(auctionManager, 'getAdUnits').callsFake(function() {
return [
{
'code': adUnitCode,
[CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING]: adServerTargeting
}
];
});
});

after(function() {
getAdUnitsStub.restore();
});

afterEach(function() {
delete adServerTargeting.test_type;
});

const pairs = [
['string', '2.3', '2.3'],
['number', 2.3, '2.3'],
['boolean', true, 'true'],
['string-separated', '2.3,4.5', '2.3, 4.5'],
['array-of-string', ['2.3', '4.5'], '2.3, 4.5'],
['array-of-number', [2.3, 4.5], '2.3, 4.5'],
['array-of-boolean', [true, false], 'true, false']
];
pairs.forEach(([type, value, result]) => {
it(`accepts ${type}`, function() {
adServerTargeting.test_type = value;

const targeting = targetingInstance.getAllTargeting([adUnitCode]);

expect(targeting[adUnitCode].test_type).is.equal(result);
});
});
});

describe('when hb_deal is present in bid.adserverTargeting', function () {
let bid4;

Expand Down