View Source GrowthBook.Context (GrowthBook v0.2.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

Features

Forced variations map

t()

Context

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() :: %{required(GrowthBook.feature_key()) => GrowthBook.Feature.t()}

Features

A map of %Feature{} structs. Keys are string ids for the features.

%{
  "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: features(),
  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 (features/0) - Feature definitions (usually pulled from an API or cache)
  • 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.