Feature flags in code
Read feature-flag values in your application with Spectry.getFlag().
Feature flags let you turn features on/off or run multivariate rollouts without redeploying. You configure them in the dashboard (see Feature flags) and read them in code.
Reading a flag
// Boolean flag
if (Spectry.getFlag('new_checkout')) {
renderNewCheckout();
}
// Multivariate flag — returns the assigned variant's value
const layout = Spectry.getFlag('pricing_layout'); // e.g. 'compact'
getFlag() is synchronous and returns null for a key it doesn't know about.
Flags arrive with the settings payload
Values are populated when the SDK finishes fetching your site settings — which is after the snippet starts loading. Code that runs before then will read null and take the "off" path. If you need to gate something at first paint, treat null as the default state explicitly and subscribe for the real value:
Spectry.onFlagChange('new_checkout', (value) => {
if (value) renderNewCheckout();
});
The callback fires whenever the value changes, including the first time it's populated after init.
How assignment works
- Rollout percentage gates exposure — only that share of visitors receives the flag.
- Multivariate flags distribute visitors across variants by weight.
- Assignment is deterministic per visitor, so a given visitor consistently sees the same value.
Use the exact flag key from the dashboard. The key is immutable once a flag is created — which also means a typo ingetFlag()fails silently asnullrather than throwing.