Posthog.FeatureFlag (posthog v1.1.0)
View SourceRepresents a PostHog feature flag with its evaluation state.
This module provides a struct and helper functions for working with PostHog feature flags. Feature flags can be either boolean (on/off) or multivariate (multiple variants).
Structure
The FeatureFlag
struct contains:
name
- The name of the feature flagpayload
- The payload value associated with the flag (can be any term)enabled
- The evaluation result (boolean for on/off flags, string for multivariate flags)
Examples
# Boolean feature flag
%Posthog.FeatureFlag{
name: "new-dashboard",
payload: true,
enabled: true
}
# Multivariate feature flag
%Posthog.FeatureFlag{
name: "pricing-test",
payload: %{"price" => 99, "period" => "monthly"},
enabled: "variant-a"
}
Summary
Types
The response format from PostHog's feature flag API. Contains both the flag states and their associated payloads.
A map of properties that can be associated with a feature flag.
The FeatureFlag struct type.
Represents the enabled state of a feature flag. Can be either a boolean for on/off flags or a string for multivariate flags.
Functions
Checks if a feature flag is a boolean flag.
Decodes a feature flag payload from JSON string to Elixir term. Returns the original payload if it's not a valid JSON string.
Decodes a map of feature flag payloads.
Creates a new FeatureFlag struct.
Processes a feature flag response from the PostHog API. Handles both v3 and v4 API response formats.
Types
@type flag_response() :: %{ feature_flags: %{optional(binary()) => variant()}, feature_flag_payloads: %{optional(binary()) => term()} }
The response format from PostHog's feature flag API. Contains both the flag states and their associated payloads.
A map of properties that can be associated with a feature flag.
The FeatureFlag struct type.
Fields:
name
- The name of the feature flag (string)payload
- The payload value associated with the flag (any term)enabled
- The evaluation result (boolean or string)
Represents the enabled state of a feature flag. Can be either a boolean for on/off flags or a string for multivariate flags.
Functions
Checks if a feature flag is a boolean flag.
Returns true
if the flag is a boolean (on/off) flag, false
if it's a multivariate flag.
Examples
flag = Posthog.FeatureFlag.new("new-dashboard", true, true)
Posthog.FeatureFlag.boolean?(flag)
# Returns: true
flag = Posthog.FeatureFlag.new("pricing-test", "variant-a", %{})
Posthog.FeatureFlag.boolean?(flag)
# Returns: false
Decodes a feature flag payload from JSON string to Elixir term. Returns the original payload if it's not a valid JSON string.
Examples
# JSON string payload
Posthog.FeatureFlag.decode_payload("{"color": "blue"}")
# Returns: %{"color" => "blue"}
# Non-JSON string payload
Posthog.FeatureFlag.decode_payload("plain-text")
# Returns: "plain-text"
# Nil payload
Posthog.FeatureFlag.decode_payload(nil)
# Returns: nil
Decodes a map of feature flag payloads.
Parameters
payloads
- Map of feature flag names to their payload values
Examples
payloads = %{
"my-flag" => "{"color": "blue"}",
"other-flag" => "plain-text"
}
Posthog.FeatureFlag.decode_payloads(payloads)
# Returns: %{
# "my-flag" => %{"color" => "blue"},
# "other-flag" => "plain-text"
# }
Creates a new FeatureFlag struct.
Parameters
name
- The name of the feature flagenabled
- The evaluation result (boolean or string)payload
- The payload value associated with the flag
Examples
# Create a boolean feature flag
Posthog.FeatureFlag.new("new-dashboard", true, true)
# Create a multivariate feature flag
Posthog.FeatureFlag.new("pricing-test", "variant-a",
%{"price" => 99, "period" => "monthly"})
@spec process_response(map()) :: %{ flags: map() | nil, feature_flags: %{optional(binary()) => variant()}, feature_flag_payloads: %{optional(binary()) => term()}, request_id: binary() | nil }
Processes a feature flag response from the PostHog API. Handles both v3 and v4 API response formats.
Parameters
response
- The raw response from the API
Examples
# v4 API response
response = %{
"flags" => %{
"my-flag" => %{
"enabled" => true,
"variant" => nil,
"metadata" => %{"payload" => "{"color": "blue"}"}
}
},
"requestId" => "123"
}
Posthog.FeatureFlag.process_response(response)
# Returns: %{
# flags: %{"my-flag" => %{"enabled" => true, "variant" => nil, "metadata" => %{"payload" => "{"color": "blue"}"}}},
# feature_flags: %{"my-flag" => true},
# feature_flag_payloads: %{"my-flag" => %{"color" => "blue"}},
# request_id: "123"
# }
# v3 API response
response = %{
"featureFlags" => %{"my-flag" => true},
"featureFlagPayloads" => %{"my-flag" => "{"color": "blue"}"},
"requestId" => "123"
}
Posthog.FeatureFlag.process_response(response)
# Returns: %{
# feature_flags: %{"my-flag" => true},
# feature_flag_payloads: %{"my-flag" => %{"color" => "blue"}},
# request_id: "123"
# }