Skip to content

Commit

Permalink
Prebid 7: Allow USP module to work without explicit configuration (#8229
Browse files Browse the repository at this point in the history
)

* Update consentManagementUsp.js

* Update consentManagementUsp.js

* Update consentManagementUsp_spec.js

* Update consentManagementUsp.js

* Update consentManagementUsp_spec.js

* Update consentManagementUsp.js

* Update consentManagementUsp.js

* Update consentManagement.js

* Update consentManagementUsp_spec.js

* Automatically enable USP on module load

* Add a test case for config after activation

Co-authored-by: Demetrio Girardi <dgirardi@prebid.org>
  • Loading branch information
patmmccann and dgirardi authored Jun 2, 2022
1 parent 4a25c2e commit da54a9c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
1 change: 1 addition & 0 deletions modules/consentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ function storeConsentData(cmpConsentObject) {
export function resetConsentData() {
consentData = undefined;
userCMP = undefined;
consentTimeout = undefined;
cmpVersion = 0;
gdprDataHandler.reset();
}
Expand Down
26 changes: 16 additions & 10 deletions modules/consentManagementUsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import { isFn, logInfo, logWarn, isStr, isNumber, isPlainObject, logError } from '../src/utils.js';
import { config } from '../src/config.js';
import { uspDataHandler } from '../src/adapterManager.js';
import {getGlobal} from '../src/prebidGlobal.js';

const DEFAULT_CONSENT_API = 'iab';
const DEFAULT_CONSENT_TIMEOUT = 50;
const USPAPI_VERSION = 1;

export let consentAPI;
export let consentTimeout;
export let consentAPI = DEFAULT_CONSENT_API;
export let consentTimeout = DEFAULT_CONSENT_TIMEOUT;
export let staticConsentData;

let consentData;
Expand Down Expand Up @@ -254,7 +255,10 @@ function storeUspConsentData(consentObject) {
export function resetConsentData() {
consentData = undefined;
consentAPI = undefined;
consentTimeout = undefined;
uspDataHandler.reset();
getGlobal().requestBids.getHooks({hook: requestBidsHook}).remove();
addedConsentHook = false;
}

/**
Expand All @@ -264,25 +268,21 @@ export function resetConsentData() {
export function setConsentConfig(config) {
config = config && config.usp;
if (!config || typeof config !== 'object') {
logWarn('consentManagement.usp config not defined, exiting usp consent manager');
return;
logWarn('consentManagement.usp config not defined, using defaults');
}
if (isStr(config.cmpApi)) {
if (config && isStr(config.cmpApi)) {
consentAPI = config.cmpApi;
} else {
consentAPI = DEFAULT_CONSENT_API;
logInfo(`consentManagement.usp config did not specify cmpApi. Using system default setting (${DEFAULT_CONSENT_API}).`);
}

if (isNumber(config.timeout)) {
if (config && isNumber(config.timeout)) {
consentTimeout = config.timeout;
} else {
consentTimeout = DEFAULT_CONSENT_TIMEOUT;
logInfo(`consentManagement.usp config did not specify timeout. Using system default setting (${DEFAULT_CONSENT_TIMEOUT}).`);
}

logInfo('USPAPI consentManagement module has been activated...');

if (consentAPI === 'static') {
if (isPlainObject(config.consentData) && isPlainObject(config.consentData.getUSPData)) {
if (config.consentData.getUSPData.uspString) staticConsentData = { usPrivacy: config.consentData.getUSPData.uspString };
Expand All @@ -291,11 +291,17 @@ export function setConsentConfig(config) {
logError(`consentManagement config with cmpApi: 'static' did not specify consentData. No consents will be available to adapters.`);
}
}
enableConsentManagement(true);
}

function enableConsentManagement(configFromUser = false) {
if (!addedConsentHook) {
$$PREBID_GLOBAL$$.requestBids.before(requestBidsHook, 50);
logInfo(`USPAPI consentManagement module has been activated${configFromUser ? '' : ` using default values (api: '${consentAPI}', timeout: ${consentTimeout}ms)`}`);
getGlobal().requestBids.before(requestBidsHook, 50);
}
addedConsentHook = true;
uspDataHandler.enable();
loadConsentData(); // immediately look up consent data to make it available without requiring an auction
}
config.getConfig('consentManagement', config => setConsentConfig(config.consentManagement));
setTimeout(() => !addedConsentHook && enableConsentManagement())
47 changes: 33 additions & 14 deletions test/spec/modules/consentManagementUsp_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@ function createIFrameMarker() {
}

describe('consentManagement', function () {
it('should be enabled by default', () => {
expect(uspDataHandler.enabled).to.be.true;
});
it('should respect configuration set after activation', () => {
setConsentConfig({
usp: {
cmpApi: 'static',
consentData: {
getUSPData: {
uspString: '1YYY'
}
}
}
});
expect(uspDataHandler.getConsentData()).to.equal('1YYY');
})

describe('setConsentConfig tests:', function () {
describe('empty setConsentConfig value', function () {
before(resetConsentData);

beforeEach(function () {
sinon.stub(utils, 'logInfo');
sinon.stub(utils, 'logWarn');
Expand All @@ -37,11 +56,11 @@ describe('consentManagement', function () {
resetConsentData();
});

it('should not run if no config', function () {
it('should run with defaults if no config', function () {
setConsentConfig({});
expect(consentAPI).to.be.undefined;
expect(consentTimeout).to.be.undefined;
sinon.assert.callCount(utils.logWarn, 1);
expect(consentAPI).to.be.equal('iab');
expect(consentTimeout).to.be.equal(50);
sinon.assert.callCount(utils.logInfo, 3);
});

it('should use system default values', function () {
Expand All @@ -51,11 +70,11 @@ describe('consentManagement', function () {
sinon.assert.callCount(utils.logInfo, 3);
});

it('should exit the consent manager if config.usp is not an object', function() {
it('should not exit the consent manager if config.usp is not an object', function() {
setConsentConfig({});
expect(consentAPI).to.be.undefined;
sinon.assert.calledOnce(utils.logWarn);
sinon.assert.notCalled(utils.logInfo);
expect(consentAPI).to.be.equal('iab');
expect(consentTimeout).to.be.equal(50);
sinon.assert.callCount(utils.logInfo, 3);
});

it('should not produce any USP metadata', function() {
Expand All @@ -66,16 +85,16 @@ describe('consentManagement', function () {

it('should exit the consent manager if only config.gdpr is an object', function() {
setConsentConfig({ gdpr: { cmpApi: 'iab' } });
expect(consentAPI).to.be.undefined;
sinon.assert.calledOnce(utils.logWarn);
sinon.assert.notCalled(utils.logInfo);
expect(consentAPI).to.be.equal('iab');
expect(consentTimeout).to.be.equal(50);
sinon.assert.callCount(utils.logInfo, 3);
});

it('should exit consentManagementUsp module if config is "undefined"', function() {
setConsentConfig(undefined);
expect(consentAPI).to.be.undefined;
sinon.assert.calledOnce(utils.logWarn);
sinon.assert.notCalled(utils.logInfo);
expect(consentAPI).to.be.equal('iab');
expect(consentTimeout).to.be.equal(50);
sinon.assert.callCount(utils.logInfo, 3);
});

it('should immediately start looking up consent data', () => {
Expand Down

0 comments on commit da54a9c

Please sign in to comment.