Crates.io | tauri-plugin-mixpanel |
lib.rs | tauri-plugin-mixpanel |
version | |
source | src |
created_at | 2025-04-26 05:49:15.175957+00 |
updated_at | 2025-05-08 06:06:56.071635+00 |
description | Tauri plugin for Mixpanel analytics |
homepage | |
repository | |
max_upload_size | |
id | 1649911 |
Cargo.toml error: | TOML parse error at line 26, column 1 | 26 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This plugin provides a Rust wrapper and TypeScript bindings for using Mixpanel analytics within your Tauri application. It leverages the mixpanel-rs
crate for the core Mixpanel interactions.
Add the plugin to your Cargo.toml
dependencies:
[dependencies]
tauri-plugin-mixpanel = { git = "https://github.com/ahonn/mixpanel-rs", branch = "main" }
# Or from crates.io:
# tauri-plugin-mixpanel = "<version>"
Register the plugin in your main.rs
:
use tauri_plugin_mixpanel::{self, Config};
fn main() {
let mixpanel_token = "YOUR_MIXPANEL_TOKEN"; // Replace with your actual token
let mixpanel_config = Some(Config {
// Optional: Configure batch size, flush interval, etc.
// See mixpanel-rs docs for details: https://docs.rs/mixpanel-rs
..Default::default()
});
tauri::Builder::default()
.plugin(tauri_plugin_mixpanel::Builder::new(mixpanel_token, mixpanel_config).build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Install the frontend bindings using your preferred package manager:
npm install @tauri-apps/api tauri-plugin-mixpanel-api
# or
yarn add @tauri-apps/api tauri-plugin-mixpanel-api
# or
pnpm add @tauri-apps/api tauri-plugin-mixpanel-api
Import the bindings and use them in your frontend code :
import mixpanel from 'tauri-plugin-mixpanel-api';
async function setupAnalytics() {
await mixpanel.identify("user_12345");
await mixpanel.people.set({ "$name": "Alice", "plan": "Premium" });
await mixpanel.register({ "App Version": "1.2.0" });
await mixpanel.track("App Started", { "source": "Frontend" });
console.log("Mixpanel initialized and event tracked.");
}
setupAnalytics();
You can interact with the Mixpanel instance directly from your Rust backend code using Tauri's state management and the MixpanelExt
trait.
First, ensure the plugin is registered as shown in the installation steps.
You can get the managed Mixpanel
state in several ways:
tauri::State<'_, MixpanelState>
.app.state::<MixpanelState>()
or the app.handle().mixpanel()
extension method.use tauri::State;
use tauri_plugin_mixpanel::{MixpanelState, Error as MixpanelError};
use serde_json::json;
#[tauri::command]
async fn track_backend_action(
event_name: String,
mixpanel: State<'_, MixpanelState>
) -> Result<(), String> {
let props = json!({
"source": "Rust Command",
"timestamp": chrono::Utc::now().to_rfc3339(),
});
mixpanel.track(&event_name, Some(props)).await
.map_err(|e: MixpanelError| e.to_string())?;
println!("[Rust Command] Tracked event: {}", event_name);
Ok(())
}
To use the Mixpanel client from Rust code outside of a Tauri command (like in the setup
hook, event listeners, or background tasks), you need access to the Tauri AppHandle
. You can then use the MixpanelExt
trait to get the client.
This often involves cloning the AppHandle
and moving it into an async task.
use tauri::{AppHandle, Manager};
use tauri_plugin_mixpanel::{MixpanelExt, Config, Error as MixpanelError};
use serde_json::json;
fn perform_background_mixpanel_actions(handle: AppHandle) {
tokio::spawn(async move {
let mixpanel = handle.mixpanel();
let distinct_id = mixpanel.get_distinct_id().await;
println!("[Background Task] Current Distinct ID: {:?}", distinct_id);
let register_props = json!({
"last_background_run": chrono::Utc::now().to_rfc3339(),
"background_task_id": rand::random::<u32>() // Example: requires `rand` crate
});
if let Err(e) = mixpanel.register(register_props, None).await {
eprintln!("[Background Task] Failed to register property: {}", e);
}
if let Err(e) = mixpanel.track("Background Task Started", None).await {
eprintln!("[Background Task] Failed to track event: {}", e);
}
});
}
This plugin currently does not require any specific capabilities to be enabled in your tauri.conf.json
allowlist, as it interacts with the network via the Rust core, not directly from the webview.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.