telemetrydeck_wasm/
lib.rs

1//! TelemetryDeck client for Rust applications and WebAssembly
2//!
3//! This library provides a simple, privacy-focused API for sending telemetry signals to
4//! [TelemetryDeck](https://telemetrydeck.com), supporting both native Rust applications
5//! and WebAssembly targets.
6//!
7//! # Features
8//!
9//! - **Platform Support**: Works in native Rust (using `reqwest` + `tokio`) and WebAssembly (using `reqwasm`)
10//! - **Privacy by Default**: Automatic SHA-256 hashing of user identifiers with optional salt
11//! - **Multi-tenant Support**: Optional namespace parameter for multi-tenant deployments
12//! - **Fire-and-Forget or Error Handling**: Choose between `send()` (async spawn) or `send_sync()` (returns Result)
13//! - **Reserved Constants**: Pre-defined signal types and parameter names for common use cases
14//! - **Session Management**: Automatic session ID generation and management
15//! - **TelemetryDeck v2 API**: Full support for the latest API features
16//!
17//! # Installation
18//!
19//! ## Native Rust Applications
20//!
21//! Add this to your `Cargo.toml`:
22//!
23//! ```toml
24//! [dependencies]
25//! telemetrydeck-wasm = "0.4"
26//! tokio = { version = "1", features = ["rt", "macros"] }
27//! ```
28//!
29//! ## WebAssembly Applications
30//!
31//! Add this to your `Cargo.toml`:
32//!
33//! ```toml
34//! [dependencies]
35//! telemetrydeck-wasm = { version = "0.4", features = ["wasm"] }
36//! ```
37//!
38//! # Quick Start
39//!
40//! ```no_run
41//! use telemetrydeck_wasm::TelemetryDeck;
42//!
43//! // Create a client with your TelemetryDeck App ID
44//! let client = TelemetryDeck::new("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
45//!
46//! // Send a signal (fire-and-forget)
47//! client.send("userLogin", Some("user@example.com"), None, None, None);
48//! ```
49//!
50//! # Examples
51//!
52//! ## Basic Signal
53//!
54//! ```no_run
55//! use telemetrydeck_wasm::TelemetryDeck;
56//!
57//! let client = TelemetryDeck::new("YOUR-APP-ID");
58//! client.send("buttonClick", None, None, None, None);
59//! ```
60//!
61//! ## Signal with User and Custom Parameters
62//!
63//! ```no_run
64//! use telemetrydeck_wasm::TelemetryDeck;
65//! use std::collections::HashMap;
66//!
67//! let client = TelemetryDeck::new("YOUR-APP-ID");
68//!
69//! let mut params = HashMap::new();
70//! params.insert("screen".to_string(), "settings".to_string());
71//! params.insert("version".to_string(), "1.2.0".to_string());
72//!
73//! client.send("appOpened", Some("user123"), Some(params), None, None);
74//! ```
75//!
76//! ## Signal with Floating-Point Value
77//!
78//! ```no_run
79//! use telemetrydeck_wasm::TelemetryDeck;
80//!
81//! let client = TelemetryDeck::new("YOUR-APP-ID");
82//!
83//! // Track revenue with a float value
84//! client.send("revenue", Some("user123"), None, None, Some(99.99));
85//! ```
86//!
87//! ## Multi-tenant Deployment with Namespace
88//!
89//! ```no_run
90//! use telemetrydeck_wasm::TelemetryDeck;
91//! use std::collections::HashMap;
92//!
93//! let client = TelemetryDeck::new_with_config(
94//!     "YOUR-APP-ID",
95//!     Some("tenant-xyz".to_string()),
96//!     None,
97//!     HashMap::new(),
98//! );
99//!
100//! client.send("userAction", Some("user123"), None, None, None);
101//! ```
102//!
103//! ## Enhanced User Privacy with Salt
104//!
105//! ```no_run
106//! use telemetrydeck_wasm::TelemetryDeck;
107//! use std::collections::HashMap;
108//!
109//! // Use a cryptographically random salt (recommended: 64 chars)
110//! let client = TelemetryDeck::new_with_config(
111//!     "YOUR-APP-ID",
112//!     None,
113//!     Some("your-64-char-random-salt-here".to_string()),
114//!     HashMap::new(),
115//! );
116//!
117//! client.send("userEvent", Some("user@example.com"), None, None, None);
118//! ```
119//!
120//! ## Using Reserved Signal Types and Parameters
121//!
122//! ```no_run
123//! use telemetrydeck_wasm::{TelemetryDeck, signals, params};
124//! use std::collections::HashMap;
125//!
126//! let client = TelemetryDeck::new("YOUR-APP-ID");
127//!
128//! // Use pre-defined signal types
129//! client.send(signals::session::STARTED, None, None, None, None);
130//!
131//! // Use pre-defined parameter names
132//! let mut payload = HashMap::new();
133//! payload.insert(params::device::PLATFORM.to_string(), "web".to_string());
134//! payload.insert(params::device::ARCHITECTURE.to_string(), "wasm32".to_string());
135//!
136//! client.send("customSignal", Some("user"), Some(payload), None, None);
137//! ```
138//!
139//! ## Error Handling with send_sync()
140//!
141//! ```no_run
142//! use telemetrydeck_wasm::TelemetryDeck;
143//!
144//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
145//! let client = TelemetryDeck::new("YOUR-APP-ID");
146//!
147//! // Use send_sync() for error handling
148//! match client.send_sync("criticalEvent", Some("user"), None, None, None).await {
149//!     Ok(()) => println!("Signal sent successfully"),
150//!     Err(e) => eprintln!("Failed to send signal: {}", e),
151//! }
152//! # Ok(())
153//! # }
154//! ```
155//!
156//! # Platform-Specific Behavior
157//!
158//! ## Native Rust (default)
159//!
160//! - Uses `reqwest` for HTTP requests
161//! - Uses `tokio::spawn` for fire-and-forget signals
162//! - Requires a tokio runtime to be running
163//!
164//! ## WebAssembly (with `wasm` feature)
165//!
166//! - Uses `reqwasm` for HTTP requests
167//! - Uses `wasm_bindgen_futures::spawn_local` for fire-and-forget signals
168//! - Works in browser event loop (no runtime needed)
169//!
170//! # Privacy and Security
171//!
172//! - User identifiers are always SHA-256 hashed before transmission
173//! - Optional salt can be added for enhanced privacy
174//! - Payload keys with colons are sanitized (`:` → `_`)
175//! - Test mode signals can be marked separately
176//!
177//! # API Version
178//!
179//! This library uses the TelemetryDeck v2 API:
180//! - Default endpoint: `/v2/`
181//! - Namespace endpoint: `/v2/namespace/{namespace}/`
182
183#![deny(missing_docs, missing_debug_implementations)]
184
185mod core;
186pub use core::{Signal, TelemetryDeck};
187
188/// Reserved signal type constants defined by TelemetryDeck
189///
190/// See the [signals] module documentation for usage examples.
191pub mod signals;
192
193/// Reserved parameter name constants defined by TelemetryDeck
194///
195/// See the [params] module documentation for usage examples.
196pub mod params;
197
198#[cfg(feature = "wasm")]
199mod client_wasm;
200
201#[cfg(not(feature = "wasm"))]
202mod client_native;