Skip to content

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

feat(docs): record search v2 rollout exposure (#51300)

Pamela Chia20d752517ff2
diff --git a/apps/docs/features/SearchV2/useSearchV2Variant.test.ts b/apps/docs/features/SearchV2/useSearchV2Variant.test.ts
new file mode 100644
index 00000000000..50f5cfbc2b0
--- /dev/null
+++ b/apps/docs/features/SearchV2/useSearchV2Variant.test.ts
@@ -0,0 +1,74 @@
+// @vitest-environment jsdom
+import { renderHook } from '@testing-library/react'
+import { posthogClient, useFeatureFlags, useSearchParamsShallow } from 'common'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { SEARCH_V2_FLAG } from './constants'
+import { useSearchV2Variant } from './useSearchV2Variant'
+
+vi.mock('common', () => ({
+ useConsentState: vi.fn(() => ({ hasAccepted: true })),
+ posthogClient: { captureExperimentExposure: vi.fn() },
+ useFeatureFlags: vi.fn(),
+ useSearchParamsShallow: vi.fn(),
+}))
+
+const captureExposure = vi.mocked(posthogClient.captureExperimentExposure)
+
+function renderVariant({
+ flagValue,
+ override,
+}: {
+ flagValue?: string | boolean
+ override?: string
+}) {
+ const posthog = flagValue === undefined ? {} : { [SEARCH_V2_FLAG]: flagValue }
+ const searchParams = new URLSearchParams(override ? { [SEARCH_V2_FLAG]: override } : {})
+
+ vi.mocked(useFeatureFlags).mockReturnValue({ posthog } as unknown as ReturnType<
+ typeof useFeatureFlags
+ >)
+ vi.mocked(useSearchParamsShallow).mockReturnValue(
+ searchParams as unknown as ReturnType<typeof useSearchParamsShallow>
+ )
+
+ return renderHook(() => useSearchV2Variant())
+}
+
+describe('useSearchV2Variant', () => {
+ beforeEach(() => {
+ captureExposure.mockClear()
+ })
+
+ it('returns the rollout variant and records its exposure', () => {
+ const { result } = renderVariant({ flagValue: 'search-v2-active' })
+
+ expect(result.current).toBe('search-v2-active')
+ expect(captureExposure).toHaveBeenCalledWith(
+ SEARCH_V2_FLAG,
+ { variant: 'search-v2-active' },
+ true
+ )
+ })
+
+ it('records control exposure for visitors outside the rollout', () => {
+ const { result } = renderVariant({ flagValue: false })
+
+ expect(result.current).toBe('control')
+ expect(captureExposure).toHaveBeenCalledWith(SEARCH_V2_FLAG, { variant: 'control' }, true)
+ })
+
+ it('records no exposure before the flag store loads', () => {
+ const { result } = renderVariant({})
+
+ expect(result.current).toBe('control')
+ expect(captureExposure).not.toHaveBeenCalled()
+ })
+
+ it('records no exposure when the query param forces a variant', () => {
+ const { result } = renderVariant({ flagValue: false, override: 'search-v2-active' })
+
+ expect(result.current).toBe('search-v2-active')
+ expect(captureExposure).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/docs/features/SearchV2/useSearchV2Variant.ts b/apps/docs/features/SearchV2/useSearchV2Variant.ts
index 7615e6a86b3..a30dac478b5 100644
--- a/apps/docs/features/SearchV2/useSearchV2Variant.ts
+++ b/apps/docs/features/SearchV2/useSearchV2Variant.ts
@@ -1,6 +1,7 @@
'use client'
-import { useFeatureFlags, useSearchParamsShallow } from 'common'
+import { posthogClient, useConsentState, useFeatureFlags, useSearchParamsShallow } from 'common'
+import { useEffect } from 'react'
import { SEARCH_V2_FLAG, type SearchV2Variant } from './constants'
@@ -13,12 +14,25 @@ const VARIANTS: SearchV2Variant[] = ['control', 'search-v2-active']
*/
export function useSearchV2Variant(): SearchV2Variant {
const { posthog } = useFeatureFlags()
+ const { hasAccepted } = useConsentState()
const searchParams = useSearchParamsShallow()
const override = searchParams.get(SEARCH_V2_FLAG)
+ const isOverridden = VARIANTS.includes(override as SearchV2Variant)
- if (VARIANTS.includes(override as SearchV2Variant)) {
+ const flagValue = posthog[SEARCH_V2_FLAG]
+ const isFlagResolved = flagValue !== undefined
+ const flagVariant: SearchV2Variant =
+ flagValue === 'search-v2-active' ? 'search-v2-active' : 'control'
+
+ useEffect(() => {
+ if (isOverridden || !isFlagResolved) return
+
+ posthogClient.captureExperimentExposure(SEARCH_V2_FLAG, { variant: flagVariant }, hasAccepted)
+ }, [isOverridden, isFlagResolved, flagVariant, hasAccepted])
+
+ if (isOverridden) {
return override as SearchV2Variant
}
- return posthog[SEARCH_V2_FLAG] === 'search-v2-active' ? 'search-v2-active' : 'control'
+ return flagVariant
}