@@ -14,7 +14,7 @@ import type { InstanceType } from '@clerk/types';
14
14
15
15
import { parsePublishableKey } from '../keys' ;
16
16
import { isTruthy } from '../underscore' ;
17
- import type { TelemetryCollectorOptions , TelemetryEvent } from './types' ;
17
+ import type { TelemetryCollectorOptions , TelemetryEvent , TelemetryEventRaw } from './types' ;
18
18
19
19
type TelemetryCollectorConfig = Pick <
20
20
TelemetryCollectorOptions ,
@@ -105,12 +105,12 @@ export class TelemetryCollector {
105
105
return this . #config. debug || ( typeof process !== 'undefined' && isTruthy ( process . env . CLERK_TELEMETRY_DEBUG ) ) ;
106
106
}
107
107
108
- record ( event : Pick < TelemetryEvent , 'event' | 'payload' > ) : void {
108
+ record ( event : TelemetryEventRaw ) : void {
109
109
const preparedPayload = this . #preparePayload( event . event , event . payload ) ;
110
110
111
111
this . #logEvent( preparedPayload . event , preparedPayload ) ;
112
112
113
- if ( ! this . #shouldRecord( ) ) {
113
+ if ( ! this . #shouldRecord( event . eventSamplingRate ) ) {
114
114
return ;
115
115
}
116
116
@@ -119,8 +119,13 @@ export class TelemetryCollector {
119
119
this . #scheduleFlush( ) ;
120
120
}
121
121
122
- #shouldRecord( ) : boolean {
123
- return this . isEnabled && ! this . isDebug && Math . random ( ) <= this . #config. samplingRate ;
122
+ #shouldRecord( eventSamplingRate ?: number ) : boolean {
123
+ const randomSeed = Math . random ( ) ;
124
+ const shouldBeSampled =
125
+ randomSeed <= this . #config. samplingRate &&
126
+ ( typeof eventSamplingRate === 'undefined' || randomSeed <= eventSamplingRate ) ;
127
+
128
+ return this . isEnabled && ! this . isDebug && shouldBeSampled ;
124
129
}
125
130
126
131
#scheduleFlush( ) : void {
0 commit comments