Skip to main content

posthog_rs/
lib.rs

1//! Official Rust SDK for PostHog.
2//!
3//! Use [`client`] to construct a [`Client`], [`Event`] to capture analytics
4//! events, and [`Client::evaluate_flags`] with [`EvaluateFlagsOptions`] for
5//! feature flag evaluation.
6//!
7//! See the [PostHog Rust SDK documentation](https://posthog.com/docs/libraries/rust)
8//! for installation, configuration, and more examples.
9//!
10//! # Getting started
11//!
12//! Add `posthog-rs` to your `Cargo.toml`, then initialize a client with your
13//! project API key.
14//!
15//! ```no_run
16//! use posthog_rs::{client, EvaluateFlagsOptions, Event};
17//!
18//! #[cfg(feature = "async-client")]
19//! #[tokio::main]
20//! async fn main() -> Result<(), posthog_rs::Error> {
21//!     let api_key = std::env::var("POSTHOG_API_KEY")
22//!         .expect("set POSTHOG_API_KEY to your PostHog project API key");
23//!
24//!     let posthog = client(api_key.as_str()).await;
25//!     let distinct_id = "user-123";
26//!
27//!     // Capture an analytics event.
28//!     let mut event = Event::new("signed_up", distinct_id);
29//!     event.insert_prop("plan", "pro")?;
30//!     posthog.capture(event);
31//!
32//!     // Evaluate feature flags once, then read from the snapshot.
33//!     let flags = posthog
34//!         .evaluate_flags(distinct_id, EvaluateFlagsOptions::default())
35//!         .await?;
36//!
37//!     if flags.is_enabled("new-onboarding") {
38//!         let mut event = Event::new("onboarding_step_completed", distinct_id);
39//!         event.with_flags(&flags.only_accessed());
40//!         posthog.capture(event);
41//!     }
42//!
43//!     Ok(())
44//! }
45//!
46//! #[cfg(not(feature = "async-client"))]
47//! fn main() -> Result<(), posthog_rs::Error> {
48//!     let api_key = std::env::var("POSTHOG_API_KEY")
49//!         .expect("set POSTHOG_API_KEY to your PostHog project API key");
50//!
51//!     let posthog = client(api_key.as_str());
52//!     let distinct_id = "user-123";
53//!
54//!     // Capture an analytics event.
55//!     let mut event = Event::new("signed_up", distinct_id);
56//!     event.insert_prop("plan", "pro")?;
57//!     posthog.capture(event);
58//!
59//!     // Evaluate feature flags once, then read from the snapshot.
60//!     let flags = posthog.evaluate_flags(distinct_id, EvaluateFlagsOptions::default())?;
61//!
62//!     if flags.is_enabled("new-onboarding") {
63//!         let mut event = Event::new("onboarding_step_completed", distinct_id);
64//!         event.with_flags(&flags.only_accessed());
65//!         posthog.capture(event);
66//!     }
67//!
68//!     Ok(())
69//! }
70//! ```
71mod client;
72mod compression;
73mod constants;
74mod endpoints;
75mod error;
76#[cfg(feature = "error-tracking")]
77mod error_tracking;
78mod event;
79#[cfg(feature = "capture-v1")]
80mod event_v1;
81mod feature_flag_evaluations;
82mod feature_flags;
83mod global;
84mod local_evaluation;
85
86// Public interface - any change to this is breaking!
87// Client
88pub use client::client;
89pub use client::BeforeSendHook;
90pub use client::CaptureCompression;
91pub use client::Client;
92pub use client::ClientOptions;
93pub use client::ClientOptionsBuilder;
94pub use client::ClientOptionsBuilderError;
95pub use client::{CaptureFailure, FlagsFailure, LocalEvaluationFailure, PostHogError};
96
97// Endpoints
98pub use endpoints::{
99    Endpoint, EndpointManager, DEFAULT_HOST, EU_INGESTION_ENDPOINT, US_INGESTION_ENDPOINT,
100};
101
102// Error
103pub use error::Error;
104
105// Error Tracking
106#[cfg(feature = "error-tracking")]
107pub use error_tracking::{
108    CaptureExceptionOptions, ErrorTrackingOptions, ErrorTrackingOptionsBuilder,
109    ErrorTrackingOptionsBuilderError,
110};
111
112// Event
113pub use event::Event;
114
115// V1 Capture types
116#[cfg(feature = "capture-v1")]
117pub use event_v1::{CaptureResponse, EventResult, EventStatus, V1ErrorResponse};
118
119// Feature Flags
120pub use feature_flag_evaluations::{EvaluateFlagsOptions, FeatureFlagEvaluations};
121pub use feature_flags::{
122    match_feature_flag, match_feature_flag_with_context, match_property_with_context,
123    CohortDefinition, EvaluationContext, FeatureFlag, FeatureFlagCondition, FeatureFlagFilters,
124    FeatureFlagsResponse, FlagDetail, FlagMetadata, FlagReason, FlagValue, InconclusiveMatchError,
125    MultivariateFilter, MultivariateVariant, Property,
126};
127
128// Local Evaluation
129pub use local_evaluation::{
130    Cohort, FlagCache, FlagPoller, LocalEvaluationConfig, LocalEvaluationResponse, LocalEvaluator,
131};
132
133#[cfg(feature = "async-client")]
134pub use local_evaluation::AsyncFlagPoller;
135
136// We expose global convenience functions (capture/flush/shutdown) that use a
137// global client. flush/shutdown matter because the global singleton lives in a
138// `static`, whose `Drop` never runs — they must be called to drain on exit.
139pub use global::capture;
140#[cfg(feature = "error-tracking")]
141pub use global::capture_exception;
142#[cfg(feature = "error-tracking")]
143pub use global::capture_exception_with;
144pub use global::disable as disable_global;
145pub use global::flush;
146pub use global::init_global_client as init_global;
147pub use global::is_disabled as global_is_disabled;
148pub use global::shutdown;