View Source Flagsmith.Client (flagsmith_engine v2.7.2)

Link to this section Summary

Functions

Return all flags from an environment as a list. If a Flagsmith.Schemas.Environment.t/0 is passed instead of a Flagsmith.Configuration.t/0 or list of options, then the flags are extracted from it.

Closes the client: sends its buffered events, waiting for at most timeout milliseconds (5 seconds by default), and stops the processes running for its environment key, polling the environment and sending events. Events not sent by then are dropped.

Sends the events tracked so far to Flagsmith without waiting for the flush interval, and returns once each of them has been delivered, dropped, or kept for the next flush.

Returns an :ok tuple containing a Flagsmith.Schemas.Environment.t/0 struct, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to.

Returns an :ok tuple containing a list of Flagsmith.Schemas.Flag.t/0 structs, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to.

Returns a Flagsmith.Schemas.Flag.t/0 by name, evaluated for an identity as get_identity_flags/4 does, and records which variant of the feature the identity was exposed to.

Returns a Flagsmith.Schemas.Flag.t/0 by name, or :not_found if the feature doesn't exist.

Returns a Flagsmith.Schemas.Flag.t/0 by name. If the feature doesn't exist, it returns :not_found by default or in case default_flag_handler has been set returns what the call to that function with the feature name returns.

Returns an :ok tuple containing a list of Flagsmith.Schemas.Flag.t/0 structs, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to. The flags are retrieved based on a user identifier so take into account segments and traits.

Returns the :enabled status of a feature by name, or :not_found if the feature doesn't exist.

Create a Flagsmith.Configuration.t/0 struct with the desired settings to use in requests. All settings are optional with exception of the :environment_key if not configured at the application level. Non specified options will assume defaults, or if set at the application level use that.

Sends an event to Flagsmith, e.g. a conversion for experiments to measure.

Sends a "$flag_exposure" event to Flagsmith, recording that an identity was shown a variant of a feature. get_experiment_flag/4 sends these by itself.

Link to this section Types

@type event_opts() :: [
  identifier: String.t(),
  value: event_value(),
  traits: [map() | Flagsmith.Schemas.Traits.Trait.t()],
  metadata: map()
]
@type event_value() :: String.t() | number() | boolean() | nil
@type tesla_header_list() :: [{String.t(), String.t()}]

Link to this section Functions

Link to this function

all_flags(config_or_opts_or_env_or_flags \\ [])

View Source
@spec all_flags(config_or_env()) :: [Flagsmith.Schemas.Flag.t()]

Return all flags from an environment as a list. If a Flagsmith.Schemas.Environment.t/0 is passed instead of a Flagsmith.Configuration.t/0 or list of options, then the flags are extracted from it.

Link to this function

close(configuration_or_opts, timeout \\ 5000)

View Source
@spec close(Flagsmith.Configuration.t() | Keyword.t(), timeout :: non_neg_integer()) ::
  :ok | :noop | {:error, term()}

Closes the client: sends its buffered events, waiting for at most timeout milliseconds (5 seconds by default), and stops the processes running for its environment key, polling the environment and sending events. Events not sent by then are dropped.

Using the client again starts them again.

@spec flush_events(Flagsmith.Configuration.t() | Keyword.t()) ::
  :ok | :noop | {:error, term()}

Sends the events tracked so far to Flagsmith without waiting for the flush interval, and returns once each of them has been delivered, dropped, or kept for the next flush.

Sending a batch of events is attempted up to 3 times, giving up on an attempt after 3 seconds and backing off from 1 second, doubling, with full jitter. A batch the events API rejects is dropped, and one it keeps failing is kept for the next flush, ahead of the events tracked since. An events API refusing the environment key stops event tracking. Up to 1000 events are buffered: when full, the oldest one is dropped.

Link to this function

get_environment(configuration_or_opts \\ [])

View Source
@spec get_environment(Flagsmith.Configuration.t() | Keyword.t()) ::
  {:ok, Flagsmith.Schemas.Environment.t()} | {:error, term()}

Returns an :ok tuple containing a Flagsmith.Schemas.Environment.t/0 struct, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to.

Passing a Flagsmith.Configuration.t/0 or options with :enable_local_evaluation as true will start a local process for the given api key used, if one is not started yet, which requires you to be running the Flagsmith.Supervisor.

Link to this function

get_environment_flags(configuration_or_env_or_opts \\ [])

View Source
@spec get_environment_flags(config_or_env()) ::
  {:ok, Flagsmith.Schemas.Flags.t()} | {:error, term()}

Returns an :ok tuple containing a list of Flagsmith.Schemas.Flag.t/0 structs, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to.

Passing a Flagsmith.Configuration.t/0 or options with :enable_local_evaluation as true will start a local process for the given api key used, if one is not started yet, which requires you to be running the Flagsmith.Supervisor.

Link to this function

get_experiment_flag(configuration_or_opts, feature_name, identifier, traits \\ [])

View Source
@spec get_experiment_flag(
  Flagsmith.Configuration.t() | Keyword.t(),
  feature_name :: String.t(),
  identifier :: String.t(),
  [map() | Flagsmith.Schemas.Traits.Trait.t()]
) :: Flagsmith.Schemas.Flag.t() | :not_found | term()

