Skip to content

Commit

Permalink
Update how key-values are passed into ad server.
Browse files Browse the repository at this point in the history
  • Loading branch information
antosarho committed Jun 6, 2024
1 parent cb91a7c commit 433976c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
17 changes: 15 additions & 2 deletions modules/adnuntiusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const misc = {
getUnixTimestamp: function (addDays, asMinutes) {
const multiplication = addDays / (asMinutes ? 1440 : 1);
return Date.now() + (addDays && addDays > 0 ? (1000 * 60 * 60 * 24 * multiplication) : 0);
},
convertObjectToArray: function(obj) {
const convertedArray = [];
for (const prop in obj) {
const arrElement = {};
arrElement[prop] = obj[prop];
convertedArray.push(arrElement);
}
return convertedArray;
}
};

Expand Down Expand Up @@ -201,10 +210,14 @@ const targetingTool = (function() {
},
mergeKvsFromOrtb: function(bidTargeting, bidderRequest) {
const kv = getKvsFromOrtb(bidderRequest || {});
if (!kv) {
if (!kv || Object.keys(kv).length === 0) {
return;
}
bidTargeting.kv = {...kv, ...bidTargeting.kv};
if (bidTargeting.kv && !Array.isArray(bidTargeting.kv)) {
bidTargeting.kv = misc.convertObjectToArray(bidTargeting.kv);
}
bidTargeting.kv = bidTargeting.kv || [];
bidTargeting.kv = bidTargeting.kv.concat(misc.convertObjectToArray(kv));
}
}
})();
Expand Down
94 changes: 88 additions & 6 deletions test/spec/modules/adnuntiusBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,20 @@ describe('adnuntiusBidAdapter', function () {
delete bidderRequests[0].params.targeting;
});

function countMatches(actualArray, expectedValue) {
return actualArray.filter(val => {
return JSON.stringify(val) === JSON.stringify(expectedValue);
}).length;
}

it('should pass site data ext as key values to ad server', function () {
const ortb2 = {
site: {
ext: {
data: {
'12345': 'true',
'45678': 'true',
'9090': 'should-be-overwritten'
'9090': 'should-be-retained'
}
}
}
Expand All @@ -614,26 +620,102 @@ describe('adnuntiusBidAdapter', function () {
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url')
const data = JSON.parse(request[0].data);
expect(data.adUnits[0].kv['12345']).to.equal('true');
expect(data.adUnits[0].kv['45678']).to.equal('true');
expect(data.adUnits[0].kv['9090'][0]).to.equal('take it over');
expect(data.adUnits[0].kv['merge'][0]).to.equal('this');
expect(countMatches(data.adUnits[0].kv, {'9090': ['take it over']})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'merge': ['this']})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'9090': 'should-be-retained'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'45678': 'true'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'12345': 'true'})).to.equal(1);
expect(data.adUnits[0].kv.length).to.equal(5);

delete bidderRequests[0].params.targeting;
});

it('should skip passing site data ext if missing', function () {
it('should pass site data ext as key values to ad server with targeting in different format', function () {
const ortb2 = {
site: {
ext: {
data: {
'12345': 'true',
'45678': 'true',
'9090': 'should-be-retained'
}
}
}
};
bidderRequests[0].params.targeting = {
kv: [
{'merge': ['this']},
{'9090': ['take it over']}
]
};
const request = config.runWithBidder('adnuntius', () => spec.buildRequests(bidderRequests, { ortb2 }));
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url')
const data = JSON.parse(request[0].data);
expect(countMatches(data.adUnits[0].kv, {'9090': ['take it over']})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'merge': ['this']})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'9090': 'should-be-retained'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'45678': 'true'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'12345': 'true'})).to.equal(1);
expect(data.adUnits[0].kv.length).to.equal(5);

delete bidderRequests[0].params.targeting;
});

it('should pass site data ext as key values to ad server even if no kv targeting specified in params.targeting', function () {
const ortb2 = {
site: {
ext: {
data: {
'12345': 'true',
'45678': 'true',
'9090': 'should-be-retained'
}
}
}
};
const request = config.runWithBidder('adnuntius', () => spec.buildRequests(bidderRequests, { ortb2 }));
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url')
const data = JSON.parse(request[0].data);
expect(countMatches(data.adUnits[0].kv, {'9090': 'should-be-retained'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'45678': 'true'})).to.equal(1);
expect(countMatches(data.adUnits[0].kv, {'12345': 'true'})).to.equal(1);
expect(data.adUnits[0].kv.length).to.equal(3);

delete bidderRequests[0].params.targeting;
});

it('should skip passing site ext if missing', function () {
const ortb2 = {
site: {
ext: {
}
}
};

delete bidderRequests[0].params.targeting;
const request = config.runWithBidder('adnuntius', () => spec.buildRequests(bidderRequests, { ortb2 }));
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url');
const data = JSON.parse(request[0].data);
expect(data.adUnits[0]).to.not.have.property('kv');
});

it('should skip passing site ext data if missing', function () {
const ortb2 = {
site: {
ext: {
data: {}
}
}
};

delete bidderRequests[0].params.targeting;
const request = config.runWithBidder('adnuntius', () => spec.buildRequests(bidderRequests, { ortb2 }));
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url');
const data = JSON.parse(request[0].data);
expect(data.adUnits[0]).to.not.have.property('kv');
});

Expand Down

0 comments on commit 433976c

Please sign in to comment.