Skip to main content

Client Persistent Assignment

Persistent assignment allows you to ensure that a user's variant stays consistent while an experiment is running, regardless of changes to allocation or targeting.

Persistent Storage

The persistent storage adapter allows you to plug in your own storage solution that Statsig SDK uses to persist user assignments. The user persistent storage interface consists of just a load/loadAsync, save, delete API for read/write operations.

info

Currently only supported in Javascript, iOS and Android On Device Evaluation SDKs

Persistent Storage Logic

  • Providing a storage adapter on Statsig initialization will give the SDK access to read & write on your custom storage
  • Providing user persisted values to get_experiment will inform the SDK to
    • save the evaluation of the current user on first evaluation
    • load the previously saved evaluation of a persisted user on subsequent evaluations
    • delete the previously saved evaluation of a persisted user if the experiment is no longer active
  • Not providing user persisted values to get_experiment will delete a previously saved evaluation

Example usage

const storage = new CustomStorageAdapter(); // Need to implement
const adapter = new UserPersistentOverrideAdapter(storage);
const client = new StatsigOnDeviceEvalClient(
'client-key',
{ overrideAdapter: adapter }
);
await client.initializeAsync();

const userInControl = { userID: "123" }
const userInUnknown = { userID: "unknown" }
const userPersistedValues = adapter.loadUserPersistedValues(user, 'userID');

let experiment = client.getExperiment('active_experiment', user, { userPersistedValues });
console.log(experiment.getGroupName()) // 'Control'

experiment = client.getExperiment('active_experiment', userInUnknown, { userPersistedValues });
console.log(experiment.getGroupName()) // 'Control'