feat: Add generic useABTest hook for A/B testing#25541
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Why no E2E tests are needed:
This is purely additive infrastructure code that enables future A/B testing capabilities but doesn't change any current app behavior. Performance Test Selection: |
|
| **Parameters:** | ||
|
|
||
| - `flagKey` - The feature flag key in LaunchDarkly (camelCase, e.g., `'buttonColorTest'`) | ||
| - `variants` - Object mapping variant names to their data. **Must include a `control` key** which is used as the fallback when the flag is not set or invalid. |
There was a problem hiding this comment.
Nit: wonder if it makes sense that the hook takes an optional default variant value?
In case there are cases where the default value is "treatment" instead of "control" ?



Description
Adds a
useABTesthook that provides a simple, type-safe interface for A/B testing using LaunchDarkly feature flags. The hook reads the assigned variant fromRemoteFeatureFlagController(which handles bucketing via SHA256 hash ofmetametricsId + flagName) and maps it to variant data defined by the caller.Why: We need a standardized, reusable pattern for running A/B tests without requiring per-test boilerplate or Segment schema changes for each experiment.
What's included:
useABTesthook with TypeScript support{ name }from controller and legacy string formatChangelog
CHANGELOG entry: null
Related issues
Fixes: N/A - Infrastructure for future A/B testing
Manual testing steps
{ name: "treatment" }→ hook returns treatment variant data,isActiveistrueisActiveisfalseSWAP_PAGE_VIEWEDevent includesab_tests: { select_amount_text: "treatment" }Screenshots/Recordings
Before
N/A - New feature
After
N/A - No UI changes; hook is internal infrastructure. First experiment (select amount text) requires LD flag to be configured to observe behavior difference.
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Low Risk
New hook + docs/tests with no changes to existing runtime flows unless adopted by callers; primary risk is incorrect variant selection/activation logic when teams start using it.
Overview
Adds a new generic
useABTestReact hook that maps a remote feature-flag value to locally-defined variant data, exposing{ variant, variantName, isActive }with a guaranteedcontrolfallback and protection against prototype-key matches.The hook supports both the controller’s
{ name }A/B-test object format and a legacy string flag value format, is exported viaapp/hooks/index.ts, and is backed by extensive unit tests plus a newdocs/ab-testing.mdguide covering LaunchDarkly configuration and analytics tracking conventions.Written by Cursor Bugbot for commit 561734a. This will update automatically on new commits. Configure here.