This library provides an Elixir HTTP client for PostHog. See the repository for more information.
Installation
This library was built by the community but it's being maintained by the PostHog core team since v1.0.0. Thank you to Nick Kezhaya for building it originally.
The package can be installed by adding posthog
to your list of dependencies in mix.exs
:
def deps do[{:posthog, "~> 1.1.0"}]end
Configuration
config :posthog,api_url: "https://us.i.posthog.com",api_key: "<ph_project_api_key>"# Optional configurationsconfig :posthog,json_library: Jason, # Default JSON parser (optional)enabled_capture: true, # Enable/disable PostHog tracking (optional, defaults to true)http_client: Posthog.HTTPClient.Hackney, # Default HTTP client (optional)http_client_opts: [ # HTTP client options (optional)timeout: 5_000, # Request timeout in milliseconds (default: 5_000)retries: 3, # Number of retries on failure (default: 3)retry_delay: 1_000 # Delay between retries in milliseconds (default: 1_000)]
Advanced configuration
The default JSON parser is Jason
but you can pass in a custom JSON library via the json_library
config.
The default HTTP client is Hackney
but you can pass in a custom HTTP client via the http_client
config. This client should implement the Posthog.HTTPClient
behaviour. For testing purposes, you'll likely want a mock HTTP client implementation.
You can refer to the library's README.md for more information on the advanced configuration options.
Development/Test mode
If you are running in development or test mode, you can pass in a enabled_capture: false
value to the config. This causes events to be dropped instead of sent to PostHog.
config :posthog,enabled_capture: false # Disable tracking in test mode
Capturing events
You can send custom events using capture
:
Posthog.capture("user_signed_up", distinct_id_of_the_user)
Tip: We recommend using a
[object] [verb]
format for your event names, where[object]
is the entity that the behavior relates to, and[verb]
is the behavior itself. For example,project created
,user signed up
, orinvite sent
.
Setting event properties
Optionally, you can include additional information with the event by including a properties object:
Posthog.capture("user_signed_up", distinct_id_of_the_user, %{login_type: "email",is_free_trial: true})
Batching events
To capture multiple events at once, use batch()
:
Posthog.batch([{"user_signed_up", distinct_id_of_the_user, %{}}])
Feature flags
PostHog's feature flags enable you to safely deploy and roll back new features as well as target specific users and groups with them.
Elixir supports a subset of what our feature flag API provides.
Boolean feature flags
is_my_flag_enabled = Posthog.feature_flag_enabled?("flag-key", "distinct_id_of_your_user")if is_my_flag_enabled# Do something differently for this user# Optional: fetch the payload{:ok, feature_flag} = Posthog.feature_flag("flag-key", "distinct_id_of_your_user")end
Multivariate feature flags
{:ok, feature_flag} = Posthog.feature_flag("flag-key", "distinct_id_of_your_user")# %Posthog.FeatureFlag{ name: "flag-key", payload: %{"variant-1" => "value-1", "variant-2" => "value-2"}, enabled: "variant-2" }if feature_flag.enabled == 'variant-2' # replace 'variant-2' with the key of your variant# Do something differently for this userend
Fetching all feature flags
Posthog.feature_flags("distinct_id_of_your_user"){:ok,%{"featureFlagPayloads" => %{"feature-1" => 1,"feature-2" => %{"variant-1" => "value-1", "variant-2" => "value-2"}},"featureFlags" => %{"feature-1" => true, "feature-2" => "variant-2"}}}
More documentation can be found in the repository.
Thanks
Thanks to nkezhaya for initially contributing this library. The library is maintained by the PostHog team since February 2025.