View Source GrowthBook.Condition (GrowthBook v0.3.0)

Functionality for evaluating conditions.

You should not (have to) use any of these functions in your own application. They are documented for library developers only. Breaking changes in this module will not be considered breaking changes in the library's public API (or cause a minor/major semver update).

Link to this section Summary

Types

A condition value

t()

Condition

Functions

Evaluates a condition against the given attributes.

Link to this section Types

Specs

condition_value() :: term()

A condition value

Specs

t() :: map()

Condition

A condition is evaluated against GrowthBook.Context.attributes/0 and used to target features/experiments to specific users.

The syntax is inspired by MongoDB queries. Here is an example:

%{
  "country" => "US",
  "browser" => %{
    "$in" => ["firefox", "chrome"]
  },
  "email" => %{
    "$not" => %{
      "$regex" => "@gmail.com$"
    }
  }
}

Link to this section Functions

Link to this function

eval_condition(attributes, conditions)

View Source

Specs

eval_condition(GrowthBook.Context.attributes(), t()) :: boolean()

Evaluates a condition against the given attributes.

Conditions are MongoDB-query-like expressions.

Available expressions:

Expression groups

  • $or: Logical OR
  • $nor: Logical OR, but inverted
  • $and: Logical AND
  • $not: Logical NOT

Simple expressions

  • $eq: left == right
  • $ne: left != right
  • $lt: left < right
  • $lte: left <= right
  • $gt: left > right
  • $gte: left >= right
  • $exists: (left in [nil, :undefined]) != right
  • $type: typeof left == right
  • $regex: right |> Regex.compile!() |> Regex.match?(left)

Array expressions

  • $in: left in right
  • $nin: left not in right
  • $elemMatch: performs the given condition(s) of left for each element of right (with support for expressions)
  • $all: performs the given condition(s) of left for each element of right (without support support for expressions)
  • $size: eval_contition_value(left, length(right))

Version comparison

  • $veq: versions are equal
  • $vne: versions are not equal
  • $vlt: the first version is lesser than the second version
  • $vlte: the first version is lesser than or equal to the second version
  • $vgt: the first version is greater than the second version
  • $vgte: the first version is greater than or equal to the second version

Examples

iex> GrowthBook.Condition.eval_condition(%{"hello" => "world"}, %{
...>   "hello" => "world"
...> })
true

iex> GrowthBook.Condition.eval_condition(%{"hello" => "world"}, %{
...>   "hello" => "optimizely"
...> })
false