View Source GrowthBook.Context (GrowthBook v0.3.0)

Stores feature and experiment context.

Holds the state of features, attributes and other "global" state. The context works similar to %Plug.Conn{}, as it is created for each request and passed along when working with features and experiments.

Link to this section Summary

Types

Attributes

Function that returns the latest features - a map of %Feature{} structs. Keys are string ids for the features. The function will be called each time features are needed, ensuring the latest features are used.

Forced variations map

t()

Context

Functions

Gets the latest features from the provider function

Link to this section Types

Specs

attributes() :: %{required(String.t()) => term()}

Attributes

Attributes are an arbitrary JSON map containing user and request attributes. Here's an example:

%{
  "id" => "123",
  "anonId" => "abcdef",
  "company" => "growthbook",
  "url" => "/pricing",
  "country" => "US",
  "browser" => "firefox",
  "age" => 25,
  "beta" => true,
  "account" => %{
    "plan" => "team",
    "seats" => 10
  }
}

Specs

features_provider() :: (-> %{
                             required(GrowthBook.feature_key()) =>
                               GrowthBook.Feature.t()
                           })

Function that returns the latest features - a map of %Feature{} structs. Keys are string ids for the features. The function will be called each time features are needed, ensuring the latest features are used.

The returned map should be in this format:

%{
  "feature-1" => %Feature{
    default_value: false
  },
  "my_other_feature" => %Feature{
    default_value: 1,
    rules: [
      %FeatureRule{
        force: 2
      }
    ]
  }
}

Specs

forced_variations() :: %{required(GrowthBook.feature_key()) => integer()}

Forced variations map

A hash or map that forces an GrowthBook.Experiment to always assign a specific variation. Useful for QA.

Keys are the experiment key, values are the list index of the variation. For example:

%{
  "my-test" => 0,
  "other-test" => 1
}

Specs

t() :: %GrowthBook.Context{
  attributes: attributes() | nil,
  enabled?: boolean(),
  features:
    %{required(GrowthBook.feature_key()) => GrowthBook.Feature.t()} | nil,
  features_provider: features_provider(),
  forced_variations: forced_variations(),
  qa_mode?: boolean(),
  url: String.t() | nil
}

Context

Context struct. Has a number of optional properties:

  • enabled? (boolean/0) - Switch to globally disable all experiments. Default true.
  • attributes (attributes/0) - Map of user attributes that are used to assign variations
  • url (String.t/0) - The URL of the current page
  • features_provider (features_provider/0) - Function that returns latest features
  • forced_variations (forced_variations/0) - Force specific experiments to always assign a specific variation (used for QA)
  • qa_mode? (boolean/0) - If true, random assignment is disabled and only explicitly forced variations are used.

Link to this section Functions

Specs

get_features(t()) :: %{
  required(GrowthBook.feature_key()) => GrowthBook.Feature.t()
}

Gets the latest features from the provider function