Skip to main content

rust_umami/
response.rs

1use serde::{Deserialize, Serialize};
2use serde_json::{Map, Value};
3
4/// Response returned after sending one analytics track.
5///
6/// Umami can intentionally ignore bot or disabled traffic and return a small
7/// response without session identifiers. Use [`TrackResponse::is_tracked`] to
8/// distinguish that case without relying on undocumented fields.
9#[derive(Clone, Debug, Default, Deserialize, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct TrackResponse {
12    /// Cache value that can be supplied with a later request from this session.
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub cache: Option<String>,
15    /// Derived Umami session ID.
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub session_id: Option<String>,
18    /// Derived Umami visit ID.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub visit_id: Option<String>,
21    /// Whether collection was disabled by the server or gateway.
22    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
23    pub disabled: bool,
24    /// Additional forward-compatible response fields.
25    #[serde(flatten)]
26    pub extra: Map<String, Value>,
27}
28
29impl TrackResponse {
30    /// Returns true when Umami recorded the track and issued session metadata.
31    #[must_use]
32    pub fn is_tracked(&self) -> bool {
33        self.session_id.is_some() && self.visit_id.is_some()
34    }
35
36    /// Returns true when Umami accepted the request but did not record it.
37    #[must_use]
38    pub fn is_ignored(&self) -> bool {
39        !self.is_tracked()
40    }
41}
42
43/// Per-item error returned inside a batch response.
44#[derive(Clone, Debug, Deserialize, Serialize)]
45pub struct BatchError {
46    /// Zero-based index of the failed item in the submitted batch.
47    pub index: usize,
48    /// Umami's response for the failed item.
49    pub response: Value,
50}
51
52/// Summary returned by Umami's batch ingestion endpoint.
53#[derive(Clone, Debug, Deserialize, Serialize)]
54#[serde(rename_all = "camelCase")]
55pub struct BatchResponse {
56    /// Number of submitted items.
57    pub size: usize,
58    /// Number of successfully processed items.
59    pub processed: usize,
60    /// Number of failed items.
61    pub errors: usize,
62    /// Details for each failed item.
63    #[serde(default)]
64    pub details: Vec<BatchError>,
65    /// Cache value returned for the first successfully processed item.
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub cache: Option<String>,
68}
69
70impl BatchResponse {
71    /// Returns true when every item was processed successfully.
72    #[must_use]
73    pub fn is_success(&self) -> bool {
74        self.errors == 0
75    }
76}