Split OpenFeature Web Provider
npm install @splitsoftware/openfeature-web-split-provider``sh`
npm install @splitsoftware/openfeature-web-split-provider
sh
npm install @splitsoftware/splitio-browserjs
npm install @openfeature/web-sdk
`$3
`js
import { OpenFeature } from '@openfeature/web-sdk';
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';const splitFactory = SplitFactory({
core: {
authorizationKey: 'your auth key'
}
});
const provider = new OpenFeatureSplitProvider(splitFactory);
OpenFeature.setProvider(provider);
`Use of OpenFeature with Split
After the initial setup you can use OpenFeature according to their documentation.One important note is that the Split Provider requires a targeting key to be set. Often times this should be set when evaluating the value of a flag by setting an EvaluationContext which contains the targeting key. An example flag evaluation is
`js
const context: EvaluationContext = {
targetingKey: 'TARGETING_KEY',
trafficType: 'account'
};
OpenFeature.setContext(context)
`Evaluate with details
Use the get*Details(...) APIs to get the value and rich context (variant, reason, error code, metadata). This provider includes the Split treatment config as a raw JSON string under flagMetadata["config"]`js
const booleanTreatment = client.getBooleanDetails('boolFlag', false);const config = booleanTreatment.flagMetadata.config;
`Evaluate with attributes
Evaluation attributes must be set in context before evaluation`js
const context: EvaluationContext = {
targetingKey: 'TARGETING_KEY',
trafficType: 'account',
plan: 'premium',
couppon: 'WELCOME10'
};
OpenFeature.setContext(context);
const booleanTreatment = client.getBooleanDetails('boolFlag', false);
`Tracking
To use track(eventName, context, details) you must provide:
- A non-blank
eventName.
- A context with:
- targetingKey (non-blank).
- trafficType (string, e.g. "user" or "account", "user" by default).Optional:
- details with:
-
value: numeric event value (defaults to 0).
- properties: map of attributes (prefer primitives: string/number/boolean/null).Example:
`js
const context = { targetingKey: 'user-123', trafficType: 'account' }
const details = { value: 19.99, properties: { plan: 'pro', coupon: 'WELCOME10' }}client.setEvaluationContext(context)
client.track('checkout.completed', details)
``