tauri_plugin_mixpanel/
lib.rs1pub use mixpanel_rs::Config;
2use tauri::{
3 plugin::{Builder as PluginBuilder, TauriPlugin},
4 Manager, Runtime, State,
5};
6
7mod commands;
8mod error;
9mod people;
10mod persistence;
11mod state;
12
13use state::MixpanelState;
14
15pub trait MixpanelExt {
16 fn mixpanel(&self) -> State<'_, MixpanelState>;
17}
18
19impl<R: Runtime> MixpanelExt for tauri::AppHandle<R> {
20 fn mixpanel(&self) -> State<'_, MixpanelState> {
21 self.state::<MixpanelState>()
22 }
23}
24
25pub struct Builder {
26 token: String,
27 config: Option<Config>,
28}
29
30impl Builder {
31 pub fn new(token: impl Into<String>, config: Option<Config>) -> Self {
32 Self {
33 token: token.into(),
34 config,
35 }
36 }
37
38 pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
39 let token = self.token;
40 let config = self.config;
41
42 PluginBuilder::<R>::new("mixpanel")
43 .invoke_handler(tauri::generate_handler![
44 commands::register,
45 commands::register_once,
46 commands::unregister,
47 commands::identify,
48 commands::alias,
49 commands::track,
50 commands::get_distinct_id,
51 commands::get_property,
52 commands::reset,
53 commands::time_event,
54 commands::set_group,
55 commands::add_group,
56 commands::remove_group,
57 commands::people_set,
58 commands::people_set_once,
59 commands::people_unset,
60 commands::people_increment,
61 commands::people_append,
62 commands::people_remove,
63 commands::people_union,
64 commands::people_delete_user,
65 ])
66 .setup(
67 move |app_handle, _api| match MixpanelState::new(app_handle, &token, config) {
68 Ok(state) => {
69 app_handle.manage(state);
70 Ok(())
71 }
72 Err(e) => {
73 panic!("Failed to initialize Mixpanel: {:?}", e);
74 }
75 },
76 )
77 .build()
78 }
79}