Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(telemetry): dedupe experiment exposure events by PostHog session ID (#42386)

Pamela Chia30093962d4d5
diff --git a/apps/studio/hooks/misc/useRealtimeExperiment.ts b/apps/studio/hooks/misc/useRealtimeExperiment.ts
index bf7740989d7..34ed3729fbc 100644
--- a/apps/studio/hooks/misc/useRealtimeExperiment.ts
+++ b/apps/studio/hooks/misc/useRealtimeExperiment.ts
@@ -1,8 +1,7 @@
-import { useEffect, useMemo, useRef } from 'react'
-
+import { useTrackExperimentExposure } from 'hooks/misc/useTrackExperimentExposure'
import { usePHFlag } from 'hooks/ui/useFlag'
import { IS_PLATFORM } from 'lib/constants'
-import { useTrack } from 'lib/telemetry/track'
+import { useMemo } from 'react'
export enum RealtimeButtonVariant {
CONTROL = 'control',
@@ -30,9 +29,7 @@ export function useRealtimeExperiment({
isTable = false,
isRealtimeEnabled = false,
}: UseRealtimeExperimentOptions): UseRealtimeExperimentResult {
- const track = useTrack()
const realtimeButtonVariant = usePHFlag<RealtimeButtonVariant>('realtimeButtonVariant')
- const hasTrackedExposure = useRef(false)
const activeVariant = useMemo(() => {
if (!IS_PLATFORM) return null
@@ -42,25 +39,11 @@ export function useRealtimeExperiment({
return realtimeButtonVariant
}, [isTable, realtimeButtonVariant])
- useEffect(() => {
- if (!IS_PLATFORM) return
- if (hasTrackedExposure.current) return
- if (!isTable || !realtimeButtonVariant) return
-
- hasTrackedExposure.current = true
+ const shouldTrack = IS_PLATFORM && isTable && !!realtimeButtonVariant
- try {
- track('realtime_experiment_exposed', {
- experiment_id: 'realtimeButtonVariant',
- variant: realtimeButtonVariant,
- table_has_realtime_enabled: isRealtimeEnabled,
- })
- } catch (error) {
- // Reset tracking flag on error to allow retry
- hasTrackedExposure.current = false
- console.error('Failed to track realtime experiment exposure:', error)
- }
- }, [isTable, realtimeButtonVariant, isRealtimeEnabled, track])
+ useTrackExperimentExposure('realtime', shouldTrack ? realtimeButtonVariant : undefined, {
+ table_has_realtime_enabled: isRealtimeEnabled,
+ })
return {
activeVariant,
diff --git a/apps/studio/hooks/misc/useTableCreateGeneratePolicies.ts b/apps/studio/hooks/misc/useTableCreateGeneratePolicies.ts
index ca217aff132..fe13743b1d9 100644
--- a/apps/studio/hooks/misc/useTableCreateGeneratePolicies.ts
+++ b/apps/studio/hooks/misc/useTableCreateGeneratePolicies.ts
@@ -1,10 +1,9 @@
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
-import { useEffect, useMemo, useRef } from 'react'
-
+import { useTrackExperimentExposure } from 'hooks/misc/useTrackExperimentExposure'
import { usePHFlag } from 'hooks/ui/useFlag'
import { IS_PLATFORM } from 'lib/constants'
-import { useTrack } from 'lib/telemetry/track'
+import { useMemo } from 'react'
dayjs.extend(utc)
@@ -50,9 +49,7 @@ export function useTableCreateGeneratePolicies({
isNewRecord = false,
projectInsertedAt,
}: UseTableCreateGeneratePoliciesOptions): UseTableCreateGeneratePoliciesResult {
- const track = useTrack()
const tableCreateGeneratePoliciesFlag = usePHFlag<string>('tableCreateGeneratePolicies')
- const hasTrackedExposure = useRef(false)
const enabled = useMemo(() => {
if (!IS_PLATFORM) return false
@@ -60,28 +57,24 @@ export function useTableCreateGeneratePolicies({
return true
}, [tableCreateGeneratePoliciesFlag])
- useEffect(() => {
- if (!IS_PLATFORM) return
- if (hasTrackedExposure.current) return
- if (!isNewRecord) return
- if (!isValidExperimentVariant(tableCreateGeneratePoliciesFlag)) return
- if (!projectInsertedAt) return
+ const daysSinceCreation = useMemo(() => {
+ if (!projectInsertedAt) return undefined
+ const insertedDate = dayjs.utc(projectInsertedAt)
+ if (!insertedDate.isValid()) return undefined
+ return dayjs.utc().diff(insertedDate, 'day')
+ }, [projectInsertedAt])
- try {
- const insertedDate = dayjs.utc(projectInsertedAt)
- if (!insertedDate.isValid()) return
+ const shouldTrack =
+ IS_PLATFORM &&
+ isNewRecord &&
+ isValidExperimentVariant(tableCreateGeneratePoliciesFlag) &&
+ daysSinceCreation !== undefined
- const daysSinceCreation = dayjs.utc().diff(insertedDate, 'day')
- track('table_create_generate_policies_experiment_exposed', {
- experiment_id: 'tableCreateGeneratePolicies',
- variant: tableCreateGeneratePoliciesFlag,
- days_since_project_creation: daysSinceCreation,
- })
- hasTrackedExposure.current = true
- } catch {
- hasTrackedExposure.current = false
- }
- }, [isNewRecord, tableCreateGeneratePoliciesFlag, projectInsertedAt, track])
+ useTrackExperimentExposure(
+ 'table_create_generate_policies',
+ shouldTrack ? tableCreateGeneratePoliciesFlag : undefined,
+ { days_since_project_creation: daysSinceCreation }
+ )
return {
enabled,
diff --git a/apps/studio/hooks/misc/useTrackExperimentExposure.ts b/apps/studio/hooks/misc/useTrackExperimentExposure.ts
new file mode 100644
index 00000000000..5b1f491137d
--- /dev/null
+++ b/apps/studio/hooks/misc/useTrackExperimentExposure.ts
@@ -0,0 +1,18 @@
+import { hasConsented, posthogClient } from 'common'
+import { useEffect } from 'react'
+
+export function useTrackExperimentExposure(
+ experimentId: string,
+ variant: string | undefined,
+ extraProperties?: Record<string, any>
+) {
+ useEffect(() => {
+ if (!variant) return
+
+ posthogClient.captureExperimentExposure(
+ experimentId,
+ { variant, ...extraProperties },
+ hasConsented()
+ )
+ }, [experimentId, variant])
+}
diff --git a/apps/studio/pages/new/[slug].tsx b/apps/studio/pages/new/[slug].tsx
index dab30244317..dccca6c1f8e 100644
--- a/apps/studio/pages/new/[slug].tsx
+++ b/apps/studio/pages/new/[slug].tsx
@@ -44,6 +44,7 @@ import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
+import { useTrackExperimentExposure } from 'hooks/misc/useTrackExperimentExposure'
import { withAuth } from 'hooks/misc/withAuth'
import { usePHFlag } from 'hooks/ui/useFlag'
import { DOCS_URL, PROJECT_STATUS, PROVIDERS, useDefaultProvider } from 'lib/constants'
@@ -51,7 +52,7 @@ import { useProfile } from 'lib/profile'
import { useTrack } from 'lib/telemetry/track'
import Link from 'next/link'
import { useRouter } from 'next/router'
-import { PropsWithChildren, useEffect, useMemo, useRef, useState } from 'react'
+import { PropsWithChildren, useEffect, useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import { AWS_REGIONS, type CloudProvider } from 'shared-data'
import { toast } from 'sonner'
@@ -93,7 +94,6 @@ const Wizard: NextPageWithLayout = () => {
const showNonProdFields = process.env.NEXT_PUBLIC_ENVIRONMENT !== 'prod'
const isNotOnHigherPlan = !['team', 'enterprise', 'platform'].includes(currentOrg?.plan.id ?? '')
- const hasTrackedRlsExposure = useRef(false)
// This is to make the database.new redirect work correctly. The database.new redirect should be set to supabase.com/dashboard/new/last-visited-org
if (slug === 'last-visited-org') {
@@ -390,18 +390,13 @@ const Wizard: NextPageWithLayout = () => {
}, [instanceSize, watchedInstanceSize, form])
// Track exposure to RLS option experiment (only when explicitly assigned to a variant)
- useEffect(() => {
- if (
- !hasTrackedRlsExposure.current &&
- currentOrg?.slug &&
- (rlsExperimentVariant === 'control' || rlsExperimentVariant === 'test')
- ) {
- hasTrackedRlsExposure.current = true
- track('project_creation_rls_option_experiment_exposed', {
- variant: rlsExperimentVariant,
- })
- }
- }, [currentOrg?.slug, rlsExperimentVariant, track])
+ const shouldTrackRlsExposure =
+ !!currentOrg?.slug && (rlsExperimentVariant === 'control' || rlsExperimentVariant === 'test')
+
+ useTrackExperimentExposure(
+ 'project_creation_rls_option',
+ shouldTrackRlsExposure ? rlsExperimentVariant : undefined
+ )
return (
<Form_Shadcn_ {...form}>
diff --git a/packages/common/posthog-client.ts b/packages/common/posthog-client.ts
index 981e87bf0eb..0c058984e64 100644
--- a/packages/common/posthog-client.ts
+++ b/packages/common/posthog-client.ts
@@ -19,6 +19,7 @@ class PostHogClient {
private pendingGroups: Record<string, string> = {}
private pendingIdentification: { userId: string; properties?: Record<string, any> } | null = null
private pendingEvents: Array<{ event: string; properties: Record<string, any> }> = []
+ private pendingExposures: Array<{ experimentId: string; properties: Record<string, any> }> = []
private config: PostHogClientConfig
private readonly maxPendingEvents = MAX_PENDING_EVENTS
@@ -84,6 +85,12 @@ class PostHogClient {
this.pendingEvents = []
this.initialized = true
+
+ // Flush any pending experiment exposures (with deduplication)
+ this.pendingExposures.forEach(({ experimentId, properties }) => {
+ this.fireExposureIfNew(experimentId, properties)
+ })
+ this.pendingExposures = []
},
}
@@ -159,6 +166,7 @@ class PostHogClient {
this.pendingIdentification = null
this.pendingGroups = {}
this.pendingEvents = []
+ this.pendingExposures = []
if (!this.initStarted) return
@@ -183,6 +191,63 @@ class PostHogClient {
return undefined
}
}
+
+ /**
+ * Returns PostHog's session_id for the current session.
+ * Returns undefined until PostHog's `loaded` callback fires.
+ */
+ getSessionId(): string | undefined {
+ if (!this.initialized) return undefined
+
+ try {
+ return posthog.get_session_id()
+ } catch (error) {
+ console.error('PostHog getSessionId failed:', error)
+ return undefined
+ }
+ }
+
+ /**
+ * Captures an experiment exposure event with session-based deduplication.
+ * Events are queued if PostHog is not yet initialized, then deduped on flush.
+ */
+ captureExperimentExposure(
+ experimentId: string,
+ properties: Record<string, any>,
+ hasConsent: boolean = true
+ ) {
+ if (!hasConsent) return
+
+ if (!this.initialized) {
+ // Only queue if not already queued for this experiment (first exposure wins)
+ if (!this.pendingExposures.some((e) => e.experimentId === experimentId)) {
+ if (this.pendingExposures.length >= this.maxPendingEvents) {
+ this.pendingExposures.shift()
+ }
+ this.pendingExposures.push({ experimentId, properties })
+ }
+ return
+ }
+
+ this.fireExposureIfNew(experimentId, properties)
+ }
+
+ private fireExposureIfNew(experimentId: string, properties: Record<string, any>) {
+ const sessionId = this.getSessionId()
+ if (!sessionId) return
+
+ const storageKey = `ph_exposed:${experimentId}`
+
+ try {
+ if (sessionStorage.getItem(storageKey) === sessionId) return
+
+ const eventName = `${experimentId}_experiment_exposed`
+ posthog.capture(eventName, { experiment_id: experimentId, ...properties })
+ sessionStorage.setItem(storageKey, sessionId)
+ } catch (error) {
+ console.error('PostHog experiment exposure capture failed:', error)
+ }
+ }
}
export const posthogClient = new PostHogClient()