PostHog.FeatureFlags.Result (posthog v2.9.1)
View SourceRepresents the result of a feature flag evaluation.
This struct contains all the information returned when evaluating a feature flag:
key- The name of the feature flagenabled- Whether the flag is enabled for this uservariant- The variant assigned to this user (nil for boolean flags)payload- The JSON payload configured for this flag/variant (nil if not set)id- Numeric flag ID from the PostHog backend (when available)version- Flag version from the PostHog backend (when available)reason- Reason map describing why this evaluation produced its valuerequest_id- Request ID returned by the/flagsendpoint (useful for experiment exposure tracking)evaluated_at- Server-side evaluation timestamp from the responseerrors_while_computing- Whether the response signalederrorsWhileComputingFlags; values for some flags may be incomplete or stale. Forwarded as$feature_flag_error: "errors_while_computing_flags"on$feature_flag_calledevents.
The metadata fields are populated when the /flags response includes them
and are forwarded as $feature_flag_id, $feature_flag_version, $feature_flag_reason,
$feature_flag_request_id, and $feature_flag_evaluated_at properties on
$feature_flag_called events.
Examples
# Boolean flag result
%PostHog.FeatureFlags.Result{
key: "my-feature",
enabled: true,
variant: nil,
payload: nil
}
# Multivariant flag result with payload and metadata
%PostHog.FeatureFlags.Result{
key: "my-experiment",
enabled: true,
variant: "control",
payload: %{"button_color" => "blue"},
id: 154_429,
version: 4,
reason: %{"code" => "condition_match", "description" => "Matched condition set 1"},
request_id: "0d23f243-399a-4904-b1a8-ec2037834b72",
evaluated_at: 1_234_567_890
}
Summary
Types
JSON-compatible value used for feature flag payloads.
Result for a single evaluated feature flag.
Functions
Returns the value of the feature flag result.
Types
@type json() :: String.t() | number() | boolean() | nil | [json()] | %{required(String.t()) => json()}
JSON-compatible value used for feature flag payloads.
@type t() :: %PostHog.FeatureFlags.Result{ enabled: boolean(), errors_while_computing: boolean(), evaluated_at: integer() | nil, id: integer() | nil, key: String.t(), payload: json(), reason: map() | nil, request_id: String.t() | nil, variant: String.t() | nil, version: integer() | nil }
Result for a single evaluated feature flag.
The struct fields mirror the data returned by PostHog's /flags endpoint and
the metadata emitted on $feature_flag_called events.
Functions
Returns the value of the feature flag result.
If a variant is present, returns the variant string. Otherwise, returns the enabled boolean status. This provides backwards compatibility with existing code that expects a simple value from feature flag checks.
Examples
iex> result = %PostHog.FeatureFlags.Result{key: "flag", enabled: true, variant: "control", payload: nil}
iex> PostHog.FeatureFlags.Result.value(result)
"control"
iex> result = %PostHog.FeatureFlags.Result{key: "flag", enabled: true, variant: nil, payload: nil}
iex> PostHog.FeatureFlags.Result.value(result)
true
iex> result = %PostHog.FeatureFlags.Result{key: "flag", enabled: false, variant: nil, payload: nil}
iex> PostHog.FeatureFlags.Result.value(result)
false