Elixir
View the full documentation on GitHub.
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
, andis_
prefix is replaced with a?
suffix - Instead of classes, a Context struct is used (similar to
%Plug.Conn{}
inplug
)
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