View Source GrowthBook (GrowthBook v0.3.0)

GrowthBook is a GrowthBook SDK for Elixir/OTP.

This SDK follows the guidelines set out in GrowthBook's documentation, and the API is tested on conformance with the test cases from the JS SDK.

To ensure an Elixir-friendly API, the implementation deviates from the official SDK in the following ways:

  • Instead of tuple-lists, this library uses actual tuples
  • Comparisons with undefined are implemented by using :undefined
  • Function names are converted to snake_case, and is_ prefix is replaced with a ? suffix
  • Instead of classes, a Context struct is used (similar to %Plug.Conn{} in plug)

What is GrowthBook?

GrowthBook is an open source A/B testing platform. The platform works significantly different from other A/B testing platforms, most notably: it is language agnostic.

Clients by default work offline, and manage their own data. This means that you are free to implement A/B tests server-side, or client-side without worrying about things like "anti-flicker" scripts, or the added latency of JS embeds.

Furthermore, GrowthBook supports both experiments (A/B tests and multivariate tests) and feature flags. Because all logic to run experiments and feature flags is contained in the library, there is virtually no added latency to running experiments or using feature flags.

Installation

Add growthbook to your list of dependencies in mix.exs:

def deps do
  [
    {:growthbook, "~> 0.2"}
  ]
end

Usage

# Create a context, which can be reused for multiple users
# Approach 1: Manual Feature Configuration
features_config = Jason.decode!("""
{
  "features": {
    "send-reminder": {
      "defaultValue": false,
      "rules": [{ "condition": { "browser": "chrome" }, "force": true }]
    },
    "add-to-cart-btn-color": {
      "rules": [{ "variations": [{ "color": "red" }, { "color": "green" }] }]
    }
  }
}
""")

features = GrowthBook.Config.features_from_config(features_config)

context = GrowthBook.build_context(
  %{
    "id" => "12345",
    "country_code" => "NL",
    "browser" => "chrome"
  },
  features  # Explicitly provide features
)

# Approach 2: Auto-refreshing Features
case GrowthBook.init(
  client_key: "key_prod_123...",
  api_host: "https://cdn.growthbook.io",
  decryption_key: "key_123...",
  swr_ttl_seconds: 60,
  on_refresh: fn features ->
    Logger.info("Features refreshed: #{map_size(features)} available")
    MyApp.FeatureNotifier.features_updated(features)
  end
) do
  {:ok, :initialized} -> # Features loaded successfully
    # Start using GrowthBook...
  {:error, reason} -> # Handle error
    # Handle initialization failure...
end

# Create context that automatically uses latest features
context = GrowthBook.build_context(%{
  "id" => "12345",
  "country_code" => "NL",
  "browser" => "chrome"
})

# Usage is the same for both approaches:

# Use a feature toggle
if GrowthBook.feature(context, "send-reminder").on? do
  Logger.info("Sending reminder")
end

# Use a feature's value
color = GrowthBook.feature(context, "add-to-cart-btn-color").value["color"]
Logger.info("Color: " <> color)

# Run an inline experiment
if GrowthBook.run(context, %GrowthBook.Experiment{
  key: "checkout-v2",
  active?: true,
  coverage: 1,
  variations: [1, 2]
}).in_experiment? do
  Logger.info("In experiment")
end

Auto-Refresh Features

GrowthBook now supports automatic feature refreshing from your GrowthBook API. Instead of manually managing features, you can initialize GrowthBook with your API credentials.

Core Components

  1. GenServer-Based Repository: Implemented as a GenServer that maintains feature state in memory (with stale-while-revalidate (SWR) caching strategy) that handles fetching, caching of features and refresh cycles based on configuration.
# Initialize GrowthBook with auto-refresh
case GrowthBook.init(
  client_key: "key_prod_123...",
  api_host: "https://cdn.growthbook.io",
  decryption_key: "key_123...",  # Optional: Required for encrypted features
  swr_ttl_seconds: 60,  # Optional: Default is 60 seconds
  refresh_strategy: :periodic,  # Optional: :periodic (default) or :manual
  on_refresh: fn features ->  # Optional: Callback when features are refreshed
    Logger.info("Features refreshed: #{map_size(features)} available")
    # Notify your application about new features
    MyApp.FeatureNotifier.features_updated(features)
  end
) do
  {:ok, :initialized} -> # Features loaded successfully
    # Start using GrowthBook...
  {:error, reason} -> # Handle error
    # Handle initialization failure...
end

# Create context that automatically uses latest features
context = GrowthBook.build_context(%{
  "id" => "12345",
  "country_code" => "NL",
  "browser" => "chrome"
})

# Features will automatically refresh in the background
# All feature evaluations will use the latest features
if GrowthBook.feature(context, "send-reminder").on? do
  Logger.info("Sending reminder")
