Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statsig adapter #27

Merged
merged 3 commits into from
Jan 20, 2025
Merged

Statsig adapter #27

merged 3 commits into from
Jan 20, 2025

Conversation

AAorris
Copy link
Contributor

@AAorris AAorris commented Jan 14, 2025

Statsig Adapter

Implements a Statsig adapter using statsig-node and statsig-node-vercel.

The adapter takes initialization details for Statsig and the Edge Config adapter, and returns an interface that can be used to declare Feature Gates, Feature Gates with metric lifts, Experiments, DynamicConfigs, and Autotunes.

The adapter will initialize Statsig, notably with defaults where IDLists are not synced and there is no strategy to sync them. This can be changed by passing alternatives in statsigOptions, but these defaults reduce network fetching when flags are precomputed in middleware.

The decide function will not log events to Statsig — Instead, it's expected that this will be done client side. The reasoning for this is that Next.js may prefetch routes, and we do not want to expose a user to experiments or gates just because they were prefetched. Additionally, because of the serverless runtime context, the buffering and flushing of logs can be unreliable.

For this reason, we will expect the client to log events, for which they will either need to log events manually with a rule ID, or take these results as initial values while checking them again with exposures on the client side.

Simple usage

The usage of calling the adapter directly is the simplest case — A feature gate without metric lifts.

const statsigAdapter = createStatsigAdapter({
  statsigSecretKey: 'statsigSecretKey',
});

export const exampleFlag = flag<boolean, StatsigUserEntities>({
  key: 'example',
  adapter: statsigAdapter(),
});

Methods

featureGate — Receives the boolean value and ruleID and can map the result to a flag value

export const featureGateFlag = flag<[boolean, string], StatsigUserEntities>({
  key: 'example-gate',
  adapter: statsigAdapter.featureGate((value, ruleId) => [value, ruleId]),
});

dynamicConfig — Receives the evaluated value of a DynamicConfig object and ruleId and can map the result to a flag value

export const dynamicConfigFlag = flag<string, StatsigUserEntities>({
  key: 'example-config',
  adapter: statsigAdapter.dynamicConfig((value) => value.heroText),
});

experiment — Receives the evaluated value of an Experiment object and ruleId and can map the result to a flag value

export const dynamicConfigFlag = flag<'control' | 'test', StatsigUserEntities>({
  key: 'example-experiment',
  adapter: statsigAdapter.experiment((value, ruleId) => value.experimentGroup),
});

autotune — Receives the evaluated value of an Autotune and ruleId and can map the result to a flag value

const experimentGroups = ['control', 'variant1', 'variant2'] as const;
type ExperimentGroup = (typeof experimentGroups)[number];

export const dynamicConfigFlag = flag<ExperimentGroup, StatsigUserEntities>({
  key: 'example-autotune',
  adapter: statsigAdapter.autotune((value, ruleId) => value.experimentGroup),
});

initialize — Statsig.initialize will be called lazily, when a flag's decide method is invoked. To initialize Statsig earlier using the adapter's implementation:

statsigAdapter.initialize()

@AAorris AAorris requested review from dferber90 and AndyBitz January 14, 2025 21:05
@AAorris AAorris self-assigned this Jan 14, 2025
Copy link

vercel bot commented Jan 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flags-sdk-snippets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 20, 2025 4:37pm
summer-sale ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 20, 2025 4:37pm

Copy link

socket-security bot commented Jan 14, 2025

New and updated dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] 🔁 npm/[email protected] environment 0 238 kB react-bot
npm/[email protected] Transitive: network +8 2.52 MB kenny-statsig
npm/[email protected] network +7 2.63 MB xinlili

View full report↗︎

Acts as a feature flag by default, and includes `.experiment` `.featureGate` and so on for advanced usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants