Posthog

This library provides an HTTP client for Posthog.

Installation

The package can be installed by adding posthog to your list of dependencies in mix.exs:

def deps do
[
{:posthog, "~> 0.3"}
]
end

Configuration

config :posthog,
api_url: "http://posthog.example.com",
api_key: "..."

You can pass in a :json_library key. The default JSON parser is Jason.

You can pass in a :version key. The default version is 3 (which is currently the latest).

Usage

Capturing events:

Posthog.capture("login", distinct_id: user.id)

Specifying additional headers:

Posthog.capture("login", [distinct_id: user.id], [headers: [{"x-forwarded-for", "127.0.0.1"}]])

Capturing multiple events:

Posthog.batch([{"login", [distinct_id: user.id], nil}])

Fetching all matched feature flags for identifier:

Posthog.feature_flags("distinct-id")
{:ok,
%{
"featureFlagPayloads" => %{
"feature-1" => 1,
"feature-2" => %{"variant-1" => "value-1", "variant-2" => "value-2"}
},
"featureFlags" => %{"feature-1" => true, "feature-2" => "variant-2"}
}}

Fetching match information for a feature flag:

# For boolean feature flags
Posthog.feature_flag("feature-1", "matching-id")
{:ok,
%Posthog.FeatureFlag{
name: "feature-1",
value: 1,
enabled: true
}}
Posthog.feature_flag("feature-1", "non-matching-id")
{:ok,
%Posthog.FeatureFlag{
name: "feature-1",
value: nil,
enabled: false
}}
# For multivariate feature flags
Posthog.feature_flag("feature-2", "distinct-id")
{:ok,
%Posthog.FeatureFlag{
name: "feature-2",
value: %{"variant-1" => "value-1", "variant-2" => "value-2"},
enabled: "variant-2"
}}
# For non-existent or disabled feature flags
{:error, :not_found}

Checking if feature flag is enabled:

Posthog.feature_flag_enabled?("feature-2", "distinct-id")
true