From b4d35a46dba9f25cdb39eb496e26bdf1d23f6bfb Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 8 Oct 2024 09:43:32 -0600 Subject: [PATCH] use sample rate --- packages/browser/src/browser/index.ts | 9 +++++++++ packages/signals/signals/src/core/signals/signals.ts | 6 ++++++ packages/signals/signals/src/types/analytics-api.ts | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/packages/browser/src/browser/index.ts b/packages/browser/src/browser/index.ts index c9d1927ec..603f80a31 100644 --- a/packages/browser/src/browser/index.ts +++ b/packages/browser/src/browser/index.ts @@ -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 { diff --git a/packages/signals/signals/src/core/signals/signals.ts b/packages/signals/signals/src/core/signals/signals.ts index 634210604..7409b2c3b 100644 --- a/packages/signals/signals/src/core/signals/signals.ts +++ b/packages/signals/signals/src/core/signals/signals.ts @@ -83,6 +83,12 @@ export class Signals implements ISignals { * - Registers custom signal generators. */ async start(analytics: AnyAnalytics): Promise { + if ( + !analytics.settings.cdnSettings.autoInstrumentationSettings || + Math.random() > + analytics.settings.cdnSettings.autoInstrumentationSettings.sampleRate + ) + return const analyticsService = new AnalyticsService(analytics) this.globalSettings.update({ diff --git a/packages/signals/signals/src/types/analytics-api.ts b/packages/signals/signals/src/types/analytics-api.ts index cc84982ce..9dcde28b4 100644 --- a/packages/signals/signals/src/types/analytics-api.ts +++ b/packages/signals/signals/src/types/analytics-api.ts @@ -9,6 +9,7 @@ export type EdgeFnCDNSettings = { } export interface CDNSettings { + autoInstrumentationSettings?: CDNSettingsAutoInstrumentation integrations: CDNSettingsIntegrations edgeFunction?: EdgeFnCDNSettings | { [key: string]: never } } @@ -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> {