Warning: This is a community maintained crate, and is still under development. It is not officially supported by PostHog.
posthog-rs crate by adding it to your
[dependencies]posthog-rs = "0.2.0"
Next, set up the client with your PostHog project key.
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
You can send custom events using
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,
user signed up, or
Setting event properties
Optionally, you can also include additional information in the event by setting the properties value:
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();
To capture multiple events at once, use
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();
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.