npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@atlaskit/feature-flag-client

v5.6.0

Published

A client to take care of feature flag evaluation and tracking of exposure events

Downloads

138

Readme

Feature flag client

This client makes it easy to work with feature flags and dark features. By using it, exposure events will be fired automatically allowing analysis of important metrics out of the box.

Usage

Bootstrap

import FrontendFeatureFlagClient from '@atlaskit/feature-flag-client';

const client = new FrontendFeatureFlagClient({
  analyticsHandler: myAnalyticsHandler,
  flags: {
    'my.experiment': {
      value: 'experiment',
      explanation: {
        kind: 'RULE_MATCH',
        ruleId: '111-bbbbb-ccc',
      },
    },
    'my.boolean.flag': {
      value: false,
    },
    'my.json.flag': {
      value: {
        nav: 'blue',
        footer: 'black',
      },
      explanation: {
        kind: 'RULE_MATCH',
        ruleId: '111-bbbbb-ccc',
      },
    },
    'my.detailed.boolean.flag': {
      value: false,
      explanation: {
        kind: 'RULE_MATCH',
        ruleId: '111-bbbbb-ccc',
      },
    },
  },
});

Retrieving values

// flag set, returns real value
client.getBooleanValue('my.detailed.boolean.flag', { default: true }); // > false

// flag set, returns real value
client.getVariantValue('my.experiment', {
  default: 'control',
  oneOf: ['control', 'experiment'],
}); // > experiment

// flag unset, returns default value
client.getBooleanValue('my.unlisted.boolean.flag', { default: false }); // > false

// flag value doesn't match expected, returns default
client.getVariantValue('my.experiment', {
  default: 'control',
  oneOf: ['control', 'variant-a'],
}); // > control

// flag set, returns real value
client.getJSONFlag('my.json.flag'); // > { nav: 'blue', footer: 'black' }

// flag set, returns real value
client.getRawValue('my.delimited.flag', {
  default: 'dashboard,issue-view,classic-board',
}); // > dashboard,issue-view,classic-board,next-gen-board

NOTE: For performance reasons, the client will only run validation checks once and then cache this result for subsequent calls. It is important that you always use the same method and oneOf values whenever retrieving the value for your flag.

Setting flags asynchronously

If you load your flags after the app bootstrap, you set the to the client through the 'setFlags' method.

client.setFlags({
  'my.async.boolean.flag': {
    value: false,
    explanation: {
      kind: 'RULE_MATCH',
      ruleId: '333-bbbbb-ccc',
    },
  },
});

Exposure events

Exposure events are great for tracking which users and sites saw your flags and in what state. These exposure events can be used for:

  1. Rollouts,
  2. Experiments, and
  3. Incident investigations.

To send any exposure events, you need to provide an object that looks like src/types.ts#AnalyticsHandler (or internally to Atlassian this is the same as the AnalyticsWebClient).

This can be set on initialisation of the client, or later with the client.setAnalyticsHandler(analyticsHandler); fucntion.

How to send exposure events for all feature flags

To turn on Automatic Exposures, you can either:

  1. Set isAutomaticExposuresEnabled to true in the constructor args, or
  2. Call client.setIsAutomaticExposuresEnabled(true).

When this mode is enabled, exposure events will be handled in the following circumstances: sent for all simple flags (ie. flags without an explanation), and for all evalulated (ie. flags with evalauation details) with the shouldTrackExposureEvent option set to false.

  • Simple Flags: Automatic exposure events will always be fired with the flag key and value
  • Evaluated Flags with shouldTrackExposureEvent: false: Automatic exposure events will fire with explanation details
  • Evaluated Flags with shouldTrackExposureEvent: true: Automatic exposure events will not fire, but the regular ones will.

The exposure events that are fired in this fashion will be tagged with an additional tag on the event tags: ['autoExposure'] to make it easier to differientiate between those exposures fired manually and those fired automatically.

How to avoid firing the exposure event

You can skip the exposure event by setting 'shouldTrackExposureEvent' to 'false'. Note: this will not disable the automatic exposure event from firing when the Automode is enabled.

client.getBooleanValue('my.detailed.boolean.flag', {
  default: true,
  shouldTrackExposureEvent: false,
});

How to fire the exposure event manually

client.trackExposure('my.detailed.boolean.flag', {
  value: true,
  explanation: {
    kind: 'RULE_MATCH',
    ruleId: 'aaaa-vbbbb-ccccc',
  },
});

How to include custom attributes in the exposure event

You can send extra attributes by including an object within the exposureData attribute. This object accepts strings, numbers or booleans. Reserved attributes can't be used, like flagKey, reason, ruleId or value.

client.getBooleanValue('my.detailed.boolean.flag', {
  default: true,
  shouldTrackExposureEvent: false,
  exposureData: {
    myAttribute1: 'whatever',
    myAttribute2: 2,
    myAttribute3: true,
  },
});

If you are firing the exposure event manually, the 3rd argument (optional) includes the custom attributes. This object accepts strings, numbers or booleans. Reserved attributes can't be used, like flagKey, reason, ruleId or value.

client.trackExposure(
  'my.detailed.boolean.flag',
  {
    value: true,
    explanation: {
      kind: 'RULE_MATCH',
      ruleId: 'aaaa-vbbbb-ccccc',
    },
  },
  {
    myAttribute1: 'whatever',
    myAttribute2: 2,
    myAttribute3: true,
  },
);

Viewing flag stats

The getFlagStats method can be called to collate some information about client usage. This method returns an object that maps each flag key to an object describing how it has been accessed.

The only statistic that is currently returned for each flag is an evaluationCount, which indicates how many times the flag has been evaluated through any of the retrieval methods.

client.getVariantValue('my.experiment', {
  default: 'control',
  oneOf: ['control', 'experiment'],
});

client.getFlagStats(); // { 'my.experiment' : { evaluationCount: 1 }}

If you reset some flags using setFlags or clear, the stats collected for these flags will also be reset.