PostHog.FeatureFlags.Evaluations (posthog v2.22.2)

Copy Markdown View Source

Snapshot of feature flag evaluations for a single distinct_id.

An Evaluations struct represents one point-in-time evaluation assembled locally and, when needed, from at most one /flags call. It is built by PostHog.FeatureFlags.evaluate_flags/2 and lets you branch on multiple flags and enrich captured events from the same frozen result set.

Each snapshot tracks which flags were accessed via enabled?/2 and get_flag/2 in an :atomics array that is garbage-collected together with the snapshot. No process is started and no manual cleanup is required, and a snapshot can be shared with other processes.

Querying

Use enabled?/2, get_flag/2, and get_flag_payload/2 to read individual flags. enabled?/2 and get_flag/2 record access and fire a $feature_flag_called event with full metadata (id, version, reason, request_id) on each call; get_flag_payload/2 does neither.

{:ok, snapshot} = PostHog.FeatureFlags.evaluate_flags("user-123")

if PostHog.FeatureFlags.Evaluations.enabled?(snapshot, "new-dashboard") do
  render_new_dashboard()
end

A flag that has a local definition but that neither local evaluation nor the /flags fallback resolved is absent from the snapshot. Its key and the reason are in the unresolved_flags field, so you can tell it apart from a flag that does not exist:

snapshot.unresolved_flags
#=> %{"checkout" => :experience_continuity}

Enriching captures

Call PostHog.FeatureFlags.set_in_context/2 to copy the snapshot's $feature/<key> and $active_feature_flags properties into the per-process context. Any subsequent PostHog.capture/3 automatically picks them up — no additional /flags request, and the values match what you branched on.

PostHog.FeatureFlags.set_in_context(snapshot)
PostHog.capture("page_viewed", %{distinct_id: "user-123"})

Or merge event_properties/1 directly into a capture's properties for an explicit, one-off attach without touching context.

Filtering

Use only_accessed/1 to narrow a snapshot to flags accessed so far via enabled?/2 or get_flag/2. Use only/2 to narrow by an explicit key list. Both return a fresh snapshot with its own access tracker — calls on the filtered view do not back-propagate to the parent.

narrowed = PostHog.FeatureFlags.Evaluations.only_accessed(snapshot)
PostHog.FeatureFlags.set_in_context(narrowed)

Summary

Types

t()

Snapshot of evaluated flags for a single distinct_id.

Why local evaluation could not resolve a flag.

Functions

Returns the sorted list of keys present in this snapshot that were accessed via enabled?/2 or get_flag/2.

Returns whether the named flag is enabled in this snapshot.

Returns the $feature/<key> and $active_feature_flags properties for this snapshot, suitable for merging into a captured event's properties.

Returns the variant string, the enabled boolean, or nil for unknown flags.

Returns the configured payload for the flag, or nil for unknown flags or flags without a payload.

Returns the sorted list of flag keys present in the snapshot.

Returns a copy of the snapshot scoped to the given keys. Unknown keys are silently dropped.

Returns a copy of the snapshot scoped to the flags accessed so far via enabled?/2 or get_flag/2.

Types

t()

@type t() :: %PostHog.FeatureFlags.Evaluations{
  accessed: accessed(),
  accessed_pid: nil,
  distinct_id: PostHog.distinct_id(),
  errors_while_computing: boolean(),
  evaluated_at: integer() | nil,
  flags: %{required(String.t()) => PostHog.FeatureFlags.Result.t()},
  groups: %{required(String.t()) => String.t()},
  request_id: String.t() | nil,
  supervisor_name: PostHog.supervisor_name(),
  unresolved_flags: %{required(String.t()) => unresolved_reason()}
}

