Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-mirrors-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-consent-wrapper-onetrust': major
---

Added the ability to remap consent keys for implementations that do not follow convention
17 changes: 17 additions & 0 deletions packages/consent/consent-wrapper-onetrust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ withOneTrust(analytics, { consentModel: () => 'opt-in' | 'opt-out' })
.load({ writeKey: '<MY_WRITE_KEY>' })
```

### Remapping Group IDs

If your OneTrust instance uses group ids that cannot be mapped in the Segment UI (e.g: `'1'`, `'2'`), you can remap them to consent category names:

```ts
withOneTrust(analytics, {
groupIdMap: {
'1': 'necessary',
'2': 'performance',
'3': 'functional',
'4': 'targeting',
'5': 'other',
},
})
.load({ writeKey: '<MY_WRITE_KEY>' })
```

## Environments

### Build Artifacts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ describe('High level "integration" tests', () => {
C0005: false,
})
})

it('should remap group ids when groupIdMap is configured', async () => {
const groupIdMap = { '1': 'necessary', '2': 'performance' }
withOneTrust(analyticsMock, { groupIdMap })
OneTrustMockGlobal.GetDomainData.mockReturnValue({
...domainDataMock,
Groups: [{ CustomGroupId: '1' }, { CustomGroupId: '2' }],
})
getNormalizedActiveGroupIds.mockImplementation(() => ['1'])
const categories = createWrapperSpyHelper.getCategories()
expect(categories).toEqual({
necessary: true,
performance: false,
})
})
})

describe('Consent changed', () => {
Expand Down Expand Up @@ -250,5 +265,33 @@ describe('High level "integration" tests', () => {
[domainGroupMock.Targeting.CustomGroupId]: false,
})
})

it('should remap group ids on consent changed when groupIdMap is configured', async () => {
const groupIdMap = { '1': 'necessary', '2': 'performance' }
withOneTrust(analyticsMock, { groupIdMap })
OneTrustMockGlobal.GetDomainData.mockReturnValue({
...domainDataMock,
Groups: [{ CustomGroupId: '1' }, { CustomGroupId: '2' }],
})
const onCategoriesChangedCb = jest.fn()

void createWrapperSpyHelper.shouldLoadWrapper()
createWrapperSpyHelper.registerOnConsentChanged(onCategoriesChangedCb)
onCategoriesChangedCb()

checkResolveWhen() // wait for OneTrust global to be available
await sleep(0)

analyticsMock.track.mockImplementationOnce(() => {}) // ignore track event sent by consent changed

const onConsentChangedArg =
OneTrustMockGlobal.OnConsentChanged.mock.lastCall![0]
onConsentChangedArg(new CustomEvent('', { detail: ['1', '2'] }))

expect(onCategoriesChangedCb.mock.lastCall[0]).toEqual({
necessary: true,
performance: true,
})
})
})
})
13 changes: 11 additions & 2 deletions packages/consent/consent-wrapper-onetrust/src/domain/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export interface OneTrustSettings {
* Enable debug logging for OneTrust wrapper
*/
enableDebugLogging?: boolean
/**
* Remap OneTrust group ids to consent category names, for cases where
* OneTrust group ids (e.g: '1', '2') cannot be mapped in the Segment UI.
* @example { '1': 'necessary', '2': 'performance', '3': 'functional', '4': 'targeting', '5': 'other' }
*/
groupIdMap?: Record<string, string>
}

/**
Expand Down Expand Up @@ -66,14 +72,17 @@ export const withOneTrust = <Analytics extends AnyAnalytics>(
}
},
getCategories: () => {
const results = getNormalizedCategories()
const results = getNormalizedCategories(undefined, settings.groupIdMap)
return results
},
registerOnConsentChanged: settings.disableConsentChangedEvent
? undefined
: (setCategories) => {
getOneTrustGlobal()!.OnConsentChanged((event) => {
const normalizedCategories = getNormalizedCategories(event.detail)
const normalizedCategories = getNormalizedCategories(
event.detail,
settings.groupIdMap
)
setCategories(normalizedCategories)
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,30 @@ describe(getNormalizedCategories, () => {
SOME_OTHER_GROUP: false,
})
})

it('should remap group ids with a groupIdMap', () => {
window.OneTrust = {
...OneTrustMockGlobal,
GetDomainData: () => ({
...domainDataMock,
Groups: ['1', '2', '3', '4', '5'].map((CustomGroupId) => ({
CustomGroupId,
})),
}),
}
const categories = getNormalizedCategories(['1', '2'], {
'1': 'necessary',
'2': 'performance',
'3': 'functional',
'4': 'targeting',
'5': 'other',
})
expect(categories).toEqual({
necessary: true,
performance: true,
functional: false,
targeting: false,
other: false,
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ export const getAllGroups = (): GroupInfo[] => {
}

export const getNormalizedCategories = (
activeGroupIds = getNormalizedActiveGroupIds()
activeGroupIds = getNormalizedActiveGroupIds(),
/**
* Rename onetrust group ids to consent category names,
* e.g: { '1': 'performance' }
*/
groupIdMap?: Record<string, string>
): Categories => {
return getAllGroups().reduce<Categories>((acc, group) => {
const groupId = groupIdMap?.[group.groupId] ?? group.groupId
return {
...acc,
[group.groupId]: activeGroupIds.includes(group.groupId),
[groupId]: activeGroupIds.includes(group.groupId),
}
}, {})
}
Expand Down