-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add consent management and support for onetrust cmp #882
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { | ||
Plugin, | ||
type SegmentClient, | ||
type DestinationPlugin, | ||
IntegrationSettings, | ||
PluginType, | ||
SegmentAPIIntegration, | ||
SegmentEvent, | ||
TrackEventType, | ||
} from '..'; | ||
|
||
import { SEGMENT_DESTINATION_KEY } from './SegmentDestination'; | ||
|
||
const CONSENT_PREF_UPDATE_EVENT = 'Segment Consent Preference'; | ||
|
||
export interface CategoryConsentStatusProvider { | ||
setApplicableCategories(categories: string[]): void; | ||
getConsentStatus(): Promise<Record<string, boolean>>; | ||
onConsentChange(cb: (updConsent: Record<string, boolean>) => void): void; | ||
shutdown?(): void; | ||
} | ||
|
||
/** | ||
* This plugin interfaces with the consent provider and it: | ||
* | ||
* - stamps all events with the consent metadata. | ||
* - augments all destinations with a consent filter plugin that prevents events from reaching them if | ||
* they are not compliant current consent setup | ||
* - listens for consent change from the provider and notifies Segment | ||
*/ | ||
export class ConsentPlugin extends Plugin { | ||
type = PluginType.before; | ||
|
||
constructor( | ||
private consentCategoryProvider: CategoryConsentStatusProvider, | ||
private categories: string[] | ||
) { | ||
super(); | ||
} | ||
|
||
configure(analytics: SegmentClient): void { | ||
super.configure(analytics); | ||
analytics.getPlugins().forEach(this.injectConsentFilterIfApplicable); | ||
zikaari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
analytics.onPluginLoaded(this.injectConsentFilterIfApplicable); | ||
this.consentCategoryProvider.setApplicableCategories(this.categories); | ||
this.consentCategoryProvider.onConsentChange((categoryPreferences) => { | ||
this.analytics | ||
?.track(CONSENT_PREF_UPDATE_EVENT, { | ||
consent: { | ||
categoryPreferences, | ||
}, | ||
}) | ||
.catch((e) => { | ||
throw e; | ||
}); | ||
}); | ||
} | ||
|
||
async execute(event: SegmentEvent): Promise<SegmentEvent> { | ||
if ((event as TrackEventType).event === CONSENT_PREF_UPDATE_EVENT) { | ||
return event; | ||
} | ||
|
||
event.context = { | ||
...event.context, | ||
consent: { | ||
categoryPreferences: | ||
await this.consentCategoryProvider.getConsentStatus(), | ||
}, | ||
}; | ||
|
||
return event; | ||
} | ||
|
||
shutdown(): void { | ||
this.consentCategoryProvider.shutdown?.(); | ||
} | ||
|
||
private injectConsentFilterIfApplicable = (plugin: Plugin) => { | ||
if ( | ||
this.isDestinationPlugin(plugin) && | ||
plugin.key !== SEGMENT_DESTINATION_KEY | ||
) { | ||
const settings = this.analytics?.settings.get()?.[plugin.key]; | ||
|
||
plugin.add( | ||
new ConsentFilterPlugin( | ||
this.containsConsentSettings(settings) | ||
? settings.consentSettings.categories | ||
: [] | ||
) | ||
); | ||
} | ||
}; | ||
|
||
private isDestinationPlugin(plugin: Plugin): plugin is DestinationPlugin { | ||
return plugin.type === PluginType.destination; | ||
} | ||
|
||
private containsConsentSettings = ( | ||
settings: IntegrationSettings | undefined | ||
): settings is Required<Pick<SegmentAPIIntegration, 'consentSettings'>> => { | ||
return ( | ||
typeof (settings as SegmentAPIIntegration)?.consentSettings | ||
?.categories === 'object' | ||
); | ||
}; | ||
} | ||
|
||
/** | ||
* This plugin reads the consent metadata set on the context object and then drops the events | ||
* if they are going into a destination which violates's set consent preferences | ||
*/ | ||
class ConsentFilterPlugin extends Plugin { | ||
type = PluginType.before; | ||
|
||
constructor(private categories: string[]) { | ||
super(); | ||
} | ||
|
||
execute(event: SegmentEvent): SegmentEvent | undefined { | ||
const preferences = event.context?.consent?.categoryPreferences; | ||
|
||
// if consent plugin is active but the setup isn't properly configured - events are blocked by default | ||
if (!preferences || this.categories.length === 0) { | ||
return undefined; | ||
} | ||
|
||
// all categories this destination is tagged with must be present, and allowed in consent preferences | ||
return this.categories.every((category) => preferences?.[category]) | ||
? event | ||
: undefined; | ||
zikaari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Segment | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# @segment/analytics-react-native-plugin-onetrust | ||
|
||
Plugin for adding support for [OneTrust](https://onetrust.com/) CMP to your React Native application. | ||
|
||
## Installation | ||
|
||
You will need to install the `@segment/analytics-react-native-plugin-onetrust` package as a dependency in your project: | ||
|
||
Using NPM: | ||
|
||
```bash | ||
npm install --save @segment/analytics-react-native-plugin-onetrust react-native-onetrust-cmp | ||
``` | ||
|
||
Using Yarn: | ||
|
||
```bash | ||
yarn add @segment/analytics-react-native-plugin-onetrust react-native-onetrust-cmp | ||
``` | ||
|
||
## Usage | ||
|
||
Follow the [instructions for adding plugins](https://github.com/segmentio/analytics-react-native#adding-plugins) on the main Analytics client: | ||
|
||
After you create your segment client add `OneTrustPlugin` as a plugin, order doesn't matter, this plugin will apply to all device mode destinations you add before and after this plugin is added: | ||
|
||
```ts | ||
import { createClient } from '@segment/analytics-react-native'; | ||
import { OneTrustPlugin } from '@segment/analytics-react-native-plugin-onetrust'; | ||
import OTPublishersNativeSDK from 'react-native-onetrust-cmp'; | ||
|
||
const segment = createClient({ | ||
writeKey: 'SEGMENT_KEY', | ||
}); | ||
|
||
segment.add({ | ||
plugin: new OneTrust(OTPublishersNativeSDK, ['C001', 'C002', '...']), | ||
}); | ||
|
||
// device mode destinations | ||
segment.add({ plugin: new BrazePlugin() }); | ||
``` | ||
|
||
## Support | ||
|
||
Please use Github issues, Pull Requests, or feel free to reach out to our [support team](https://segment.com/help/). | ||
|
||
## Integrating with Segment | ||
|
||
Interested in integrating your service with us? Check out our [Partners page](https://segment.com/partners/) for more details. | ||
|
||
## License | ||
|
||
``` | ||
MIT License | ||
|
||
Copyright (c) 2021 Segment | ||
zikaari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: ['module:metro-react-native-babel-preset'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
const { compilerOptions } = require('./tsconfig'); | ||
|
||
module.exports = { | ||
preset: 'react-native', | ||
roots: ['<rootDir>'], | ||
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'], | ||
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'], | ||
modulePathIgnorePatterns: ['/lib/'], | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
modulePaths: [compilerOptions.baseUrl], | ||
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't know about this helper! |
||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about this syntax!