Skip to main content

getFeature

Since js-sdk version 1.4.0

getFeature()

Returns feature with remote config

interface Growthflags {
//...
getFeature(featureName: string): {
name: string,
config: JSONObject,
enabled: boolean,
variant: { name: string, remoteConfig: JSONObject },
}
}

Arguments

  • featureName - Feature flag name. Required

Example

Feature theFeature exists with remote config:

{
"buttonText": "Press me"
}
import GrowthFlags from '@growthflags/js-sdk';

const flags = GrowthFlags.create({
publicApiKey: 'pk_3a693ae7b88bf3afb8d9eca2304e7d66ff484a41bba6e211',
env: 'staging',
});

flags.fetchFeatureFlags({ email: '[email protected]' })

const defaultConfig = { buttonText: 'Click me' };
const { enabled, config } = growthflags.getFeature('theFeature', { defaultConfig });

const Page = () => {
return (
// ...
{
enabled && (
<Button>
{config.buttonText}
</Button>
)
}
);
};

Variants

Since js-sdk version >=1.4.2
/* ... */
import analytics from 'any-analytics-you-using';

const defaultConfig = { buttonText: 'Click me' };
const { enabled, config, variant } = growthflags.getFeature('feature-with-ab', { defaultConfig });

const trackClicks = () =>
analytics.sendEvent({ // Any analytics you use
name: 'button-clicked',
variant: variant.name,
config: config,
...
});


const Page = () => {
return (
// ...
{
enabled && (
<Button onClick={trackClicks}>
{config.buttonText}
</Button>
)
}
);
};