end

Refresh Strategies

The SDK supports two refresh strategies:

  • :periodic (default): Features are automatically refreshed in the background based on the TTL.
  • :manual: Features are only refreshed when explicitly called with GrowthBook.FeatureRepository.refresh().

Shutting Down

When your application is shutting down or you no longer need the feature repository, stop it properly to release resources:

# Stops the FeatureRepository GenServer
GenServer.stop(GrowthBook.FeatureRepository)

Supervision

Since the FeatureRepository is a GenServer, it's recommended to add it to your application's supervision tree for proper lifecycle management:

# In your application.ex
def start(_type, _args) do
  children = [
    # Other children...
    
    # Add GrowthBook FeatureRepository to supervision tree
    {GrowthBook.FeatureRepository, 
      client_key: "key_prod_123...",
      api_host: "https://cdn.growthbook.io",
      # Add other options as needed
    }
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
  
  # Then initialize GrowthBook
  case GrowthBook.FeatureRepository.await_initialization(GrowthBook.FeatureRepository, 5000) do
    :ok -> 
      Logger.info("GrowthBook features initialized successfully")
    {:error, reason} -> 
      Logger.error("Failed to initialize GrowthBook features: #{reason}")
  end
  
  {:ok, self()}
end

Key improvements:

  • Auto-refresh: Features automatically refresh from your GrowthBook API
  • Encryption: Support for encrypted feature payloads
  • Callbacks: Get notified when features are updated
  • Resilient: Keeps existing features on API errors
  • Configurable: Control refresh intervals and strategies

License

This library is MIT licensed. See the LICENSE file in this repository for details.

Link to this section Summary

Types

Bucket range

Feature key

Namespace

Functions

Build a context with the given attributes and features. If features are not provided, it will use the FeatureRepository to always get the latest features.

Determine feature state for a given context

Get a feature's value with a default fallback

Initialize GrowthBook with the feature repository configuration. Returns {:ok, :initialized} if initialization succeeds with features loaded, {:error, reason} otherwise.

Run an experiment for the given context

Link to this section Types

Specs

bucket_range() :: {float(), float()}

Bucket range

A tuple that describes a range of the numberline between 0 and 1.

The tuple has 2 parts, both floats - the start of the range and the end. For example:

{0.3, 0.7}

Specs

feature_key() :: String.t()

Feature key

A key for a feature. This is a string that references a feature.

Specs

init_result() :: {:ok, :initialized} | {:error, String.t()}

Specs

namespace() :: {String.t(), float(), float()}

Namespace

A tuple that specifies what part of a namespace an experiment includes. If two experiments are in the same namespace and their ranges don't overlap, they wil be mutually exclusive.

The tuple has 3 parts:

  1. The namespace id (String.t())
  2. The beginning of the range (float(), between 0 and 1)
  3. The end of the range (float(), between 0 and 1)

For example:

{"namespace1", 0, 0.5}

Link to this section Functions

Link to this function

build_context(attributes, features \\ nil)

View Source

Specs

build_context(map(), map() | nil) :: GrowthBook.Context.t()

Build a context with the given attributes and features. If features are not provided, it will use the FeatureRepository to always get the latest features.

Link to this function

feature(context, feature_id, path \\ [])

View Source

Specs

Determine feature state for a given context

This function takes a context and a feature key, and returns a GrowthBook.FeatureResult struct.

Link to this function

feature_value(context, feature_id, default)

View Source

Specs

feature_value(GrowthBook.Context.t(), feature_key(), term()) :: term()

Get a feature's value with a default fallback

This is a convenience function that calls GrowthBook.feature/2 and returns the value, or the provided default if the feature isn't found or its value is nil.

Examples

iex> GrowthBook.feature_value(context, "my-feature", false)
true

iex> GrowthBook.feature_value(context, "unknown-feature", "default")
"default"

Specs

init(Keyword.t()) :: init_result()

Initialize GrowthBook with the feature repository configuration. Returns {:ok, :initialized} if initialization succeeds with features loaded, {:error, reason} otherwise.

Options

  • :client_key - Required. The API client key
  • :api_host - Required. The GrowthBook API host
  • :decryption_key - Optional. Key for decrypting feature payloads
  • :swr_ttl_seconds - Optional. Cache TTL in seconds (default: 60)
  • :refresh_strategy - Optional. Either :periodic or :manual (default: :periodic)
  • :on_refresh - Optional. Function to call when features are refreshed
  • :initialization_timeout - Optional. Timeout in ms for initial feature fetch (default: 5000)
Link to this function

run(context, exp, feature_id \\ nil, path \\ [])

View Source

Run an experiment for the given context

This function takes a context and an experiment, and returns an GrowthBook.ExperimentResult struct.