Skip to main content
Skip table of contents

Staggering features to anonymous users

Overview

Feature flags can be set to roll out to a percent of authenticated users, but anonymous visitors will need a client-side treatment for rollout which is not part of the server-side feature flags stack.

To roll out a feature to a percent of anonymous users, use the useStaggeredFeatureRelease() React hook which is part of the department-of-veterans-affairs/platform-utilities/react-hooks library. See the example below:

Example that displays a modal for 75% of anonymous visitors:

CODE
import { VaModal } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import React from 'react';
import { useStaggeredFeatureRelease } from '@department-of-veterans-affairs/platform-utilities/react-hooks';

function StaggeredFeatureExample() {
  // Release this feature to 75% of anonymous users.
  const PERCENT_OF_ANON_USERS = 75;

  // Provide a value for the localStorage key used by
  // useStaggeredFeatureRelease().
  const LOCAL_STORAGE_KEY = 'use-staggered-release-react-hook-example';

  // Instantiate the hook. The hook will return a boolean that can be used in
  // decisions.
  const viewModal = useStaggeredFeatureRelease(
    PERCENT_OF_ANON_USERS,
    LOCAL_STORAGE_KEY,
  );

  // Renders a modal if viewModal is true.
  return (
    <>
      {viewModal && (
        <VaModal
          role="dialog"
          cssClass="va-modal announcement-brand-consolidation"
          visible
          id="modal-announcement"
          modalTitle="An example of the useStaggeredFeatureRelease React hook."
        >
          <p>This modal displays for 75% of users.</p>
        </VaModal>
      )}
    </>
  );
}

export default StaggeredFeatureExample;

This hook is meant to be used in non-authenticated parts of the site for staggered releases of new features. As a feature toggle cannot be used in such environments, this will allow a specified percent of users to use the new feature. The useStaggeredFeatureRelease component stores its calculation result in localStorage to avoid re-calculating each time a user visits.

Provide evidence

The react hook is not in a highly visible location surrounding other documentation that impacts teams across http://VA.gov .


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.