tauri-plugin-mixpanel 0.3.1

Tauri plugin for Mixpanel analytics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::error::{Error, Result};
use crate::persistence::Persistence;
use mixpanel_rs::Mixpanel;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;

pub(crate) const SET_ACTION: &str = "$set";
pub(crate) const SET_ONCE_ACTION: &str = "$set_once";
pub(crate) const UNSET_ACTION: &str = "$unset";
pub(crate) const ADD_ACTION: &str = "$add";
pub(crate) const APPEND_ACTION: &str = "$append";
pub(crate) const REMOVE_ACTION: &str = "$remove";
pub(crate) const UNION_ACTION: &str = "$union";
pub(crate) const DELETE_ACTION: &str = "$delete";

pub struct MixpanelPeople {
    client: Mixpanel,
    persistence: Arc<Persistence>,
}

impl MixpanelPeople {
    pub(crate) fn new(client: Mixpanel, persistence: Arc<Persistence>) -> Self {
        Self {
            client,
            persistence,
        }
    }

    fn get_distinct_id(&self) -> Option<String> {
        self.persistence.get_distinct_id()
    }

    fn identify_called(&self) -> bool {
        self.get_distinct_id()
            .map_or(false, |id| !id.starts_with("$device:"))
    }

    fn is_reserved_property(&self, prop: &str) -> bool {
        matches!(
            prop,
            "$distinct_id" | "$token" | "$device_id" | "$user_id" | "$had_persisted_distinct_id"
        )
    }

    /// Internal function to prepare and send the people request.
    async fn send_request(&self, action: &str, properties: HashMap<String, Value>) -> Result<()> {
        if !self.identify_called() {
            println!("Mixpanel People: identify() must be called before using People API methods. Operation queued (in theory - queuing not fully implemented yet).");
            return Ok(());
        }

        let distinct_id = self.get_distinct_id().ok_or_else(|| {
            Error::MixpanelError(
                "Cannot perform People operation without a distinct_id.".to_string(),
            )
        })?;
        let map_err = |e: mixpanel_rs::error::Error| Error::MixpanelClient(e);

        match action {
            SET_ACTION => self
                .client
                .people
                .set(&distinct_id, properties, None)
                .await
                .map_err(map_err)?,
            SET_ONCE_ACTION => self
                .client
                .people
                .set_once(&distinct_id, properties, None)
                .await
                .map_err(map_err)?,
            UNSET_ACTION => {
                let keys_to_unset: Vec<String> = properties.keys().cloned().collect();
                self.client
                    .people
                    .unset(&distinct_id, keys_to_unset, None)
                    .await
                    .map_err(map_err)?
            }
            ADD_ACTION => {
                let mut increment_props: HashMap<String, i64> = HashMap::new();
                for (key, value) in properties {
                    if let Some(num) = value.as_i64() {
                        increment_props.insert(key, num);
                    } else {
                        eprintln!(
                            "Mixpanel People: Invalid increment value for key '{}' - must be convertible to i64.",
                            key
                        );
                        return Err(Error::MixpanelError(format!(
                            "Invalid increment value for key '{}'",
                            key
                        )));
                    }
                }

                self.client
                    .people
                    .increment(&distinct_id, increment_props, None)
                    .await
                    .map_err(map_err)?
            }
            APPEND_ACTION => self
                .client
                .people
                .append(&distinct_id, properties, None)
                .await
                .map_err(map_err)?,
            REMOVE_ACTION => self
                .client
                .people
                .remove(&distinct_id, properties, None)
                .await
                .map_err(map_err)?,
            UNION_ACTION => self
                .client
                .people
                .union(&distinct_id, properties, None)
                .await
                .map_err(map_err)?,
            DELETE_ACTION => self
                .client
                .people
                .delete_user(&distinct_id, None)
                .await
                .map_err(map_err)?,
            _ => {
                return Err(Error::MixpanelError(format!(
                    "Unknown People action: {}",
                    action
                )))
            }
        };

        Ok(())
    }

    /// Parses a serde_json::Value into a HashMap<String, Value>.
    /// Filters out reserved properties.
    fn parse_and_filter_props(
        &self,
        value: Value,
        action_name: &str,
    ) -> Result<HashMap<String, Value>> {
        match value {
            Value::Object(obj) => Ok(obj
                .into_iter()
                .filter(|(k, _)| !self.is_reserved_property(k))
                .collect()),
            Value::Null => Ok(HashMap::new()),
            _ => Err(Error::MixpanelError(format!(
                "Properties for people.{} must be an object or null",
                action_name
            ))),
        }
    }