Snapshot of evaluated flags for a single distinct_id.

  • :supervisor_name - PostHog instance the snapshot was produced from; used when enabled?/2 and get_flag/2 fire $feature_flag_called events.
  • :distinct_id - resolved distinct ID the local and/or remote evaluation was performed for. "" for the empty fallback returned when no distinct_id could be resolved; events are short-circuited in that case.
  • :flags - map of flag key to PostHog.FeatureFlags.Result.t/0.
  • :groups - normalized group type/key context used for evaluation and $feature_flag_called deduplication.
  • :request_id - request ID returned when remote fallback was used.
  • :evaluated_at - server-side evaluation timestamp when remote fallback was used.
  • :errors_while_computing - whether the response signaled errorsWhileComputingFlags. When true, every event fired from this snapshot includes errors_while_computing_flags in its $feature_flag_error property.
  • :unresolved_flags - map of flag key to unresolved_reason/0 for each flag that has a loaded local definition but that neither local evaluation nor the /flags fallback resolved. These flags are absent from :flags. Reading one with enabled?/2 or get_flag/2 reports local_evaluation_inconclusive instead of flag_missing. Reading this field records no access and sends no event. Always empty on snapshots returned by only/2 and only_accessed/1.
  • :accessed - internal. Its contents may change in any release without notice. Don't read or match on it; use accessed/1.
  • :accessed_pid - internal and deprecated. Always nil. It held the pid of an internal Agent that tracked accessed keys and was never meant to be used directly. Snapshots no longer start a process, so there is nothing to stop or clean up, and the process leak this Agent caused no longer happens. Kept only so existing code that reads or matches the field still compiles. Don't use it; it will be removed in the next major version.

unresolved_reason()

@type unresolved_reason() ::
  :experience_continuity
  | :unsupported_definition
  | :unresolved_dependency
  | :missing_context

Why local evaluation could not resolve a flag.

  • :experience_continuity - the flag has experience continuity enabled, which local evaluation never resolves. Disable it to evaluate the flag locally.
  • :unsupported_definition - the flag uses something local evaluation does not support, such as a static cohort, an unknown operator, a malformed value, or a missing or circular flag dependency. Change the flag or upgrade the SDK.
  • :unresolved_dependency - a flag this flag depends on has a loaded definition but was not resolved.
  • :missing_context - the call did not pass a property, group key, group property, or device id the flag needs, or passed it in a form local evaluation cannot use.

When several causes apply, the reason is the first in this list.

Functions

accessed(evaluations)

@spec accessed(t()) :: [String.t()]

Returns the sorted list of keys present in this snapshot that were accessed via enabled?/2 or get_flag/2.

Keys that were accessed but are absent from the snapshot are not included.

enabled?(snapshot, key)

@spec enabled?(t(), String.t()) :: boolean()

Returns whether the named flag is enabled in this snapshot.

Returns false for unknown flags. Records the access. Fires a $feature_flag_called event with full metadata when the flag is present, or with $feature_flag_error: "flag_missing" and $feature_flag_response: nil when it is not. The error is local_evaluation_inconclusive instead when the flag is in unresolved_flags.

event_properties(evaluations)

@spec event_properties(t()) :: PostHog.properties()

Returns the $feature/<key> and $active_feature_flags properties for this snapshot, suitable for merging into a captured event's properties.

  • $feature/<key> is set to the variant string when present, or to the enabled boolean otherwise. Disabled flags are included with false.
  • $active_feature_flags is the sorted list of keys whose flag is enabled.

Examples

properties = PostHog.FeatureFlags.Evaluations.event_properties(snapshot)
PostHog.capture("page_viewed", Map.merge(%{distinct_id: "u1"}, properties))

get_flag(snapshot, key)

@spec get_flag(t(), String.t()) :: String.t() | boolean() | nil

Returns the variant string, the enabled boolean, or nil for unknown flags.

Records the access. Fires a $feature_flag_called event with full metadata when the flag is present, or with $feature_flag_error: "flag_missing" and $feature_flag_response: nil when it is not. The error is local_evaluation_inconclusive instead when the flag is in unresolved_flags.

get_flag_payload(evaluations, key)

@spec get_flag_payload(t(), String.t()) :: any() | nil

Returns the configured payload for the flag, or nil for unknown flags or flags without a payload.

Does not record access for only_accessed/1 or fire a $feature_flag_called event.

keys(evaluations)

@spec keys(t()) :: [String.t()]

Returns the sorted list of flag keys present in the snapshot.

only(snapshot, keys)

@spec only(t(), [String.t()]) :: t()

Returns a copy of the snapshot scoped to the given keys. Unknown keys are silently dropped.

The returned snapshot has its own access tracker. Calls on it do not back-propagate to the parent.

only_accessed(snapshot)

@spec only_accessed(t()) :: t()

Returns a copy of the snapshot scoped to the flags accessed so far via enabled?/2 or get_flag/2.

Returns an empty snapshot when nothing has been accessed yet — including no flags would be more surprising than helpful, since the caller asked for "only what I touched."

The returned snapshot has its own access tracker. Calls on it do not back-propagate to the parent.