Skip to content

Commit

Permalink
remove shouldDisableConsentRequirement
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Oct 27, 2023
1 parent e8f5a83 commit 26ca6e7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .changeset/fair-pillows-sin.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
---

* Rename `shouldLoad` -> `shouldLoadSegment`
* Rename `ctx.abort({ loadSegmentNormally -> disableConsentRequirement` to be more consistent with the setting that shares that name.
* Remove redundant `shouldDisableConsentRequirement` setting, in favor of shouldLoad's `ctx.abort({ disableConsentRequirement: true/false })`
* Create `shouldLoadWrapper` API for waiting for consent script initialization.
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,26 @@ describe(createWrapper, () => {
)

it('should allow segment to be loaded normally (with all consent wrapper behavior disabled) via ctx.abort', async () => {
const mockCdnSettings = settingsBuilder.build()

wrapTestAnalytics({
shouldLoadSegment: (ctx) => {
ctx.abort({
disableConsentRequirement: true, // magic config option
disableConsentRequirement: true,
})
},
})

await analytics.load(DEFAULT_LOAD_SETTINGS)
const loadArgs: [any, any] = [
{
...DEFAULT_LOAD_SETTINGS,
cdnSettings: mockCdnSettings,
},
{},
]
await analytics.load(...loadArgs)
expect(analyticsLoadSpy).toBeCalled()
expect(getAnalyticsLoadLastCall().args).toEqual(loadArgs)
})

it('should allow segment loading to be completely aborted via ctx.abort', async () => {
Expand Down Expand Up @@ -461,75 +471,6 @@ describe(createWrapper, () => {
})
})

describe('shouldDisableConsentRequirement', () => {
describe('if true on wrapper initialization', () => {
it('should load analytics as usual', async () => {
wrapTestAnalytics({
shouldDisableConsentRequirement: () => true,
})
await analytics.load(DEFAULT_LOAD_SETTINGS)
expect(analyticsLoadSpy).toBeCalled()
})

it('should not call shouldLoadSegment if called on first', async () => {
const shouldLoadSegment = jest.fn()
wrapTestAnalytics({
shouldDisableConsentRequirement: () => true,
shouldLoadSegment,
})
await analytics.load(DEFAULT_LOAD_SETTINGS)
expect(shouldLoadSegment).not.toBeCalled()
})

it('should work with promises if false', async () => {
const shouldLoadSegment = jest.fn()
wrapTestAnalytics({
shouldDisableConsentRequirement: () => Promise.resolve(false),
shouldLoadSegment,
})
await analytics.load(DEFAULT_LOAD_SETTINGS)
expect(shouldLoadSegment).toBeCalled()
})

it('should work with promises if true', async () => {
const shouldLoadSegment = jest.fn()
wrapTestAnalytics({
shouldDisableConsentRequirement: () => Promise.resolve(true),
shouldLoadSegment,
})
await analytics.load(DEFAULT_LOAD_SETTINGS)
expect(shouldLoadSegment).not.toBeCalled()
})

it('should forward all arguments to the original analytics.load method', async () => {
const mockCdnSettings = settingsBuilder.build()

wrapTestAnalytics({
shouldDisableConsentRequirement: () => true,
})

const loadArgs: [any, any] = [
{
...DEFAULT_LOAD_SETTINGS,
cdnSettings: mockCdnSettings,
},
{},
]
await analytics.load(...loadArgs)
expect(analyticsLoadSpy).toBeCalled()
expect(getAnalyticsLoadLastCall().args).toEqual(loadArgs)
})

it('should not stamp the event with consent info', async () => {
wrapTestAnalytics({
shouldDisableConsentRequirement: () => true,
})
await analytics.load(DEFAULT_LOAD_SETTINGS)
expect(addSourceMiddlewareSpy).not.toBeCalled()
})
})
})

describe('shouldDisableSegment', () => {
it('should load analytics if disableAll returns false', async () => {
wrapTestAnalytics({
Expand Down
9 changes: 2 additions & 7 deletions packages/consent/consent-tools/src/domain/create-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const createWrapper = <Analytics extends AnyAnalytics>(

const {
shouldDisableSegment,
shouldDisableConsentRequirement,
getCategories,
shouldLoadSegment,
integrationCategoryMappings,
Expand Down Expand Up @@ -60,13 +59,9 @@ export const createWrapper = <Analytics extends AnyAnalytics>(
let initialCategories: Categories
try {
await loadWrapper
const ctx = new LoadContext()
if (await shouldDisableConsentRequirement?.()) {
throw ctx.abort({ disableConsentRequirement: true })
}

initialCategories =
(await shouldLoadSegment?.(ctx)) || (await getCategories())
(await shouldLoadSegment?.(new LoadContext())) ||
(await getCategories())
} catch (e: unknown) {
// consumer can call ctx.abort({ disableConsentRequirement: true })
// to load Segment but disable consent requirement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export function validateSettings(options: {
options.shouldLoadSegment &&
assertIsFunction(options.shouldLoadSegment, 'shouldLoadSegment')

options.shouldDisableConsentRequirement &&
assertIsFunction(
options.shouldDisableConsentRequirement,
'shouldDisableConsentRequirement'
)

options.shouldEnableIntegration &&
assertIsFunction(options.shouldEnableIntegration, 'shouldEnableIntegration')

Expand Down
7 changes: 0 additions & 7 deletions packages/consent/consent-tools/src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ export interface CreateWrapperSettings {
*/
registerOnConsentChanged?: RegisterOnConsentChangedFunction

/**
* This permanently disables the consent requirement (i.e device mode gating, event pref stamping) enforced by the wrapper.
* This is the same as doing "ctx.abort({ disableConsentRequirement: true })" inside of `shouldLoadSegment`.
* Called once on wrapper initialization. **shouldLoadSegment will never be called**
**/
shouldDisableConsentRequirement?: () => boolean | Promise<boolean>

/**
* Disable the Segment analytics SDK completely. analytics.load() will have no effect.
* .track / .identify etc calls should not throw any errors, but analytics settings will never be fetched and no events will be sent to Segment.
Expand Down

0 comments on commit 26ca6e7

Please sign in to comment.