    /// Set properties on a user profile.
    ///
    /// If `prop` is a String, `to` is the value.
    /// If `prop` is an Object (Value::Object), `to` is ignored and the object keys/values are used.
    pub async fn set(&self, prop: Value, to: Option<Value>) -> Result<()> {
        let properties = match prop {
            Value::Object(map) => self.parse_and_filter_props(Value::Object(map), "set")?,
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let mut map = HashMap::new();
                map.insert(key, to.unwrap_or(Value::Null));
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'prop' argument for people.set. Must be String or Object.".to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }
        self.send_request(SET_ACTION, properties).await
    }

    /// Set properties on a user profile, only if they do not yet exist.
    pub async fn set_once(&self, prop: Value, to: Option<Value>) -> Result<()> {
        let properties = match prop {
            Value::Object(map) => self.parse_and_filter_props(Value::Object(map), "set_once")?,
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let mut map = HashMap::new();
                map.insert(key, to.unwrap_or(Value::Null));
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'prop' argument for people.set_once. Must be String or Object."
                        .to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }

        self.send_request(SET_ONCE_ACTION, properties).await
    }

    /// Unset properties on a user profile.
    ///
    /// `prop` should be a String (single key) or an Array of Strings (multiple keys).
    pub async fn unset(&self, prop: Value) -> Result<()> {
        let mut keys_to_unset = HashMap::new();

        match prop {
            Value::String(key) => {
                if !self.is_reserved_property(&key) {
                    keys_to_unset.insert(key, Value::Null);
                }
            }
            Value::Array(keys) => {
                for key_val in keys {
                    if let Value::String(key) = key_val {
                        if !self.is_reserved_property(&key) {
                            keys_to_unset.insert(key, Value::Null);
                        }
                    } else {
                        return Err(Error::MixpanelError(
                            "Invalid array element in people.unset. Must be strings.".to_string(),
                        ));
                    }
                }
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'prop' argument for people.unset. Must be String or Array of Strings."
                        .to_string(),
                ))
            }
        }

        if keys_to_unset.is_empty() {
            return Ok(());
        }

        self.send_request(UNSET_ACTION, keys_to_unset).await
    }

    /// Increment/decrement numeric user profile properties.
    ///
    /// If `prop` is a String, `by` is the numeric amount.
    /// If `prop` is an Object (Value::Object), `by` is ignored, and the object's key/numeric values are used.
    pub async fn increment(&self, prop: Value, by: Option<Value>) -> Result<()> {
        let properties = match prop {
            Value::Object(map) => {
                let mut filtered_map = HashMap::new();
                for (key, value) in map {
                    if self.is_reserved_property(&key) {
                        continue;
                    }
                    if value.is_number() {
                        filtered_map.insert(key, value);
                    } else {
                        eprintln!(
                            "Mixpanel People: Invalid increment value for key '{}' - must be a number.",
                            key
                        );
                        return Err(Error::MixpanelError(format!(
                            "Invalid increment value for key '{}'",
                            key
                        )));
                    }
                }
                filtered_map
            }
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let amount = by.unwrap_or(Value::Number(1.into())); // Default increment by 1
                if !amount.is_number() {
                    return Err(Error::MixpanelError(
                        "Invalid 'by' argument for people.increment. Must be a number.".to_string(),
                    ));
                }
                let mut map = HashMap::new();
                map.insert(key, amount);
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'prop' argument for people.increment. Must be String or Object."
                        .to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }

        self.send_request(ADD_ACTION, properties).await
    }

    /// Append a value to a list-valued user profile property.
    pub async fn append(&self, list_name: Value, value: Option<Value>) -> Result<()> {
        let properties = match list_name {
            Value::Object(map) => self.parse_and_filter_props(Value::Object(map), "append")?,
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let mut map = HashMap::new();
                map.insert(key, value.unwrap_or(Value::Null));
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'list_name' argument for people.append. Must be String or Object."
                        .to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }

        self.send_request(APPEND_ACTION, properties).await
    }

    /// Remove a value from a list-valued user profile property.
    pub async fn remove(&self, list_name: Value, value: Option<Value>) -> Result<()> {
        let properties = match list_name {
            Value::Object(map) => self.parse_and_filter_props(Value::Object(map), "remove")?,
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let mut map = HashMap::new();
                map.insert(key, value.unwrap_or(Value::Null));
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'list_name' argument for people.remove. Must be String or Object."
                        .to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }

        self.send_request(REMOVE_ACTION, properties).await
    }

    /// Merge a list with a list-valued user profile property, excluding duplicate values.
    pub async fn union(&self, list_name: Value, values: Option<Value>) -> Result<()> {
        let properties = match list_name {
            Value::Object(map) => {
                let mut processed_map = HashMap::new();
                for (key, value) in map {
                    if self.is_reserved_property(&key) {
                        continue;
                    }
                    match value {
                        Value::Array(arr) => {
                            processed_map.insert(key, Value::Array(arr));
                        }
                        single_val => {
                            processed_map.insert(key, Value::Array(vec![single_val]));
                        }
                    }
                }
                processed_map
            }
            Value::String(key) => {
                if self.is_reserved_property(&key) {
                    return Ok(());
                }
                let values_to_union = match values.unwrap_or(Value::Array(vec![])) {
                    Value::Array(arr) => Value::Array(arr),
                    single_val => Value::Array(vec![single_val]),
                };
                let mut map = HashMap::new();
                map.insert(key, values_to_union);
                map
            }
            _ => {
                return Err(Error::MixpanelError(
                    "Invalid 'list_name' argument for people.union. Must be String or Object."
                        .to_string(),
                ))
            }
        };

        if properties.is_empty() {
            return Ok(());
        }

        self.send_request(UNION_ACTION, properties).await
    }

    /// Permanently delete the user's profile.
    pub async fn delete_user(&self) -> Result<()> {
        if !self.identify_called() {
            eprintln!("Mixpanel People: delete_user() requires identify() to be called first.");
            return Ok(());
        }
        self.send_request(DELETE_ACTION, HashMap::new()).await
    }
}