Analytics API Guide
Learn how to retrieve analytics and insights using the Wacht Rust SDK.
Prerequisites
Before using any API methods, you must initialize the SDK:
use wacht::WachtClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error.Error>> {
// Initialize the client with an API key
let client = WachtClient::new("wk_live_...");
// Now you can use the API
Ok(())
}
Fetch Analytics Summary
Get overall statistics for your deployment with optional date filtering.
Basic Statistics
use wacht::WachtClient;
let client = WachtClient::new("wk_live_...");
// Get all-time statistics
let stats = client.analytics().fetch_stats()
.send()
.await?;
println!("Signups: {}", stats.signups);
println!("Unique Sign-ins: {}", stats.unique_signins);
println!("Organizations Created: {}", stats.organizations_created);
println!("Workspaces Created: {}", stats.workspaces_created);
With Date Range
use wacht::WachtClient;
use chrono::{Utc, Duration};
use wacht::models::AnalyticsStatsOptions;
let client = WachtClient::new("wk_live_...");
// Get statistics for the last 30 days
let stats = client.analytics().fetch_stats()
.options(AnalyticsStatsOptions {
from: Utc::now() - Duration::days(30),
to: Utc::now(),
})
.send()
.await?;
println!("Period Signups: {}", stats.signups);
println!("Period Sign-ins: {}", stats.unique_signins);
if let Some(change) = stats.signups_change {
println!("Signups Change: {:.1}%", change);
}
Get Recent Signups
Retrieve the most recent user registrations.
Basic List
use wacht::WachtClient;
let client = WachtClient::new("wk_live_...");
// Get default number of recent signups
let recent = client.analytics().fetch_recent_signups()
.send()
.await?;
for signup in recent.signups {
println!("{}: {} ({})", signup.signed_up_at, signup.email, signup.first_name);
}
With Limit
use wacht::WachtClient;
let client = WachtClient::new("wk_live_...");
// Get last 50 recent signups
let recent = client.analytics().fetch_recent_signups()
.limit(50)
.send()
.await?;
println!("Recent signups: {}", recent.signups.len());
Get Recent Sign-ins
Retrieve the most recent user authentication events.
Basic List
use wacht::WachtClient;
let client = WachtClient::new("wk_live_...");
// Get default number of recent sign-ins
let recent = client.analytics().fetch_recent_signins()
.send()
.await?;
for signin in recent.signups {
println!("{}: {} via {}", signin.signed_in_at, signin.user_email, signin.method);
}
With Limit
use wacht::WachtClient;
let client = WachtClient::new("wk_live_...");
// Get last 100 recent sign-ins
let recent = client.analytics().fetch_recent_signins()
.limit(100)
.send()
.await?;
println!("Recent sign-ins: {}", recent.signups.len());
Understanding the Response
AnalyticsStatsResponse Fields
Number of new sign-ups in the period.
Number of unique sign-ins in the period.
Number of organizations created in the period.
Number of workspaces created in the period.
Percentage change from previous period.
Percentage change from previous period.
organizations_created_change
Percentage change from previous period.
workspaces_created_change
Percentage change from previous period.
RecentSignup Fields
Sign-up timestamp (ISO 8601).
Sign-up method (e.g., “password”, “oauth”).
RecentSignin Fields
Sign-in timestamp (ISO 8601).
Sign-in method (e.g., “password”, “otp”, “oauth”).
IP address of the sign-in.
Builder Methods
FetchStatsBuilder
Set date range for statistics.
FetchRecentSignupsBuilder
Number of results to return (max 100).
FetchRecentSigninsBuilder
Number of results to return (max 100).
Error Handling
All SDK methods return a Result<T, Error>:
use wacht::{Error, api::analytics};
match analytics::fetch_stats().send().await {
Ok(stats) => {
println!("Total signups: {}", stats.signups);
}
Err(Error::Api { status, message, .. }) => {
eprintln!("API Error {}: {}", status, message);
}
Err(Error::Request(e)) => {
eprintln!("Network error: {}", e);
}
Err(e) => {
eprintln!("Other error: {}", e);
}
}