A simple and efficient Rust SDK for game-events.io analytics.
- 🚀 Simple API: Easy-to-use builder pattern for events
- 📦 Event Buffering: Automatically buffers events for batch sending
- 🔄 Batch Upload: Send events in configurable batches
- 🛡️ Type-Safe: Leverages Rust's type system for safety
- ⚡ Lightweight: Minimal dependencies
Add this to your Cargo.toml:
[dependencies]
game-events-sdk = { git = "https://github.com/game-events-io/rust-sdk.git" }use game_events_sdk::{GameEventsIOClient, GameEventsIOSession};
use std::collections::HashMap;
fn main() {
// Initialize the client with your API key
let mut client = GameEventsIOClient::new("YOUR_API_KEY");
// Create a session (automatically generates UUIDs for user_id and session_id)
let mut session = GameEventsIOSession::default();
// Log an event
session.push_event("level_completed", HashMap::new());
// Move events from session to client
for event in session.take_events(100) {
client.log_event(event);
}
// Send all buffered events
match client.flush() {
Ok(response) => println!("Events sent: {}", response),
Err(e) => eprintln!("Error sending events: {}", e),
}
}GameEventsIOSession helps manage user_id, session_id, and user properties automatically.
use game_events_sdk::GameEventsIOSession;
use serde_json::json;
use std::collections::HashMap;
// Create session with specific IDs
let mut session = GameEventsIOSession::new("user_123", "session_456");
// Set user properties (added to all events)
session.set_user_property("platform", json!("rust"));
session.set_user_property("subscription", json!("premium"));
// Log events (stored internally)
session.push_event("app_start", HashMap::new());
let mut props = HashMap::new();
props.insert("level", json!(5));
session.push_event("level_start", props);
// Retrieve events to send
let events = session.take_events(10);You can still create events manually if you prefer:
let event = GameEventsIOEventBuilder::default()
.event("button_click")
.user_id("user_123")
.session_id("session_456")
.build()
.unwrap();
client.log_event(event);use serde_json::json;
let mut event_props = HashMap::new();
event_props.insert("level_id".to_string(), json!(5));
event_props.insert("score".to_string(), json!(1500));
event_props.insert("difficulty".to_string(), json!("hard"));
let event = GameEventsIOEventBuilder::default()
.event("level_completed")
.user_id("user_123")
.session_id("session_456")
.event_properties(event_props)
.build()
.unwrap();
client.log_event(event);// Send events in batches of 100
while client.pending_events_count() > 0 {
match client.flush_batch(100) {
Ok(response) => println!("Batch sent: {}", response),
Err(e) => eprintln!("Error: {}", e),
}
}use game_events_sdk::GameEventsIOClientBuilder;
let client = GameEventsIOClientBuilder::default()
.api_key("YOUR_API_KEY")
.backend_url("https://api.game-events.io/v1/events") // Updated to new domain
.build()
.unwrap();new(api_key: impl Into<String>) -> Self- Create a new clientlog_event(&mut self, event: GameEventsIOEvent)- Add an event to the bufferflush(&mut self) -> Result<String, reqwest::Error>- Send all buffered eventsflush_batch(&mut self, batch_size: usize) -> Result<String, reqwest::Error>- Send events in batchespending_events_count(&self) -> usize- Get the number of buffered events
event: String- Event name (required)user_id: String- Unique user identifier (required)session_id: String- Session identifier (required)time: u64- Unix timestamp in seconds (auto-generated if not provided)event_properties: HashMap<String, serde_json::Value>- Event-specific propertiesuser_properties: HashMap<String, serde_json::Value>- User properties
- Rust 1.70 or later
MIT
Contributions are welcome! Please feel free to submit a Pull Request.