Skip to content

Commit

Permalink
Handle kvs and segments better from multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
antosarho committed May 17, 2024
1 parent 5426945 commit 76651cf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
86 changes: 53 additions & 33 deletions modules/adnuntiusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,6 @@ const storageTool = (function () {
return (meta && meta.usi) ? meta.usi : false
}

const getSegmentsFromOrtb = function (ortb2) {
const userData = deepAccess(ortb2, 'user.data');
let segments = [];
if (userData) {
userData.forEach(userdat => {
if (userdat.segment) {
segments.push(...userdat.segment.map((segment) => {
if (isStr(segment)) return segment;
if (isStr(segment.id)) return segment.id;
}).filter((seg) => !!seg));
}
});
}
return segments
}

const getKvsFromOrtb = function (ortb2) {
const siteData = deepAccess(ortb2, 'site.ext.data');
if (siteData) {
return siteData
} else {
return null
}
}

return {
refreshStorage: function (bidderRequest) {
const ortb2 = bidderRequest.ortb2 || {};
Expand All @@ -171,25 +146,69 @@ const storageTool = (function () {
return voidAuId.auId;
});
}
metaInternal.segments = getSegmentsFromOrtb(ortb2);
metaInternal.kv = getKvsFromOrtb(ortb2);
},
saveToStorage: function (serverData, network) {
setMetaInternal(serverData, network);
},
getUrlRelatedData: function () {
// getting the URL information is theoretically not network-specific
const { segments, kv, usi, voidAuIdsArray } = metaInternal;
return { segments, kv, usi, voidAuIdsArray };
const { usi, voidAuIdsArray } = metaInternal;
return { usi, voidAuIdsArray };
},
getPayloadRelatedData: function (network) {
// getting the payload data should be network-specific
const { segments, kv, usi, userId, voidAuIdsArray, voidAuIds, ...payloadRelatedData } = getMetaDataFromLocalStorage(network).reduce((a, entry) => ({ ...a, [entry.key]: entry.value }), {});
const { segments, usi, userId, voidAuIdsArray, voidAuIds, ...payloadRelatedData } = getMetaDataFromLocalStorage(network).reduce((a, entry) => ({ ...a, [entry.key]: entry.value }), {});
return payloadRelatedData;
}
};
})();

const targetingTool = (function() {
const getSegmentsFromOrtb = function(bidderRequest) {
const userData = deepAccess(bidderRequest.ortb2 || {}, 'user.data');
let segments = [];
if (userData) {
userData.forEach(userdat => {
if (userdat.segment) {
segments.push(...userdat.segment.map((segment) => {
if (isStr(segment)) return segment;
if (isStr(segment.id)) return segment.id;
}).filter((seg) => !!seg));
}
});
}
return segments
};

const getKvsFromOrtb = function(bidderRequest) {
return deepAccess(bidderRequest.ortb2 || {}, 'site.ext.data');
};

return {
addSegmentsToUrlData: function (validBids, bidderRequest, existingUrlRelatedData) {
let segments = getSegmentsFromOrtb(bidderRequest || {});

for (let i = 0; i < validBids.length; i++) {
const bid = validBids[i];
const targeting = bid.params.targeting || {};
if (Array.isArray(targeting.segments)) {
segments = segments.concat(targeting.segments);
delete bid.params.targeting.segments;
}
}

existingUrlRelatedData.segments = segments;
},
mergeKvsFromOrtb: function(bidTargeting, bidderRequest) {
const kv = getKvsFromOrtb(bidderRequest || {});
if (!kv) {
return;
}
bidTargeting.kv = {...kv, ...bidTargeting.kv};
}
}
})();

const validateBidType = function (bidTypeOption) {
return VALID_BID_TYPES.indexOf(bidTypeOption || '') > -1 ? bidTypeOption : 'bid';
}
Expand Down Expand Up @@ -227,6 +246,7 @@ export const spec = {
storageTool.refreshStorage(bidderRequest);

const urlRelatedMetaData = storageTool.getUrlRelatedData();
targetingTool.addSegmentsToUrlData(validBidRequests, bidderRequest, urlRelatedMetaData);
if (urlRelatedMetaData.segments.length > 0) queryParamsAndValues.push('segments=' + urlRelatedMetaData.segments.join(','));
if (urlRelatedMetaData.usi) queryParamsAndValues.push('userId=' + urlRelatedMetaData.usi);

Expand Down Expand Up @@ -261,9 +281,9 @@ export const spec = {
networks[network].metaData = payloadRelatedData;
}

const targeting = bid.params.targeting || {};
if (urlRelatedMetaData.kv) targeting.kv = urlRelatedMetaData.kv;
const adUnit = { ...targeting, auId: bid.params.auId, targetId: bid.params.targetId || bid.bidId };
const bidTargeting = {...bid.params.targeting || {}};
targetingTool.mergeKvsFromOrtb(bidTargeting, bidderRequest);
const adUnit = { ...bidTargeting, auId: bid.params.auId, targetId: bid.params.targetId || bid.bidId };
const maxDeals = Math.max(0, Math.min(bid.params.maxDeals || 0, MAXIMUM_DEALS_LIMIT));
if (maxDeals > 0) {
adUnit.maxDeals = maxDeals;
Expand Down
26 changes: 20 additions & 6 deletions test/spec/modules/adnuntiusBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ describe('adnuntiusBidAdapter', function () {
expect(request[0].url).to.equal(ENDPOINT_URL_VIDEO);
});

it('should pass segments if available in config', function () {
it('should pass segments if available in config and merge from targeting', function () {
const ortb2 = {
user: {
data: [{
Expand All @@ -580,10 +580,16 @@ describe('adnuntiusBidAdapter', function () {
}
};

bidderRequests[0].params.targeting = {
segments: ['merge-this', 'and-this']
};

const request = config.runWithBidder('adnuntius', () => spec.buildRequests(bidderRequests, { ortb2 }));
expect(request.length).to.equal(1);
expect(request[0]).to.have.property('url')
expect(request[0].url).to.equal(ENDPOINT_URL_SEGMENTS);
expect(request[0].url).to.equal(ENDPOINT_URL_SEGMENTS.replace('segment3', 'segment3,merge-this,and-this'));

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

it('should pass site data ext as key values to ad server', function () {
Expand All @@ -592,20 +598,28 @@ describe('adnuntiusBidAdapter', function () {
ext: {
data: {
'12345': 'true',
'45678': 'true'
'45678': 'true',
'9090': 'should-be-overwritten'
}
}
}
};

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(data.adUnits[0].kv).to.have.property('12345');
expect(data.adUnits[0].kv['12345']).to.equal('true');
expect(data.adUnits[0].kv).to.have.property('45678');
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');

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

it('should skip passing site data ext if missing', function () {
Expand Down

0 comments on commit 76651cf

Please sign in to comment.