Rust

Last updated:

|Edit this page
Which features are available in this library?
  • Event capture
  • Autocapture
  • User identification
  • Session recording
  • Feature flags
  • Group analytics

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?

Was this page useful?