tauri_plugin_mixpanel/
lib.rs

1pub 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    fn try_mixpanel(&self) -> Option<State<'_, MixpanelState>>;
19}
20
21impl<R: Runtime> MixpanelExt for tauri::AppHandle<R> {
22    fn mixpanel(&self) -> State<'_, MixpanelState> {
23        self.state::<MixpanelState>()
24    }
25
26    fn try_mixpanel(&self) -> Option<State<'_, MixpanelState>> {
27        self.try_state::<MixpanelState>()
28    }
29}
30
31pub struct Builder {
32    token: String,
33    config: Option<Config>,
34}
35
36impl Builder {
37    pub fn new(token: impl Into<String>, config: Option<Config>) -> Self {
38        Self {
39            token: token.into(),
40            config,
41        }
42    }
43
44    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
45        let token = self.token;
46        let config = self.config;
47
48        PluginBuilder::<R>::new("mixpanel")
49            .invoke_handler(tauri::generate_handler![
50                commands::register,
51                commands::register_once,
52                commands::unregister,
53                commands::identify,
54                commands::alias,
55                commands::track,
56                commands::get_distinct_id,
57                commands::get_property,
58                commands::reset,
59                commands::time_event,
60                commands::set_group,
61                commands::add_group,
62                commands::remove_group,
63                commands::people_set,
64                commands::people_set_once,
65                commands::people_unset,
66                commands::people_increment,
67                commands::people_append,
68                commands::people_remove,
69                commands::people_union,
70                commands::people_delete_user,
71            ])
72            .setup(
73                move |app_handle, _api| match MixpanelState::new(app_handle, &token, config) {
74                    Ok(state) => {
75                        app_handle.manage(state);
76                        Ok(())
77                    }
78                    Err(e) => {
79                        panic!("Failed to initialize Mixpanel: {:?}", e);
80                    }
81                },
82            )
83            .build()
84    }
85}