|
| 1 | +import { getGlobalScope } from '@amplitude/experiment-core'; |
| 2 | +import { |
| 3 | + ExperimentEvent, |
| 4 | + IntegrationPlugin, |
| 5 | +} from '@amplitude/experiment-js-client'; |
| 6 | + |
| 7 | +import { ConsentStatus } from '../types'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Consent-aware exposure handler that wraps window.experimentIntegration.track |
| 11 | + */ |
| 12 | +export class ConsentAwareExposureHandler { |
| 13 | + private pendingEvents: ExperimentEvent[] = []; |
| 14 | + private consentStatus: ConsentStatus = ConsentStatus.PENDING; |
| 15 | + private originalTrack: ((event: ExperimentEvent) => boolean) | null = null; |
| 16 | + |
| 17 | + constructor(initialConsentStatus: ConsentStatus) { |
| 18 | + this.consentStatus = initialConsentStatus; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Wrap the experimentIntegration.track method with consent-aware logic |
| 23 | + * Prevents nested wrapping by checking if already wrapped |
| 24 | + */ |
| 25 | + public wrapExperimentIntegrationTrack(): void { |
| 26 | + const globalScope = getGlobalScope(); |
| 27 | + const experimentIntegration = |
| 28 | + globalScope?.experimentIntegration as IntegrationPlugin; |
| 29 | + if (experimentIntegration?.track) { |
| 30 | + if (this.isTrackMethodWrapped(experimentIntegration.track)) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + this.originalTrack = experimentIntegration.track.bind( |
| 35 | + experimentIntegration, |
| 36 | + ); |
| 37 | + const wrappedTrack = this.createConsentAwareTrack(this.originalTrack); |
| 38 | + (wrappedTrack as any).__isConsentAwareWrapped = true; |
| 39 | + experimentIntegration.track = wrappedTrack; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Check if a track method is already wrapped |
| 45 | + */ |
| 46 | + private isTrackMethodWrapped( |
| 47 | + trackMethod: (event: ExperimentEvent) => boolean, |
| 48 | + ): boolean { |
| 49 | + return (trackMethod as any).__isConsentAwareWrapped === true; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a consent-aware wrapper for the track method |
| 54 | + */ |
| 55 | + private createConsentAwareTrack( |
| 56 | + originalTrack: (event: ExperimentEvent) => boolean, |
| 57 | + ) { |
| 58 | + return (event: ExperimentEvent): boolean => { |
| 59 | + if (event?.eventProperties) { |
| 60 | + event.eventProperties.time = Date.now(); |
| 61 | + } |
| 62 | + try { |
| 63 | + if (this.consentStatus === ConsentStatus.PENDING) { |
| 64 | + this.pendingEvents.push(event); |
| 65 | + return true; |
| 66 | + } else if (this.consentStatus === ConsentStatus.GRANTED) { |
| 67 | + return originalTrack(event); |
| 68 | + } |
| 69 | + return false; |
| 70 | + } catch (error) { |
| 71 | + console.warn('Failed to track event:', error); |
| 72 | + return false; |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set the consent status and handle pending events accordingly |
| 79 | + */ |
| 80 | + public setConsentStatus(consentStatus: ConsentStatus): void { |
| 81 | + const previousStatus = this.consentStatus; |
| 82 | + this.consentStatus = consentStatus; |
| 83 | + |
| 84 | + if (previousStatus === ConsentStatus.PENDING) { |
| 85 | + if (consentStatus === ConsentStatus.GRANTED) { |
| 86 | + for (const event of this.pendingEvents) { |
| 87 | + if (this.originalTrack) { |
| 88 | + try { |
| 89 | + this.originalTrack(event); |
| 90 | + } catch (error) { |
| 91 | + console.warn('Failed to track pending event:', error); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + this.pendingEvents = []; |
| 96 | + } else if (consentStatus === ConsentStatus.REJECTED) { |
| 97 | + // Delete all pending events |
| 98 | + this.pendingEvents = []; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments