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

Prebid Core: Adding support for a global return of consent metadata. #8043

Merged
merged 5 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 23 additions & 2 deletions src/adapterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,42 @@ function getAdUnitCopyForClientAdapters(adUnits) {

export let gdprDataHandler = {
consentData: null,
setConsentData: function(consentInfo) {
generatedTime: null,
setConsentData: function(consentInfo, time = timestamp()) {
gdprDataHandler.consentData = consentInfo;
gdprDataHandler.generatedTime = time;
},
getConsentData: function() {
return gdprDataHandler.consentData;
},
getConsentMeta: function() {
if (gdprDataHandler.consentData && gdprDataHandler.consentData.vendorData && gdprDataHandler.generatedTime) {
return {
gdprApplies: gdprDataHandler.consentData.gdprApplies,
consentStringSize: gdprDataHandler.consentData.vendorData.tcString.length,
decaffeinatedio marked this conversation as resolved.
Show resolved Hide resolved
decaffeinatedio marked this conversation as resolved.
Show resolved Hide resolved
timestamp: gdprDataHandler.generatedTime,
apiVersion: gdprDataHandler.consentData.apiVersion
};
}
}
};

export let uspDataHandler = {
consentData: null,
setConsentData: function(consentInfo) {
generatedTime: null,
setConsentData: function(consentInfo, time = timestamp()) {
uspDataHandler.consentData = consentInfo;
uspDataHandler.generatedTime = time;
},
getConsentData: function() {
return uspDataHandler.consentData;
},
getConsentMeta: function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the USP case there does not seem to be a distinction between data and metadata - this just adds a timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can further strip this down for consistency, but since this doesn't qualify as personal data, it's OK to reveal.

if (uspDataHandler.consentData && uspDataHandler.generatedTime) {
return {
usp: uspDataHandler.consentData,
timestamp: uspDataHandler.generatedTime}
decaffeinatedio marked this conversation as resolved.
Show resolved Hide resolved
}
}
};

Expand Down
19 changes: 19 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { executeRenderer, isRendererRequired } from './Renderer.js';
import { createBid } from './bidfactory.js';
import { storageCallbacks } from './storageManager.js';
import { emitAdRenderSucceeded, emitAdRenderFail } from './adRendering.js';
import {gdprDataHandler, uspDataHandler} from './adapterManager.js'
decaffeinatedio marked this conversation as resolved.
Show resolved Hide resolved

const $$PREBID_GLOBAL$$ = getGlobal();
const CONSTANTS = require('./constants.json');
Expand Down Expand Up @@ -268,6 +269,24 @@ $$PREBID_GLOBAL$$.getAdserverTargeting = function (adUnitCode) {
return targeting.getAllTargeting(adUnitCode);
};

/**
* returns all consent data
* @return {Object} Map of consent types and data
* @alias module:pbjs.getConsentData
*/
function getConsentMetadata() {
return {
gdpr: gdprDataHandler.getConsentMeta(),
usp: uspDataHandler.getConsentMeta(),
coppa: !!(config.getConfig('coppa'))
}
}

$$PREBID_GLOBAL$$.getConsentMetadata = function () {
logInfo('Invoking $$PREBID_GLOBAL$$.getConsentMetadata');
return getConsentMetadata();
};

function getBids(type) {
const responses = auctionManager[type]()
.filter(bind.call(adUnitsFilter, this, auctionManager.getAdUnitCodes()));
Expand Down
27 changes: 27 additions & 0 deletions test/spec/modules/consentManagementUsp_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe('consentManagement', function () {
sinon.assert.notCalled(utils.logInfo);
});

it('should not produce any USP metadata', function() {
setConsentConfig({});
let consentMeta = uspDataHandler.getConsentMeta();
expect(consentMeta).to.be.undefined;
});

it('should exit the consent manager if only config.gdpr is an object', function() {
setConsentConfig({ gdpr: { cmpApi: 'iab' } });
expect(consentAPI).to.be.undefined;
Expand Down Expand Up @@ -366,6 +372,27 @@ describe('consentManagement', function () {
expect(didHookReturn).to.be.true;
expect(consent).to.equal(testConsentData.uspString);
});

it('returns USP consent metadata', function () {
let testConsentData = {
uspString: '1NY'
};

uspapiStub = sinon.stub(window, '__uspapi').callsFake((...args) => {
args[2](testConsentData, true);
});

setConsentConfig(goodConfig);
requestBidsHook(() => { didHookReturn = true; }, {});

let consentMeta = uspDataHandler.getConsentMeta();

sinon.assert.notCalled(utils.logWarn);
sinon.assert.notCalled(utils.logError);

expect(consentMeta.usp).to.equal(testConsentData.uspString);
expect(consentMeta.timestamp).to.be.above(1644367751709);
});
});
});
});
34 changes: 34 additions & 0 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ describe('consentManagement', function () {
expect(userCMP).to.be.undefined;
sinon.assert.calledOnce(utils.logWarn);
});

it('should not produce any consent metadata', function() {
setConsentConfig(undefined)
let consentMetadata = gdprDataHandler.getConsentMeta();
expect(consentMetadata).to.be.undefined;
sinon.assert.calledOnce(utils.logWarn);
})
});

describe('valid setConsentConfig value', function () {
Expand Down Expand Up @@ -667,6 +674,33 @@ describe('consentManagement', function () {
expect(consent.apiVersion).to.equal(2);
});

it('produces gdpr metadata', function () {
let testConsentData = {
tcString: 'abc12345234',
gdprApplies: true,
purposeOneTreatment: false,
eventStatus: 'tcloaded',
vendorData: {
tcString: 'abc12345234'
}
};
cmpStub = sinon.stub(window, '__tcfapi').callsFake((...args) => {
args[2](testConsentData, true);
});

setConsentConfig(goodConfigWithAllowAuction);

requestBidsHook(() => {
didHookReturn = true;
}, {});
let consentMeta = gdprDataHandler.getConsentMeta();
sinon.assert.notCalled(utils.logError);
expect(consentMeta.consentStringSize).to.be.above(0)
expect(consentMeta.gdprApplies).to.be.true;
expect(consentMeta.apiVersion).to.equal(2);
expect(consentMeta.timestamp).to.be.above(1644367751709);
});

it('performs lookup check and stores consentData for a valid existing user with additional consent', function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a copy of the test case below it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup is the same, but the assertions related to the getConsentMeta() return:

          let consentMeta = gdprDataHandler.getConsentMeta();
          sinon.assert.notCalled(utils.logError);
          expect(consentMeta.consentStringSize).to.be.above(0)
          expect(consentMeta.gdprApplies).to.be.true;
          expect(consentMeta.apiVersion).to.equal(2);
          expect(consentMeta.timestamp).to.be.above(1644367751709);

vs.

          let consent = gdprDataHandler.getConsentData();
          sinon.assert.notCalled(utils.logError);
          expect(didHookReturn).to.be.true;
          expect(consent.consentString).to.equal(testConsentData.tcString);
          expect(consent.addtlConsent).to.equal(testConsentData.addtlConsent);
          expect(consent.gdprApplies).to.be.true;
          expect(consent.apiVersion).to.equal(2);

The checks for consentStringSize and timestamp are specifically novel.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be having a stroke, but I don't see any call to getConsentMeta (test case in lines 704-729). diff tells me it's identical to the test case from lines 730-754.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, looks like I had the stroke. You're right, that's an unnecessary copy/paste. Let me pull the 704 block out and commit it. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgirardi This should do the trick 🤞

let testConsentData = {
tcString: 'abc12345234',
Expand Down