Posthog.FeatureFlag (posthog v1.1.0)

View Source

Represents 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 flag
  • payload - 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.

t()

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

flag_response()

@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.

properties()

@type properties() :: %{optional(binary()) => term()}

A map of properties that can be associated with a feature flag.

t()

@type t() :: %Posthog.FeatureFlag{enabled: variant(), name: binary(), payload: term()}

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)

variant()

@type variant() :: binary() | boolean()

Represents the enabled state of a feature flag. Can be either a boolean for on/off flags or a string for multivariate flags.

Functions

boolean?(feature_flag)

@spec boolean?(t()) :: boolean()

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

decode_payload(payload)

@spec decode_payload(term()) :: term()

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

decode_payloads(payloads)

@spec decode_payloads(%{optional(binary()) => term()}) :: %{
  optional(binary()) => term()
}

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"
# }

new(name, enabled, payload)

@spec new(binary(), variant(), term()) :: t()

Creates a new FeatureFlag struct.

Parameters

  • name - The name of the feature flag
  • enabled - 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"})

process_response(response)

@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"
# }