Rust

Last updated:

|Edit this page
Which features are available in this library?
  • Event capture
  • User identification
  • Feature flags
  • Group analytics
  • Surveys
  • LLM observability
  • Error tracking

Installation

Warning: This is a community maintained crate, and is still under development. It is not officially supported by PostHog.

Install the posthog-rs crate by adding it to your Cargo.toml.

Cargo.toml
[dependencies]
posthog-rs = "0.2.0"

Next, set up the client with your PostHog project key.

Rust
let client = posthog_rs::client(env!("<ph_project_api_key>"));

Note: Currently, there is no way to customize the host that events are sent to, and we default to app.posthog.com

Capturing events

You can send custom events using capture:

Rust
let mut event = Event::new("user_signed_up", "distinct_id_of_the_user");
client.capture(event).unwrap();

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, or invite sent.

Setting event properties

Optionally, you can also include additional information in the event by setting the properties value:

Rust
let mut event = Event::new("user_signed_up", "distinct_id_of_the_user");
event.insert_prop("login_type", "email").unwrap();
event.insert_prop("is_free_trial", true).unwrap();
client.capture(event).unwrap();

Batching events

To capture multiple events at once, use batch():

Rust
let event1 = posthog_rs::Event::new("event 1", "distinct_id_of_user_A");
let event2 = posthog_rs::Event::new("event 2", "distinct_id_of_user_B");
client.capture_batch(vec![event1, event2]).unwrap();

Feature flags

PostHog's feature flags enable you to safely deploy and roll back new features.

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.

Questions? Ask Max AI.

It's easier than reading through 589 docs articles.

Community questions

Was this page useful?

Next article

.NET

⚠️ Warning: This is in beta and may break. This is an optional library you can install if you're working with .NET Core. It uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server side application that needs performance. Installation Capturing events Person profiles and properties The .NET SDK captures identified events by default. These create person profiles . To set…

Read next article