Rust SDK
AnalyticsManager is a thread-safe singleton struct that sends analytics events to the Vortex platform from any Rust environment.
GitHub: Vortex-Analytics-IO/Rust-SDK
Supported features:
- Immediate and batched event tracking
- Automatic retry when the server becomes available
- Persistent device and session identification
- Thread-safe queue management
Installation
Section titled “Installation”Add the following to your Cargo.toml:
[dependencies]vortex_analytics = { git = "https://github.com/Vortex-Analytics-IO/Rust-SDK" }The crate internally depends on:
| Crate | Purpose |
|---|---|
reqwest | Blocking HTTP client with JSON support |
serde / serde_json | Serialization |
uuid | Device and session ID generation |
chrono | Timestamps |
Initialization
Section titled “Initialization”AnalyticsManager is a singleton accessed via AnalyticsManager::instance(). Initialize it once in main() before sending any events.
use vortex_analytics::AnalyticsManager;
fn main() { AnalyticsManager::instance().init( "my_app", // tenant_id "https://in.vortexanalytics.io", // url "Windows", // platform "1.0.0", // app_version true, // auto_batching 10, // flush_interval_sec );
// ... your application logic ...
AnalyticsManager::instance().shutdown();}Verbose logging
Section titled “Verbose logging”Enable verbose output to see internal debug information:
AnalyticsManager::instance().set_verbose(true);What happens on initialization
Section titled “What happens on initialization”- Loads or generates a persistent device identifier (stored in a local
analytics.idfile) - Creates a new session ID
- Spawns a background thread to check server health
- Enables or disables analytics based on server availability
- Automatically tracks an
app_startedevent
If the server is unreachable, events are safely queued until connectivity is restored.
Tracking events
Section titled “Tracking events”Simple event
Section titled “Simple event”AnalyticsManager::instance().track_event_string("app_started", "");Event with string payload
Section titled “Event with string payload”AnalyticsManager::instance().track_event_string("menu_opened", "settings");Event with structured data
Section titled “Event with structured data”use std::collections::HashMap;use serde_json::Value;
let mut props = HashMap::new();props.insert("level".to_string(), Value::from(5));props.insert("difficulty".to_string(), Value::from("Hard"));props.insert("time".to_string(), Value::from(123.4));
AnalyticsManager::instance().track_event("level_completed", props);Batching
Section titled “Batching”Manual batching
Section titled “Manual batching”Use manual batching when you want explicit control over when events are sent (e.g. at the end of a match).
use std::collections::HashMap;use serde_json::Value;
// Queue eventsAnalyticsManager::instance().batched_track_event_string("EnemyKilled", "");
let mut props = HashMap::new();props.insert("item".to_string(), Value::from("MagicSword"));props.insert("rarity".to_string(), Value::from("Epic"));
AnalyticsManager::instance().batched_track_event("ItemCrafted", props);
// Send all queued events in a single request (spawns a background thread)AnalyticsManager::instance().flush_manual_batch();Automatic batching
Section titled “Automatic batching”Enable auto_batching in init():
track_event/track_event_stringcalls are queued automatically- The queue flushes every
flush_interval_secseconds - If the server is unreachable, events stay queued until connectivity is restored
Custom data
Section titled “Custom data”Attach persistent metadata to every event the manager sends.
use std::collections::HashMap;use serde_json::Value;
// Set custom data — included in all subsequent eventslet mut custom = HashMap::new();custom.insert("user_id".to_string(), Value::from(123));custom.insert("tier".to_string(), Value::from("gold"));
AnalyticsManager::instance().set_custom_data(Some(custom));
// Remove custom dataAnalyticsManager::instance().set_custom_data(None);- Empty custom data is not included in request payloads
- The map is automatically serialized to JSON
Lifecycle handling
Section titled “Lifecycle handling”AnalyticsManager cannot detect application shutdown automatically. You must call shutdown() when your app exits to flush any buffered events.
use vortex_analytics::AnalyticsManager;
fn main() { AnalyticsManager::instance().init( "my_app", "https://in.vortexanalytics.io", "Windows", "1.0.0", false, 10, );
// ... your application logic ...
AnalyticsManager::instance().shutdown();}For signal-based shutdown (e.g. Ctrl+C), use a crate like ctrlc:
ctrlc::set_handler(|| { AnalyticsManager::instance().shutdown(); std::process::exit(0);}).expect("Error setting Ctrl-C handler");