View Source GrowthBook (GrowthBook v0.2.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
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.Context{
  enabled?: true,
  features: features,
  attributes: %{
    "id" => "12345",
    "country_code" => "NL",
    "browser" => "chrome"
  }
}

# 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

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

Determine feature state for a given context

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

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

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

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.