Use layer parameters in code

Read a layer's shared parameters with getLayer so experiments in the layer can change without code changes, and learn how Statsig logs and bills layer exposures.

A layer owns its parameters. Your code reads a layer parameter with the Statsig SDK's getLayer method, and each experiment in the layer sets its own value for that parameter. You can then add, stop, or iterate on experiments in the layer without changing code.

Share parameters across experiments

Parameters exist at the layer level, and experiments within a layer share associated parameters. Your code works only with layer parameters, so multiple experiments that change the same parameter can run and iterate without code changes.

For example, your product has a signup dialog with text that your team tests frequently. Some tests run in parallel while others iterate on previous experiments. If your code calls each experiment directly, it grows similar to:

jsx
let signUpText = DEFAULT_SIGNUP_TEXT;
const signUpTestV1 = statsig.getExperiment("sign_up_dialog_text_test_v1");
const signUpTestV2 = statsig.getExperiment("sign_up_dialog_text_test_v2");
const specialSignUpTest = statsig.getExperiment("sign_up_test_special_offer");
const holidaySignUpTest = statsig.getExperiment("sign_up_test_holiday");

if (signUpTestV1.get("is_in_test", false)) {
  // original test, added in app version v1.2
  signUpText = signUpTestV1.get("dialog_content", DEFAULT_SIGNUP_TEXT);
} else if (signUpTestV2.get("is_in_test", false)) {
  // v2 of the original test, added in app version v1.6 because we wanted to test a new copy but don't want to stop v1
  signUpText = signUpTestV2.get("dialog_content", DEFAULT_SIGNUP_TEXT);
} else if (specialSignUpTest.get("is_in_test", false)) {
  // test showing a special offer in the text, added in v2.0
  signUpText = specialSignUpTest.get("dialog_content", DEFAULT_SIGNUP_TEXT);
} else if (holidaySignUpTest.get("is_in_test", false)) {
  // test showing some holiday greetings in the dialog, added in v2.1
  signUpText = holidaySignUpTest.get("dialog_content", DEFAULT_SIGNUP_TEXT);
}

// Then we display the text in the dialog

Every time you add a test, you change the code. The change ships only with a new release.

With a layer, the code reads one parameter:

jsx
let signUpText = statsig
  .getLayer("sign_up_tests")
  .get("sign_up_dialog_text", DEFAULT_SIGNUP_TEXT);

// Then we display the text in the dialog

To add a test, add a new experiment to the same layer and choose sign_up_dialog_text as a parameter. The SDK serves the value from whichever experiment Statsig allocates the user to, with no code change or app release.

Use getLayer for layered experiments

Experiments in a layer remain accessible through getExperiment but that API evaluates only the current experiment. Use getLayer so the SDK honors layer-level decisions, mutual exclusion, and shared parameters.

How exposures work with layers

Calling getLayer("layer_name") by itself doesn't log an exposure. Statsig logs a statsig::layer_exposure event when you read a specific parameter with getLayer("layer_name").get("parameter_name").

  • If Statsig assigns the user to an experiment within the layer, the statsig::layer_exposure event is billable.
  • If Statsig doesn't assign the user to an experiment within the layer, the statsig::layer_exposure event isn't billable.

Repeated reads of the same layer parameter for the same user within the deduplication window count as a single billable exposure. Reads of different layer parameters use separate dedupe keys and might count separately.

Deduplication window lengths:

  • Client SDKs (JS/web, iOS, Android): 10 minutes per user, layer, and parameter combination.
  • Server SDKs: About 1 minute. The dedupe set resets every 60 seconds.

Was this helpful?

For AI agents: a documentation index is available at /llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

On this page