Skip to content

Commit

Permalink
use sample rate
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljackins committed Oct 8, 2024
1 parent 58ca71a commit b4d35a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ export interface CDNSettings {
version: number
}
| {}
/**
* Settings for auto instrumentation
*/
autoInstrumentationSettings?: {
/**
* A sample rate to determine what percentage of sessions should send signals, from [0,1]
*/
sampleRate: number
}
}

export interface AnalyticsBrowserSettings {
Expand Down
6 changes: 6 additions & 0 deletions packages/signals/signals/src/core/signals/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export class Signals implements ISignals {
* - Registers custom signal generators.
*/
async start(analytics: AnyAnalytics): Promise<void> {
if (
!analytics.settings.cdnSettings.autoInstrumentationSettings ||
Math.random() >
analytics.settings.cdnSettings.autoInstrumentationSettings.sampleRate
)
return
const analyticsService = new AnalyticsService(analytics)

this.globalSettings.update({
Expand Down
5 changes: 5 additions & 0 deletions packages/signals/signals/src/types/analytics-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type EdgeFnCDNSettings = {
}

export interface CDNSettings {
autoInstrumentationSettings?: CDNSettingsAutoInstrumentation
integrations: CDNSettingsIntegrations
edgeFunction?: EdgeFnCDNSettings | { [key: string]: never }
}
Expand Down Expand Up @@ -72,6 +73,10 @@ export interface CDNSettingsIntegrations {
[integrationName: string]: { [key: string]: any }
}

export interface CDNSettingsAutoInstrumentation {
sampleRate: number
}

export type PluginType = 'before' | 'after' | 'destination'

export interface Plugin<Analytics = AnyAnalytics, Ctx = Record<string, any>> {
Expand Down

0 comments on commit b4d35a4

Please sign in to comment.