diff --git a/packages/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts b/packages/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts index 5e56c29b3..f04602e81 100644 --- a/packages/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts +++ b/packages/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts @@ -1,14 +1,10 @@ import { AnalyticsBrowser } from '@segment/analytics-next' import { createWrapper } from '@segment/analytics-consent-tools' -const fakeCategories = { Advertising: true, Analytics: true } +const fakeCategories = { FooCategory1: true, FooCategory2: true } const withCMP = createWrapper({ getCategories: () => fakeCategories, - integrationCategoryMappings: { - Fullstory: ['Analytics'], - 'Actions Amplitude': ['Advertising'], - }, }) const analytics = new AnalyticsBrowser() diff --git a/packages/consent/consent-tools-integration-tests/src/page-objects/base-page.ts b/packages/consent/consent-tools-integration-tests/src/page-objects/base-page.ts index 569028cce..0da5da67d 100644 --- a/packages/consent/consent-tools-integration-tests/src/page-objects/base-page.ts +++ b/packages/consent/consent-tools-integration-tests/src/page-objects/base-page.ts @@ -36,13 +36,13 @@ export abstract class BasePage { { creationName: 'FullStory', consentSettings: { - categories: ['Analytics'], + categories: ['FooCategory2'], }, }, { creationName: 'Actions Amplitude', consentSettings: { - categories: ['Advertising'], + categories: ['FooCategory1'], }, } ) diff --git a/packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla.test.ts b/packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla.test.ts index 62c9c1154..2043ecdf0 100644 --- a/packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla.test.ts +++ b/packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla.test.ts @@ -1,7 +1,10 @@ +/** + * Tests targeting @segment/analytics-consent-tools + */ + import page from '../page-objects/consent-tools-vanilla' import { expect } from 'expect' -// Verify that the consent tools wrapper is working as expected (no OneTrust) it('should stamp each event', async () => { await page.load() @@ -11,8 +14,8 @@ it('should stamp each event', async () => { expect((ctx.event.context as any).consent).toEqual({ categoryPreferences: { - Advertising: true, - Analytics: true, + FooCategory1: true, + FooCategory2: true, }, }) }) diff --git a/packages/consent/consent-tools-integration-tests/src/tests/onetrust.test.ts b/packages/consent/consent-tools-integration-tests/src/tests/onetrust.test.ts index 40b73aa76..bc8d2227c 100644 --- a/packages/consent/consent-tools-integration-tests/src/tests/onetrust.test.ts +++ b/packages/consent/consent-tools-integration-tests/src/tests/onetrust.test.ts @@ -1,6 +1,7 @@ -// Verify that @segment/analytics-consent-wrapper-onetrust is working as expected +/** + * Tests targeting @segment/analytics-consent-wrapper-onetrust + */ -/* eslint-disable @typescript-eslint/no-floating-promises */ import page from '../page-objects/onetrust' import { expect } from 'expect' diff --git a/packages/consent/consent-tools/README.md b/packages/consent/consent-tools/README.md index a9fa8ac3d..7c851378c 100644 --- a/packages/consent/consent-tools/README.md +++ b/packages/consent/consent-tools/README.md @@ -7,23 +7,41 @@ import { createWrapper, resolveWhen } from '@segment/analytics-consent-tools' export const withCMP = createWrapper({ + + // Do not attempt to load segment until this function returns / resolves shouldLoad: (ctx) => { + const CMP = await getCMP() await resolveWhen( - () => window.CMP !== undefined && !window.CMP.popUpVisible(), + () => !CMP.popUpVisible(), 500 ) + // Optional -- for granular control of initialization if (noConsentNeeded) { ctx.abort({ loadSegmentNormally: true }) } else if (allTrackingDisabled) { ctx.abort({ loadSegmentNormally: false }) } }, + getCategories: () => { - // e.g. { Advertising: true, Functional: false } - return normalizeCategories(window.CMP.consentedCategories()) + const CMP = await getCMP() + return normalizeCategories(CMP.consentedCategories()) // Expected format: { foo: true, bar: false } + }, + + registerOnConsentChanged: (setCategories) => { + const CMP = await getCMP() + CMP.onConsentChanged((event) => { + setCategories(normalizeCategories(event.detail)) + }) }, }) + + +const getCMP = async () => { + await resolveWhen(() => window.CMP !== undefined, 500) + return window.CMP +} ``` ## Wrapper Usage API @@ -36,9 +54,7 @@ import { AnalyticsBrowser } from '@segment/analytics-next' export const analytics = new AnalyticsBrowser() -withCMP(analytics) - -analytics.load({ +withCMP(analytics).load({ writeKey: ' }) @@ -58,9 +74,7 @@ analytics.load({ ```js import { withCMP } from './wrapper' -withCMP(window.analytics) - -window.analytics.load(' }) > - +