Skip to content

Commit

Permalink
[docs/test] Tweak consent tests / update docs (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Sep 20, 2023
1 parent cb56224 commit b49ed5f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export abstract class BasePage {
{
creationName: 'FullStory',
consentSettings: {
categories: ['Analytics'],
categories: ['FooCategory2'],
},
},
{
creationName: 'Actions Amplitude',
consentSettings: {
categories: ['Advertising'],
categories: ['FooCategory1'],
},
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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,
},
})
})
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
32 changes: 23 additions & 9 deletions packages/consent/consent-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,9 +54,7 @@ import { AnalyticsBrowser } from '@segment/analytics-next'

export const analytics = new AnalyticsBrowser()

withCMP(analytics)

analytics.load({
withCMP(analytics).load({
writeKey: '<MY_WRITE_KEY'>
})

Expand All @@ -58,9 +74,7 @@ analytics.load({
```js
import { withCMP } from './wrapper'

withCMP(window.analytics)

window.analytics.load('<MY_WRITE_KEY')
withCMP(window.analytics).load('<MY_WRITE_KEY')
```

## Wrapper Examples
Expand Down
2 changes: 1 addition & 1 deletion packages/consent/consent-wrapper-onetrust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ withOneTrust(analytics).load({ writeKey: '<MY_WRITE_KEY'> })
></script>

<!-- Add OneTrust Consent Wrapper -->
<script src="https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@^0.2.0/dist/umd/analytics-onetrust.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@latest/dist/umd/analytics-onetrust.umd.js"></script>

<!--
Add / Modify Segment Analytics Snippet
Expand Down

0 comments on commit b49ed5f

Please sign in to comment.