Skip to main content
A remote configuration is a set of keys and string values that you create in the dashboard and read in your code. When you change a value in the dashboard, every client receives the new value at its next refresh, without a deploy. Only Analytics Web (@grainql/analytics-web) reads remote configuration. Grain Tag has no remote configuration.
Every value is a string (Record<string, string>). A flag is the string 'true' or 'false', and a number is a string that you parse. When this page and the TypeScript types of the published package disagree, the types win.

The cache-first flow

Analytics Web never waits for the network to answer a read. One read goes through these steps.
1

Answer from cache or defaults

getConfig returns the value from the cache in localStorage. In Node.js the cache is in memory. If the cache holds no value, getConfig returns the value from defaultConfigurations. If neither holds the key, it returns undefined.
2

Fetch in the background

The read starts a background request to the API. The API applies the rules of the tenant and returns the values for this client.
3

Update the cache and notify listeners

When the response arrives, Analytics Web writes the values to the cache and calls every change listener with the full set of values. The next getConfig returns the new value.
Without defaultConfigurations, the first getConfig on a new browser returns undefined until the first response arrives. Set a default for every key that the UI reads on first paint.

Read a value

The synchronous methods answer from the cache and defaults. Use them in a component body and in event handlers.
The asynchronous methods answer from the cache first and then wait for the API when the cache holds nothing. With forceRefresh: true they skip the cache and wait for the API.
fetchConfig sends one request to the API and returns the full response, or null when the request fails. It also updates the cache.

Options

getConfigAsync, getAllConfigsAsync, and fetchConfig accept a RemoteConfigOptions object. The response of fetchConfig holds these fields. Read Personalization for the properties option in a full example.

Preload at startup

preloadConfig waits for the API one time, then every getConfig answers from the cache. Call it after setUserId so that the API evaluates the rules for the right user. With no immediateKeys, it loads every key.

Listen for changes

A change listener receives the full set of values each time the cache updates. The cache updates after a background fetch, after a manual refresh, and after fetchConfig.
Remove the listener when the component unmounts or before you call destroy(). In React, useConfig and useAllConfigs register the listener for you and update the component when the value changes. Read useConfig.

Refresh and cache options

Three options of GrainConfig control the refresh timer and the cache.
Read Configuration options for the other options.

Create a configuration in the dashboard

  1. Open grainql.com/dashboard.
  2. Open Dashboard > Remote Config.
  3. Create a configuration key and set its default value.
  4. If a segment of users must receive a different value, add a rule.
  5. Publish.
A published change reaches a client at its next refresh, or at its next read that waits for the API.

Rules for the client

  1. Set a default for every key that the UI reads before the first response.
  2. Compare values as strings. 'true' is a string, not a boolean.
  3. Call preloadConfig after setUserId when a user-specific value must be correct on first paint.
  4. Do not put a secret in a configuration value. With authStrategy: 'NONE', anyone who holds the tenant alias can read every value.
Read Feature flags and A/B tests for the two common uses. Read Configuration methods for the signatures.