# `PostHog.OpenFeature.Provider`
[🔗](https://github.com/posthog/posthog-elixir/blob/v2.18.0/lib/posthog/open_feature.ex#L2)

[OpenFeature](https://openfeature.dev) provider backed by PostHog feature flags.

Requires the optional `:open_feature` dependency:

    {:open_feature, "~> 0.1"}

## Usage

    {:ok, _provider} = OpenFeature.set_provider(%PostHog.OpenFeature.Provider{})
    client = OpenFeature.get_client()

    OpenFeature.Client.get_boolean_value(client, "new-dashboard", false,
      context: %{
        "targeting_key" => "user-123",
        "plan" => "enterprise",
        "groups" => %{"company" => "acme"},
        "group_properties" => %{"company" => %{"size" => 250}}
      }
    )

## Options

- `:supervisor_name` - PostHog instance to evaluate flags with. Defaults to
  `PostHog`.
- `:default_distinct_id` - distinct ID used when the evaluation context has
  no `targeting_key`. When unset, such evaluations fail with
  `:targeting_key_missing`.
- `:send_feature_flag_events` - whether evaluations fire
  `$feature_flag_called` events. Defaults to `true`.

## Evaluation context

Keys can be strings or atoms.

- `targeting_key` - used as the PostHog `distinct_id`.
- `groups` - map of group type to group key.
- `group_properties` - map of group type to group properties.
- Every other key is sent as a person property.

## Resolution

- Boolean flags resolve to whether the flag is enabled.
- String flags resolve to the variant key.
- Number flags resolve to the variant key parsed as a number, including
  unsigned hexadecimal integers such as `0x10`.
- Map flags resolve to the flag's JSON object or array payload. The upstream
  OpenFeature SDK requires a map default (`%{}`), even for array payloads;
  `get_map_value/4` can return a list when the payload is an array.

For string, number, and map reads, a disabled flag returns the default value,
unless it still carries a variant (or object/array payload, for maps). That
value is then returned, as in the Node and Python providers. Boolean reads
return the flag's enabled state, including `false` regardless of the default.
Reading an enabled flag whose value doesn't fit the requested type returns
the default value with `error_code: :type_mismatch`. Unknown flags return
`:flag_not_found`.

Resolution details include `flag_metadata["posthog_reason"]` when PostHog
supplies an evaluation reason. Off results use `:disabled` when the server
explicitly reports the `flag_disabled` reason code, otherwise `:default`.

The OpenFeature Elixir SDK has no error tuple for `:type_mismatch` or
`:targeting_key_missing`, so these are returned as resolution details with
`reason: :error` and run `after` hooks rather than `error` hooks.

# `t`

```elixir
@type t() :: %PostHog.OpenFeature.Provider{
  default_distinct_id: PostHog.distinct_id() | nil,
  domain: String.t() | nil,
  hooks: list(),
  name: String.t(),
  send_feature_flag_events: boolean(),
  state: atom(),
  supervisor_name: PostHog.supervisor_name()
}
```

PostHog OpenFeature provider.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
