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 calling StatsigClient.initializeSync
, 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. If you do not want this background refresh to happen, you can pass in disableBackgroundCacheRefresh
in the options when calling initializeSync
.
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 calling 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 unnecessary 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.
If you are using a server SDK to bootstrap your js/react app, you will need to make some updates to how your server SDK generates values. One of the optimizations we made with this new js-client SDK was to remove the sha256
library for hashing gate/experiment names. Instead, we use a djb2
hash. By default, all server SDKs generate sha256
hashes of gate/experiment names in the getClientInitializeResponse
method. You will need to set the hash algorithm parameter to that method call to "djb2"
instead in order to bootstrap this new client SDK. One of the benefits to this hashing algorithm is it will make the entire payload smaller as well, so its a net win on package size, speed, and payload size for the SDK.
For example, if you are bootstrapping from a nodejs app, you will need to do:
statsig.getClientInitializeResponse(
user,
'[client-key]',
{
hash: 'djb2',
},
);
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 the evaluations data from a JSON string received from a Statsig Server SDK's getClientInitializeResponse
method.
setData: (data: string) => Promise<void>;
Note: You can use setDataLegacy
if your Server SDK is outdated.
setData is supported by the following Statsig Server SDKs
- node-js-server-sdk@5.20.0
- java-server-sdk@1.18.0
- ruby-sdk@1.34.0
- dotnet-sdk@1.25.0
- php-sdk@3.2.0
setDataLegacy
- Manually set evaluations data for the given user.
This method is deprecated and is provided only to support older versions of Statsig server SDKs.
setDataLegacy: (data: string, user: StatsigUser) => void;