Rust

Last updated:

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

Warning: This crate is still under development and is not recommended for production use yet.

Installation

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

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

Usage

To setup the client, all you need to do is pass your PostHog project key.

Rust
let client = posthog_rs::client(env!("POSTHOG_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

Currently, the only functionality this library supports is sending events, which can be done using the capture and cature_batch methods.

Rust
let mut event = Event::new("<event_name>", "<distinct_id>");
event.insert_prop("key1", "value1").unwrap();
event.insert_prop("key2", vec!["a", "b"]).unwrap();
client.capture(event).unwrap();
Rust
let event1 = posthog_rs::Event::new("event 1", "1234");
let event2 = posthog_rs::Event::new("event 2", "1234");
client.capture_batch(vec![event1, event2]).unwrap();

Questions?

Was this page useful?

Next article

Library comparison

PostHog provides a number of both official and community maintained libraries to help you easily integrate with your preferred language or framework. This document outlines all of our current client-side and server-side libraries, as well as which features each of them currently supports. For information on how to send events using these libraries, check out this guide . Note: Session recording and autocapture are not possible in server libraries.

Read next article