Packages
posthog
0.4.3
2.16.2
2.16.1
2.16.0
2.15.1
2.15.0
2.14.2
2.14.1
2.14.0
2.13.0
2.12.1
2.12.0
2.11.1
2.11.0
2.10.3
2.10.2
2.10.1
2.10.0
2.9.2
2.9.1
2.9.0
2.8.4
2.8.3
2.8.2
2.8.1
2.8.0
2.7.1
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.0
2.0.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0
Official PostHog Elixir SDK
Current section
Files
Jump to
Current section
Files
README.md
# PostHog Elixir Client
[](https://hex.pm/packages/posthog)
[](https://hexdocs.pm/posthog)
A powerful Elixir client for [PostHog](https://posthog.com), providing seamless integration with PostHog's analytics and feature flag APIs.
## Features
- Event Capture: Track user actions and custom events
- Feature Flags: Manage feature flags and multivariate tests
- Batch Processing: Send multiple events efficiently
- Custom Properties: Support for user, group, and person properties
- Flexible Configuration: Customizable JSON library and API version
## Installation
Add `posthog` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:posthog, "~> 0.3"}
]
end
```
## Configuration
Add your PostHog configuration to your application's config:
```elixir
# config/config.exs
config :posthog,
api_url: "https://app.posthog.com", # Or your self-hosted PostHog instance URL
api_key: "phc_your_project_api_key"
# Optional configurations
config :posthog,
json_library: Jason, # Default JSON parser (optional)
version: 3 # API version (optional, defaults to 3)
```
## Usage
### Capturing Events
Simple event capture:
```elixir
# Basic event
Posthog.capture("page_view", distinct_id: "user_123")
# Event with properties
Posthog.capture("purchase", [
distinct_id: "user_123",
properties: %{
product_id: "prod_123",
price: 99.99,
currency: "USD"
}
])
# Event with custom timestamp
Posthog.capture("signup_completed",
[distinct_id: "user_123"],
DateTime.utc_now()
)
# Event with custom headers
Posthog.capture("login",
[distinct_id: "user_123"],
[headers: [{"x-forwarded-for", "127.0.0.1"}]]
)
```
### Batch Processing
Send multiple events in a single request:
```elixir
events = [
{"page_view", [distinct_id: "user_123"], nil},
{"button_click", [distinct_id: "user_123", properties: %{button_id: "signup"}], nil}
]
Posthog.batch(events)
```
### Feature Flags
Get all feature flags for a user:
```elixir
{:ok, flags} = Posthog.feature_flags("user_123")
# Response format:
# %{
# "featureFlags" => %{"flag-1" => true, "flag-2" => "variant-b"},
# "featureFlagPayloads" => %{
# "flag-1" => true,
# "flag-2" => %{"color" => "blue", "size" => "large"}
# }
# }
```
Check specific feature flag:
```elixir
# Boolean feature flag
{:ok, flag} = Posthog.feature_flag("new-dashboard", "user_123")
# Returns: %Posthog.FeatureFlag{name: "new-dashboard", value: true, enabled: true}
# Multivariate feature flag
{:ok, flag} = Posthog.feature_flag("pricing-test", "user_123")
# Returns: %Posthog.FeatureFlag{
# name: "pricing-test",
# value: %{"price" => 99, "period" => "monthly"},
# enabled: "variant-a"
# }
# Quick boolean check
if Posthog.feature_flag_enabled?("new-dashboard", "user_123") do
# Show new dashboard
end
```
Feature flags with group properties:
```elixir
Posthog.feature_flags("user_123",
groups: %{company: "company_123"},
group_properties: %{company: %{industry: "tech"}},
person_properties: %{email: "user@example.com"}
)
```
## Local Development
We recommend using `asdf` to manage Elixir and Erlang versions:
```sh
# Install required versions
asdf install
# Install dependencies
mix deps.get
mix compile
```
### Troubleshooting
If you encounter WX library issues during Erlang installation:
```sh
# Disable WX during installation
export KERL_CONFIGURE_OPTIONS="--without-wx"
asdf install
```
To persist this setting, add it to your shell configuration file (`~/.bashrc`, `~/.zshrc`, or `~/.profile`).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.