Rust
Contents
Installation
Install the posthog-rs
crate by adding it to your Cargo.toml
.
Next, set up the client with your PostHog project key.
Blocking client
Our Rust SDK supports both blocking and async clients. The async client is the default and is recommended for most use cases.
If you need to use a synchronous client instead – like we do in our CLI –, you can opt into it by disabling the asynchronous feature on your Cargo.toml
file.
In blocking mode, calls to capture
and related methods will block until the PostHog event capture API returns – generally this is on the order of tens of milliseconds, but you may want to thread::spawn
a background thread when you send an event.
Capturing events
You can send custom events using capture
:
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:
Batching events
To capture multiple events at once, use batch()
:
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.
Feature flags are not supported yet in our community-maintained Rust SDK. However, you can integrate them into your project by using the PostHog API.