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).await?;
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).await?;
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 endpoints;
74mod error;
75#[cfg(feature = "error-tracking")]
76mod error_tracking;
77mod event;
78#[cfg(feature = "capture-v1")]
79mod event_v1;
80mod feature_flag_evaluations;
81mod feature_flags;
82mod global;
83mod local_evaluation;
84
85// Public interface - any change to this is breaking!
86// Client
87pub use client::client;
88pub use client::BeforeSendHook;
89pub use client::CaptureCompression;
90pub use client::Client;
91pub use client::ClientOptions;
92pub use client::ClientOptionsBuilder;
93pub use client::ClientOptionsBuilderError;
94
95// Endpoints
96pub use endpoints::{
97 Endpoint, EndpointManager, DEFAULT_HOST, EU_INGESTION_ENDPOINT, US_INGESTION_ENDPOINT,
98};
99
100// Error
101pub use error::Error;
102
103// Error Tracking
104#[cfg(feature = "error-tracking")]
105pub use error_tracking::{
106 CaptureExceptionOptions, ErrorTrackingOptions, ErrorTrackingOptionsBuilder,
107 ErrorTrackingOptionsBuilderError,
108};
109
110// Event
111pub use event::Event;
112#[cfg(feature = "capture-v1")]
113pub use event::EventOptions;
114
115// V1 Capture types
116#[cfg(feature = "capture-v1")]
117pub use event_v1::{CaptureResponse, EventResult, EventStatus};
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 a global capture function as a convenience, that uses a global client
137pub use global::capture;
138#[cfg(feature = "error-tracking")]
139pub use global::capture_exception;
140#[cfg(feature = "error-tracking")]
141pub use global::capture_exception_with;
142pub use global::disable as disable_global;
143pub use global::init_global_client as init_global;
144pub use global::is_disabled as global_is_disabled;