Returns a Flagsmith.Schemas.Flag.t/0 by name, evaluated for an identity as get_identity_flags/4 does, and records which variant of the feature the identity was exposed to.

The exposure is sent as a "$flag_exposure" event, valued with the variant the identity was served, only when the flag is enabled and the identity is enrolled in a running experiment on the feature.

Requires :enable_events to be true and the Flagsmith.Supervisor to be running. Returns what get_flag/2 returns for the identity's flags, or an :error tuple if unable to get them.

Link to this function

get_feature_value(configuration_or_env_or_opts \\ [], feature_name)

View Source
@spec get_feature_value(config_or_env(), feature_name :: String.t()) ::
  :not_found | term()

Returns a Flagsmith.Schemas.Flag.t/0 by name, or :not_found if the feature doesn't exist.

If a Flagsmith.Schemas.Environment.t/0 is passed instead of a Flagsmith.Configuration.t/0 or list of options, then the feature is looked up in that environment, otherwise a local evaluation or api call is executed according to the configuration or passed options.

Link to this function

get_flag(configuration_or_env_or_opts \\ [], feature_name)

View Source
@spec get_flag(
  config_or_env() | Flagsmith.Schemas.Flags.t(),
  feature_name :: String.t()
) :: Flagsmith.Schemas.Flag.t() | :not_found | term()

Returns a Flagsmith.Schemas.Flag.t/0 by name. If the feature doesn't exist, it returns :not_found by default or in case default_flag_handler has been set returns what the call to that function with the feature name returns.

If a Flagsmith.Schemas.Environment.t/0 is passed instead of a Flagsmith.Configuration.t/0 or list of options, then the feature is looked up in that environment, otherwise a local evaluation or api call is executed according to the configuration or passed options.

Link to this function

get_identity_flags(configuration_or_opts \\ [], identifier, traits, transient \\ false)

View Source
@spec get_identity_flags(
  Flagsmith.Configuration.t() | Keyword.t(),
  String.t(),
  [map() | Flagsmith.Schemas.Traits.Trait.t()],
  boolean()
) :: {:ok, Flagsmith.Schemas.Flags.t()} | {:error, term()}

Returns an :ok tuple containing a list of Flagsmith.Schemas.Flag.t/0 structs, either from the local evaluation or API depending on the configuration used, or an :error tuple if unable to. The flags are retrieved based on a user identifier so take into account segments and traits.

Note: when using local evaluation there's no way to update the traits, the traits passed on to this function are used to check any segment rule specified on the Flagsmith.Schemas.Environment.t/0 you're accessing. On the other hand, when using the live API evaluation the traits you pass will be used to update the traits associated with the identity you're specifying.

Passing a Flagsmith.Configuration.t/0 or options with :enable_local_evaluation as true will start a local process for the given api key used, if one is not started yet, which requires you to be running the Flagsmith.Supervisor.

Link to this function

is_feature_enabled(configuration_or_env_or_opts \\ [], feature_name)

View Source
@spec is_feature_enabled(config_or_env(), feature_name :: String.t()) ::
  boolean() | :not_found | term()

Returns the :enabled status of a feature by name, or :not_found if the feature doesn't exist.

If a Flagsmith.Schemas.Environment.t/0 is passed instead of a Flagsmith.Configuration.t/0 or list of options, then the feature is evaluated from that environment, otherwise a local evaluation or api call is executed according to the configuration or passed options.

Create a Flagsmith.Configuration.t/0 struct with the desired settings to use in requests. All settings are optional with exception of the :environment_key if not configured at the application level. Non specified options will assume defaults, or if set at the application level use that.

Link to this function

track_event(configuration_or_opts, event, opts \\ [])

View Source
@spec track_event(
  Flagsmith.Configuration.t() | Keyword.t(),
  event :: String.t(),
  event_opts()
) :: :ok | {:error, term()}

Sends an event to Flagsmith, e.g. a conversion for experiments to measure.

Options:

  • :identifier -> the identity the event happened to
  • :value -> a string, number or boolean, sent as a string
  • :traits -> the identity's traits, in the format get_identity_flags/4 takes
  • :metadata -> a map of additional data

Events are buffered and sent in batches, so this returns :ok without waiting for the event to be sent. Requires :enable_events to be true and the Flagsmith.Supervisor to be running. Event names starting with $ are reserved for Flagsmith's own events.

Link to this function

track_exposure_event(configuration_or_opts, feature_name, opts \\ [])

View Source
@spec track_exposure_event(
  Flagsmith.Configuration.t() | Keyword.t(),
  feature_name :: String.t(),
  event_opts()
) :: :ok | {:error, term()}

Sends a "$flag_exposure" event to Flagsmith, recording that an identity was shown a variant of a feature. get_experiment_flag/4 sends these by itself.

Takes the same options as track_event/3, with :value being the variant, and :identifier being required to reconcile the exposure with the identity's events. Exposures equal in feature, identifier and value are sent once per flush interval.