-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Cookie consent exposure handling #222
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
347d32e
add consent-dependent exposure handler
tyiuhc b2bce1b
merge
tyiuhc 1fd155a
Merge branch 'web/cookie-consent' into web/cookie-consent-exposure
tyiuhc ae74942
fix import
tyiuhc 159ff20
move exposure handler
tyiuhc 06186e4
Merge branch 'web/cookie-consent' into web/cookie-consent-exposure
tyiuhc 394f169
fix lint
tyiuhc 9034cde
add test cases
tyiuhc c55b91b
fix: attach timestamp to impression events (#223)
tyiuhc f8b2a89
add timestamp tests
tyiuhc fd84544
simplify comments
tyiuhc cc414ba
wrap integrationplugin
tyiuhc 636cafb
update tests
tyiuhc fb46694
check track wrapping, finx lint
tyiuhc 18adb44
fix comment
tyiuhc 78be137
fix init
tyiuhc 40c6cd1
nit: formatting
tyiuhc 9b09523
simplify
tyiuhc 4050b46
use date.now
tyiuhc 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 hidden or 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 hidden or 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
102 changes: 102 additions & 0 deletions
102
packages/experiment-tag/src/exposure/consent-aware-exposure-handler.ts
This file contains hidden or 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,102 @@ | ||
| import { getGlobalScope } from '@amplitude/experiment-core'; | ||
| import { | ||
| ExperimentEvent, | ||
| IntegrationPlugin, | ||
| } from '@amplitude/experiment-js-client'; | ||
|
|
||
| import { ConsentStatus } from '../types'; | ||
|
|
||
| /** | ||
| * Consent-aware exposure handler that wraps window.experimentIntegration.track | ||
| */ | ||
| export class ConsentAwareExposureHandler { | ||
| private pendingEvents: ExperimentEvent[] = []; | ||
| private consentStatus: ConsentStatus = ConsentStatus.PENDING; | ||
| private originalTrack: ((event: ExperimentEvent) => boolean) | null = null; | ||
|
|
||
| constructor(initialConsentStatus: ConsentStatus) { | ||
| this.consentStatus = initialConsentStatus; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap the experimentIntegration.track method with consent-aware logic | ||
| * Prevents nested wrapping by checking if already wrapped | ||
| */ | ||
| public wrapExperimentIntegrationTrack(): void { | ||
| const globalScope = getGlobalScope(); | ||
| const experimentIntegration = | ||
| globalScope?.experimentIntegration as IntegrationPlugin; | ||
| if (experimentIntegration?.track) { | ||
| if (this.isTrackMethodWrapped(experimentIntegration.track)) { | ||
| return; | ||
| } | ||
|
|
||
| this.originalTrack = experimentIntegration.track.bind( | ||
| experimentIntegration, | ||
| ); | ||
| const wrappedTrack = this.createConsentAwareTrack(this.originalTrack); | ||
| (wrappedTrack as any).__isConsentAwareWrapped = true; | ||
| experimentIntegration.track = wrappedTrack; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if a track method is already wrapped | ||
| */ | ||
| private isTrackMethodWrapped( | ||
| trackMethod: (event: ExperimentEvent) => boolean, | ||
| ): boolean { | ||
| return (trackMethod as any).__isConsentAwareWrapped === true; | ||
| } | ||
|
|
||
| /** | ||
| * Create a consent-aware wrapper for the track method | ||
| */ | ||
| private createConsentAwareTrack( | ||
| originalTrack: (event: ExperimentEvent) => boolean, | ||
| ) { | ||
| return (event: ExperimentEvent): boolean => { | ||
| if (event?.eventProperties) { | ||
| event.eventProperties.time = Date.now(); | ||
| } | ||
| try { | ||
| if (this.consentStatus === ConsentStatus.PENDING) { | ||
| this.pendingEvents.push(event); | ||
| return true; | ||
| } else if (this.consentStatus === ConsentStatus.GRANTED) { | ||
| return originalTrack(event); | ||
| } | ||
| return false; | ||
| } catch (error) { | ||
| console.warn('Failed to track event:', error); | ||
| return false; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Set the consent status and handle pending events accordingly | ||
| */ | ||
| public setConsentStatus(consentStatus: ConsentStatus): void { | ||
| const previousStatus = this.consentStatus; | ||
| this.consentStatus = consentStatus; | ||
|
|
||
| if (previousStatus === ConsentStatus.PENDING) { | ||
| if (consentStatus === ConsentStatus.GRANTED) { | ||
| for (const event of this.pendingEvents) { | ||
| if (this.originalTrack) { | ||
| try { | ||
| this.originalTrack(event); | ||
| } catch (error) { | ||
| console.warn('Failed to track pending event:', error); | ||
| } | ||
| } | ||
| } | ||
| this.pendingEvents = []; | ||
| } else if (consentStatus === ConsentStatus.REJECTED) { | ||
| // Delete all pending events | ||
| this.pendingEvents = []; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.