Skip to content

Commit

Permalink
Allow bidder-specific FPD enrichments
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Aug 1, 2024
1 parent 6fd6d76 commit 2fb7452
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 24 deletions.
8 changes: 4 additions & 4 deletions modules/consentManagementGpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ export function setConsentConfig(config) {
config.getConfig('consentManagement', config => setConsentConfig(config.consentManagement));

export function enrichFPDHook(next, fpd) {
return next(fpd.then(ortb2 => {
return next(fpd.then(ortb2Fragments => {
const consent = gppDataHandler.getConsentData();
if (consent) {
if (Array.isArray(consent.applicableSections)) {
deepSetValue(ortb2, 'regs.gpp_sid', consent.applicableSections);
deepSetValue(ortb2Fragments.global, 'regs.gpp_sid', consent.applicableSections);
}
deepSetValue(ortb2, 'regs.gpp', consent.gppString);
deepSetValue(ortb2Fragments.global, 'regs.gpp', consent.gppString);
}
return ortb2;
return ortb2Fragments;
}));
}

Expand Down
5 changes: 3 additions & 2 deletions modules/consentManagementTcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ export function setConsentConfig(config) {
config.getConfig('consentManagement', config => setConsentConfig(config.consentManagement));

export function enrichFPDHook(next, fpd) {
return next(fpd.then(ortb2 => {
return next(fpd.then(ortb2Fragments => {
const ortb2 = ortb2Fragments.global;
const consent = gdprDataHandler.getConsentData();
if (consent) {
if (typeof consent.gdprApplies === 'boolean') {
Expand All @@ -284,7 +285,7 @@ export function enrichFPDHook(next, fpd) {
if (dsaPlatform) {
deepSetValue(ortb2, 'regs.ext.dsa.dsarequired', 3);
}
return ortb2;
return ortb2Fragments;
}));
}

Expand Down
6 changes: 3 additions & 3 deletions modules/consentManagementUsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ config.getConfig('consentManagement', config => setConsentConfig(config.consentM
getHook('requestBids').before(requestBidsHook, 50);

export function enrichFPDHook(next, fpd) {
return next(fpd.then(ortb2 => {
return next(fpd.then(ortb2Fragments => {
const consent = uspDataHandler.getConsentData();
if (consent) {
deepSetValue(ortb2, 'regs.ext.us_privacy', consent)
deepSetValue(ortb2Fragments.global, 'regs.ext.us_privacy', consent)
}
return ortb2;
return ortb2Fragments;
}))
}

Expand Down
5 changes: 3 additions & 2 deletions src/fpd/enrichment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const enrichFPD = hook('sync', (fpd) => {
const promArr = [fpd, getSUA().catch(() => null), tryToGetCdepLabel().catch(() => null)];

return GreedyPromise.all(promArr)
.then(([ortb2, sua, cdep]) => {
.then(([ortb2Fragments, sua, cdep]) => {
let ortb2 = ortb2Fragments.global;
const ri = dep.getRefererInfo();
Object.entries(ENRICHMENTS).forEach(([section, getEnrichments]) => {
const data = getEnrichments(ortb2, ri);
Expand Down Expand Up @@ -59,7 +60,7 @@ export const enrichFPD = hook('sync', (fpd) => {
}
}

return ortb2;
return ortb2Fragments;
});
});

Expand Down
3 changes: 1 addition & 2 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ pbjsInstance.requestBids = (function() {
global: mergeDeep({}, config.getAnyConfig('ortb2') || {}, ortb2 || {}),
bidder: Object.fromEntries(Object.entries(config.getBidderConfig()).map(([bidder, cfg]) => [bidder, cfg.ortb2]).filter(([_, ortb2]) => ortb2 != null))
}
return enrichFPD(GreedyPromise.resolve(ortb2Fragments.global)).then(global => {
ortb2Fragments.global = global;
return enrichFPD(GreedyPromise.resolve(ortb2Fragments)).then(ortb2Fragments => {
return startAuction({bidsBackHandler, timeout: cbTimeout, adUnits, adUnitCodes, labels, auctionId, ttlBuffer, ortb2Fragments, metrics, defer});
})
}, 'requestBids');
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/fpd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function mockFpdEnrichments(sandbox, overrides = {}) {
export function addFPDEnrichments(ortb2 = {}, overrides) {
const sandbox = sinon.sandbox.create();
mockFpdEnrichments(sandbox, overrides)
return enrichFPD(GreedyPromise.resolve(deepClone(ortb2))).finally(() => sandbox.restore());
return enrichFPD(GreedyPromise.resolve({global: deepClone(ortb2)})).finally(() => sandbox.restore());
}

export const syncAddFPDEnrichments = synchronize(addFPDEnrichments);
Expand All @@ -52,7 +52,7 @@ export function addFPDToBidderRequest(bidderRequest, overrides) {
return bidderRequest.uspConsent;
}
}, overrides);
return addFPDEnrichments(bidderRequest.ortb2 || {}, overrides).then(ortb2 => {
return addFPDEnrichments(bidderRequest.ortb2 || {}, overrides).then(({global: ortb2}) => {
return {
...bidderRequest,
ortb2
Expand Down
2 changes: 1 addition & 1 deletion test/spec/fpd/enrichment_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('FPD enrichment', () => {
});

function fpd(ortb2 = {}) {
return enrichFPD(Promise.resolve(ortb2));
return enrichFPD(Promise.resolve({global: ortb2})).then(({global}) => global);
}

function mockWindow() {
Expand Down
4 changes: 2 additions & 2 deletions test/spec/fpd/gdpr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('GDPR FPD enrichment', () => {

function callHook(ortb2 = {}) {
let result;
enrichFPDHook((res) => { result = res }, Promise.resolve(ortb2));
return result;
enrichFPDHook((res) => { result = res }, Promise.resolve({global: ortb2}));
return result.then(({global}) => global);
}

it('sets gdpr properties from gdprDataHandler', () => {
Expand Down
41 changes: 41 additions & 0 deletions test/spec/fpd/gpp_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {enrichFPDHook} from '../../../modules/consentManagementGpp.js';
import {gppDataHandler} from '../../../src/adapterManager.js';

describe('gpp FPD enrichment', () => {
let sandbox, consent;
beforeEach(() => {
sandbox = sinon.sandbox.create();
consent = null;
sandbox.stub(gppDataHandler, 'getConsentData').callsFake(() => consent);
})

afterEach(() => {
sandbox.restore();
})

function enrich() {
let result;
enrichFPDHook((val) => {
result = val;
}, Promise.resolve({global: {}}));
return result.then(({global}) => global);
}

it('should add consent data', () => {
consent = {
applicableSections: [1, 2],
gppString: 'ABC'
};
return enrich().then(ortb2 => {
expect(ortb2.regs.gpp).to.eql('ABC');
expect(ortb2.regs.gpp_sid).to.eql([1, 2]);
});
});

it('should not run if consent is missing', () => {
return enrich().then(ortb2 => {
expect(ortb2.regs?.gpp).to.not.exist;
expect(ortb2.regs?.gpp_sid).to.not.exist;
})
})
})
4 changes: 2 additions & 2 deletions test/spec/fpd/usp_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('FPD enrichment USP', () => {
let result;
enrichFPDHook((res) => {
result = res;
}, Promise.resolve({}));
return result;
}, Promise.resolve({global: {}}));
return result.then(({global}) => global);
}

it('sets regs.ext.us_privacy from uspDataHandler', () => {
Expand Down
19 changes: 15 additions & 4 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,17 +1819,28 @@ describe('Unit: Prebid Module', function () {

it('enriched through enrichFPD', () => {
function enrich(next, fpd) {
next.bail(fpd.then(ortb2 => {
ortb2.enrich = true;
return ortb2;
next.bail(fpd.then((ortb2Fragments) => {
const {global, bidder} = ortb2Fragments;
global.enrich = true;
bidder.mockBidder.bEnrich = true
return ortb2Fragments;
}))
}
enrichFPD.before(enrich);
try {
configObj.setConfig({ortb2: globalFPD});
configObj.setBidderConfig({bidders: ['mockBidder'], config: {ortb2: {bidder: 'config'}}})
$$PREBID_GLOBAL$$.requestBids({ortb2: auctionFPD});
sinon.assert.calledWith(startAuctionStub, sinon.match({
ortb2Fragments: {global: {...mergedFPD, enrich: true}}
ortb2Fragments: {
global: {...mergedFPD, enrich: true},
bidder: {
mockBidder: {
bidder: 'config',
bEnrich: true
}
}
}
}));
} finally {
enrichFPD.getHooks({hook: enrich}).remove();
Expand Down

0 comments on commit 2fb7452

Please sign in to comment.