Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Apr 12, 2024
1 parent 7478e56 commit f505ae7
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initMockConsentManager } from '../helpers/mock-cmp'
import { withMockCMP } from '../helpers/mock-cmp-wrapper'

initMockConsentManager({
isOptIn: false,
consentModel: 'opt-out',
})

const analytics = new AnalyticsBrowser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AnalyticsBrowser } from '@segment/analytics-next'
import { initMockConsentManager } from '../helpers/mock-cmp'
import { withMockCMP } from '../helpers/mock-cmp-wrapper'

initMockConsentManager({ isOptIn: true })
initMockConsentManager({ consentModel: 'opt-in' })

const analytics = new AnalyticsBrowser()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const withMockCMP = (analyticsInstance: any) =>
window.MockCMP.onConsentChange(fn)
},
shouldLoadSegment: async (ctx) => {
if (!window.MockCMP.isOptIn) {
if (window.MockCMP.consentModel === 'opt-out') {
// if opt out, load immediately
return ctx.load({ optIn: false })
return ctx.load({ consentModel: 'opt-out' })
}
await window.MockCMP.waitForAlertBoxClose()
ctx.load({ optIn: true })
ctx.load({ consentModel: 'opt-in' })
},
})(analyticsInstance)
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ type ConsentChangeFn = (categories: Record<string, boolean>) => void
* Similar to OneTrust, TrustArc, etc.
* sets a global `window.MockCMP` object that can be used to interact with the mock consent manager.
*/
export const initMockConsentManager = (settings: { isOptIn: boolean }) => {
export const initMockConsentManager = (settings: { consentModel: string }) => {
const isOptIn = settings.consentModel === 'opt-in'
// if opt-in is true, all categories are set to true by default
let categories = {
FooCategory1: settings.isOptIn,
FooCategory2: settings.isOptIn,
FooCategory1: isOptIn,
FooCategory2: isOptIn,
}
console.log('initMockConsentManager', settings.isOptIn, categories)
console.log('initMockConsentManager', settings, categories)

let onConsentChange = (_categories: Record<string, boolean>) =>
undefined as void

Expand Down Expand Up @@ -50,8 +52,8 @@ export const initMockConsentManager = (settings: { isOptIn: boolean }) => {
get isLoaded() {
return loaded
},
get isOptIn() {
return settings.isOptIn
get consentModel() {
return settings.consentModel
},
setCategories: (newCategories: Record<string, boolean>) => {
categories = { ...categories, ...newCategories }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { BasePage } from './base-page'

export class ConsentToolsVanilla extends BasePage {
constructor({ optIn }: { optIn: boolean }) {
super(
optIn
? 'consent-tools-vanilla.html'
: 'consent-tools-vanilla-opt-out.html'
)
}

clickGiveConsent() {
return $('#give-consent').click()
}
clickDenyConsent() {
return $('#deny-consent').click()
}
}

export class ConsentToolsVanillaOptOut extends ConsentToolsVanilla {
constructor() {
super('consent-tools-vanilla-opt-out.html')
}
}

export class ConsentToolsVanillaOptIn extends ConsentToolsVanilla {
constructor() {
super('consent-tools-vanilla.html')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Tests targeting @segment/analytics-consent-tools
*/

import { ConsentToolsVanilla } from '../page-objects/consent-tools-vanilla'
import { ConsentToolsVanillaOptOut } from '../page-objects/consent-tools-vanilla'
import { expect } from 'expect'

const page = new ConsentToolsVanilla({ optIn: false })
const page = new ConsentToolsVanillaOptOut()

afterEach(async () => {
await page.cleanup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Tests targeting @segment/analytics-consent-tools
*/

import { ConsentToolsVanilla } from '../page-objects/consent-tools-vanilla'
import { ConsentToolsVanillaOptIn } from '../page-objects/consent-tools-vanilla'
import { expect } from 'expect'

const page = new ConsentToolsVanilla({ optIn: true })
const page = new ConsentToolsVanillaOptIn()

afterEach(async () => {
await page.cleanup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe(createWrapper, () => {
wrapTestAnalytics({
shouldLoadSegment: (ctx) => {
ctx.abort({ loadSegmentNormally: true })
ctx.load({ optIn: true })
ctx.load({ consentModel: 'opt-in' })
},
})
await expect(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const createWrapper = <Analytics extends AnyAnalytics>(

// if opt-out, we load as usual and then rely on the consent blocking middleware to block events
// if opt-in, we remove all destinations that are not explicitly consented to so they never load in the first place
if (loadCtx.loadOptions.optIn === false) {
if (loadCtx.loadOptions.consentModel === false) {
analyticsService.configureBlockingMiddlewareForOptOut()
analyticsService.load(settings, options)
return undefined
Expand Down
6 changes: 4 additions & 2 deletions packages/consent/consent-tools/src/domain/load-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export class LoadToken {
constructor(public options: LoadOptions) {}
}

export type ConsentModel = 'opt-in' | 'opt-out'

interface LoadOptions {
optIn: boolean
consentModel: ConsentModel
}

export class LoadContext {
Expand All @@ -38,7 +40,7 @@ export class LoadContext {
/**
* Opt-in consent
*/
optIn: true,
consentModel: 'opt-in',
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/consent/consent-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export { createWrapper } from './domain/create-wrapper'
export { resolveWhen } from './utils'

export type { ConsentModel } from './domain/load-context'
export type {
Wrapper,
CreateWrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ConsentModel } from '@segment/analytics-consent-tools'
import { OtConsentModel } from '../lib/onetrust-api'

export const isOptInConsentModel = (model: OtConsentModel): boolean => {
/**
* We don't support all consent models, so we need to coerce them to the ones we do support.
*/
export const coerceConsentModel = (model: OtConsentModel): ConsentModel => {
switch (model) {
case OtConsentModel.optIn:
case OtConsentModel.implicit:
return true
return 'opt-in'
case OtConsentModel.optOut:
case OtConsentModel.custom:
case OtConsentModel.notice:
return false
return 'opt-out'
default:
return false
return 'opt-out'
}
}
20 changes: 10 additions & 10 deletions packages/consent/consent-wrapper-onetrust/src/domain/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import {
getConsentedGroupIds,
getOneTrustGlobal,
} from '../lib/onetrust-api'
import { isOptInConsentModel } from './consent-model'
import { coerceConsentModel } from './consent-model'
export interface OneTrustSettings {
integrationCategoryMappings?: CreateWrapperSettings['integrationCategoryMappings']
disableConsentChangedEvent?: boolean
/**
* Override configured consent model
* - optIn = true (default) - load segment and all destinations without waiting for explicit consent.
* - optIn = false (strict/GDPR) - wait for explicit consent before loading segment
* - opt-in (default) - load segment and all destinations without waiting for explicit consent.
* - opt-out (strict/GDPR) - wait for explicit consent before loading segment
*
* By default, the value is determined by `OneTrust.GetDomainData().ConsentModel` which is set in the OneTrust UI.
*/
optIn?: () => boolean
consentModel?: () => 'opt-in' | 'opt-out'
}

/**
Expand All @@ -42,13 +42,13 @@ export const withOneTrust = <Analytics extends AnyAnalytics>(
// wait for AlertBox to be closed before segment can be loaded. If no consented groups, do not load Segment.
shouldLoadSegment: async (ctx) => {
const OneTrust = getOneTrustGlobal()!
const isOptIn =
settings.optIn ??
isOptInConsentModel(OneTrust.GetDomainData().ConsentModel.Name)
const consentModel =
settings.consentModel?.() ||
coerceConsentModel(OneTrust.GetDomainData().ConsentModel.Name)

if (!isOptIn) {
if (consentModel === 'opt-out') {
return ctx.load({
optIn: false,
consentModel: 'opt-out',
})
}
await resolveWhen(() => {
Expand All @@ -61,7 +61,7 @@ export const withOneTrust = <Analytics extends AnyAnalytics>(
OneTrust.IsAlertBoxClosed())
)
}, 500)
return ctx.load({ optIn: true })
return ctx.load({ consentModel: 'opt-in' })
},
getCategories: () => {
const results = getNormalizedCategoriesFromGroupData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
analytics.SNIPPET_VERSION = '4.13.1'
analytics._writeKey = writeKey
window['SEGMENT_CONSENT_WRAPPER_DEBUG_MODE'] = true
withOneTrust(analytics, { optIn: () => false }).load()
withOneTrust(analytics, { consentModel: () => 'opt-in' }).load()
analytics.page()
}
})()
Expand Down

0 comments on commit f505ae7

Please sign in to comment.