Posthog.Client (posthog v0.4.1)
Low-level HTTP client for interacting with PostHog's API.
This module handles the direct HTTP communication with PostHog's API endpoints. It provides functions for:
- Sending event capture requests
- Processing batch events
- Retrieving feature flag information
While this module can be used directly, it's recommended to use the higher-level
functions in the Posthog
module instead.
Configuration
The client uses the following configuration from your application:
config :posthog,
api_url: "https://app.posthog.com", # Required
api_key: "phc_your_project_api_key", # Required
json_library: Jason, # Optional (default: Jason)
version: 3 # Optional (default: 3)
API Endpoints
The client interacts with the following PostHog API endpoints:
/capture
- For sending individual and batch events/decide
- For retrieving feature flag information
Error Handling
All functions return a result tuple:
{:ok, response}
for successful requests{:error, response}
for failed requests with a response{:error, term()}
for other errors (network issues, etc.)
Examples
# Capture an event
Posthog.Client.capture("page_view", %{distinct_id: "user_123"})
# Send batch events
events = [
{"page_view", %{distinct_id: "user_123"}, nil},
{"click", %{distinct_id: "user_123"}, DateTime.utc_now()}
]
Posthog.Client.batch(events)
# Get feature flags
Posthog.Client.feature_flags("user_123", groups: %{team: "engineering"})
Summary
Types
Event name, can be either an atom or a binary string.
HTTP headers in the format expected by :hackney.
Options that can be passed to API requests.
Properties that can be attached to events or feature flag requests.
Response from the PostHog API. Contains the status code, headers, and parsed JSON body (if any).
Timestamp for events. Can be a DateTime, NaiveDateTime, or ISO8601 string.
Functions
Sends multiple events to PostHog in a single request.
Builds an event payload for the PostHog API.
Captures a single event in PostHog.
Retrieves feature flags for a given distinct ID.
Types
Event name, can be either an atom or a binary string.
HTTP headers in the format expected by :hackney.
@type opts() :: [ headers: headers(), groups: map(), group_properties: map(), person_properties: map(), timestamp: timestamp() ]
Options that can be passed to API requests.
:headers
- Additional HTTP headers to include in the request:groups
- Group properties for feature flag evaluation:group_properties
- Additional properties for groups:person_properties
- Properties for the person:timestamp
- Custom timestamp for events
Properties that can be attached to events or feature flag requests.
@type response() :: %{status: pos_integer(), headers: headers(), body: map() | nil}
Response from the PostHog API. Contains the status code, headers, and parsed JSON body (if any).
@type timestamp() :: String.t()
Timestamp for events. Can be a DateTime, NaiveDateTime, or ISO8601 string.
Functions
Sends multiple events to PostHog in a single request.
Parameters
events
- List of event tuples in the format{event_name, properties, timestamp}
opts
- Additional options (seeopts/0
)headers
- Additional HTTP headers
Examples
events = [
{"page_view", %{distinct_id: "user_123"}, nil},
{"click", %{distinct_id: "user_123", properties: %{button: "signup"}}, DateTime.utc_now()}
]
Posthog.Client.batch(events)
@spec build_event(event(), properties(), timestamp()) :: map()
Builds an event payload for the PostHog API.
Parameters
event
- The name of the eventproperties
- Event propertiestimestamp
- Optional timestamp for the event
Examples
build_event("page_view", %{distinct_id: "user_123"}, nil)
build_event("purchase", %{distinct_id: "user_123", price: 99.99}, DateTime.utc_now())
@spec capture(event(), properties(), opts() | timestamp()) :: {:ok, response()} | {:error, response() | term()}
Captures a single event in PostHog.
Parameters
event
- The name of the event (string or atom)params
- Event properties including:distinct_id
opts
- Additional options (seeopts/0
)
Examples
# Basic event
Posthog.Client.capture("page_view", %{distinct_id: "user_123"})
# Event with properties and timestamp
Posthog.Client.capture("purchase",
%{
distinct_id: "user_123",
properties: %{product_id: "123", price: 99.99}
},
timestamp: DateTime.utc_now()
)
# Event with custom headers
Posthog.Client.capture("login",
%{distinct_id: "user_123"},
headers: [{"x-forwarded-for", "127.0.0.1"}]
)
@spec feature_flags(binary(), opts()) :: {:ok, Posthog.FeatureFlag.flag_response()} | {:error, response() | term()}
Retrieves feature flags for a given distinct ID.
Parameters
distinct_id
- The unique identifier for the useropts
- Additional options (seeopts/0
)
Examples
# Basic feature flags request
Posthog.Client.feature_flags("user_123")
# With group properties
Posthog.Client.feature_flags("user_123",
groups: %{company: "company_123"},
group_properties: %{company: %{industry: "tech"}}
)