Skip to main content

Using EvaluationsDataAdapter

The StatsigEvaluationsDataAdapter is the default EvaluationsDataAdapter used by a StatsigClient instance.

It handles fetching and caching values from Statsig's servers. If required, you can create your own custom EvaluationsDataAdapter and provide it to the Statsig client via StatsigOptions.dataAdapter.

Overview

Synchronous Behavior (Cache)

When callling StatsigClient.initalizeSync, the StatsigEvaluationsDataAdapter will load values from Cache and provide them to the client. The client will also call refresh in the background via StatsigEvaluationsDataAdapter.getDataAsync, leading to values lagging until the next time initializeSync or updateUserSync is called.

In practice (Unless an additional update call is made), this means that for the very first session of a given user, there will be no values, and not until the next session will the cache have values.

Asynchronous Behavior (Network)

When callling StatsigClient.initializeAsync, the StatsigEvaluationsDataAdapter will load values from Cache and provide them to the client. The client will then block on a call to StatsigEvaluationsDataAdapter.getDataAsync, allowing the ability to await the latest values from Statsig.

If you want the latest values, but do not want to await the asynchronous call, you may call initializeAsync and simply .catch the promise. Note that this could lead to values changing mid-session as newer values arrive from the network.

Advanced Usage

Getting the Data Adapter

...

Bootstrapping

Bootstrapping allows you to provide data for a given StatsigUser. This can be useful if you are are running a Statsig Server SDK on your backend and do not wish to make unneccesary network calls (See statsig-node's getClientInitializeResponse).

This approach can also be useful if you are building a mobile app and want to bundle values with your application. Then you can load the values from a local file at startup and provide them to the data adapter.

...

Prefetching

...

Custom Implementation

If you would like to customize when and how data is fetched, as well as where it is stored, you can create your own class the conforms to the EvaluationsDataAdapter type.

The EvaluationsDataAdapter type outlines the following functions:

  • attach - Called when the EvaluationsDataAdapter is passed into a StatsigClient via StatsigOptions. This allows the EvaluationsDataAdapter to use the same SDK Key and StatsigOptions as the StatsigClient instance.

    • attach: (sdkKey: string, options: StatsigOptionsCommon | null) => void
  • getDataSync - Synchronously get evaluation data for the given user. Called during initializeSync and/or updateUserSync. It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.

    • getDataSync: (user: StatsigUser) => DataAdapterResult | null;
  • getDataAsync - Asynchronously get evaluation data for the given user. Called during initializeAsync and/or updateUserAsync.

    • getDataAsync: (current: DataAdapterResult | null, user: StatsigUser) =>
      Promise<DataAdapterResult | null>;
  • prefetchData - Manually trigger a fetch for new evaluations data for the given user.

    • prefetchData: (user: StatsigUser) => Promise<void>;
  • setData - Manually set evaluations data for the given user.

    • setData: (data: string, user: StatsigUser) => Promise<void>;