Skip to main content

rustango_cms/analytics/
collect.rs

1//! Public analytics collector — `POST /__cms__/collect`.
2//!
3//! Receives the beacon JSON (via `navigator.sendBeacon`), enriches it
4//! server-side (country from a CDN header, device/browser/OS from the
5//! User-Agent), and appends one `cms_analytics_event` row. Always
6//! returns `204` + `Cache-Control: no-store`. Bots, prefetches, and
7//! Do-Not-Track / Global-Privacy-Control requests are silently dropped.
8//! No raw IP is stored.
9
10use crate::log_err::LogErr as _;
11use axum::extract::State;
12use axum::http::HeaderMap;
13use axum::response::Response;
14use rustango::extractors::Tenant;
15use rustango::sql::Auto;
16use serde::Deserialize;
17
18use super::model::AnalyticsEvent;
19
20/// The beacon payload (all fields optional / defaulted — never trust the client).
21#[derive(Deserialize, Default)]
22struct Beacon {
23    #[serde(default)]
24    t: String,
25    #[serde(default)]
26    vid: String,
27    #[serde(default)]
28    sid: String,
29    #[serde(default)]
30    p: String,
31    #[serde(default)]
32    referrer: String,
33    #[serde(default)]
34    sw: i32,
35    #[serde(default)]
36    sh: i32,
37    #[serde(default)]
38    dur: i64,
39    #[serde(default)]
40    scroll: i32,
41    #[serde(default)]
42    bounce: bool,
43    #[serde(default)]
44    loc: String,
45}
46
47/// `POST /__cms__/collect` — record a pageview or (fallback) engagement event.
48pub(crate) async fn handle_collect(
49    State(_state): State<crate::router::PublicState>,
50    tenant: Tenant,
51    headers: HeaderMap,
52    req: axum::extract::Request,
53) -> Response {
54    let pool = tenant.pool();
55    let ua = header_str(&headers, axum::http::header::USER_AGENT.as_str());
56
57    // Cheap drops before reading the body.
58    if is_bot(&ua) || is_prefetch(&headers) || privacy_optout(&headers) {
59        return no_content();
60    }
61
62    let bytes = axum::body::to_bytes(req.into_body(), 8 * 1024)
63        .await
64        .unwrap_or_default();
65    let Ok(b) = serde_json::from_slice::<Beacon>(&bytes) else {
66        return no_content();
67    };
68
69    let (device, browser, os) = classify_ua(&ua);
70    let mut entry = AnalyticsEvent {
71        id: Auto::Unset,
72        visitor_id: b.vid.chars().take(36).collect(),
73        session_id: b.sid.chars().take(36).collect(),
74        event_type: if b.t == "engagement" {
75            "engagement".to_owned()
76        } else {
77            "pageview".to_owned()
78        },
79        path: sanitize_path(&b.p),
80        referrer_host: host_of(&b.referrer),
81        country: geo_country(&headers),
82        device: device.to_owned(),
83        browser: browser.chars().take(32).collect(),
84        os: os.chars().take(32).collect(),
85        screen_w: b.sw.clamp(0, 100_000),
86        screen_h: b.sh.clamp(0, 100_000),
87        duration_ms: b.dur.max(0),
88        max_scroll_pct: b.scroll.clamp(0, 100),
89        is_bounce: b.bounce,
90        locale: b.loc.chars().take(16).collect(),
91        created_at: chrono::Utc::now(),
92    };
93    // Best-effort; analytics must never surface an error to the client.
94    entry.insert_pool(pool).await.log_warn("analytics event not recorded");
95    no_content()
96}
97
98/// `204 No Content` + `Cache-Control: no-store` (never cached anywhere).
99pub(crate) fn no_content() -> Response {
100    use axum::http::{header, HeaderValue, StatusCode};
101    let mut r = Response::new(axum::body::Body::empty());
102    *r.status_mut() = StatusCode::NO_CONTENT;
103    r.headers_mut()
104        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
105    r
106}
107
108// ---- shared enrichment helpers (reused by the WS handler) -----------
109
110pub(crate) fn header_str(h: &HeaderMap, name: &str) -> String {
111    h.get(name)
112        .and_then(|v| v.to_str().ok())
113        .unwrap_or("")
114        .to_owned()
115}
116
117/// Country from common CDN geo headers; `""` when none present/valid.
118pub(crate) fn geo_country(h: &HeaderMap) -> String {
119    for name in [
120        "cf-ipcountry",
121        "x-vercel-ip-country",
122        "x-country-code",
123        "x-appengine-country",
124        "fly-region",
125    ] {
126        if let Some(v) = h.get(name).and_then(|v| v.to_str().ok()) {
127            let c = v.trim().to_ascii_uppercase();
128            if c.len() == 2 && c != "XX" && c.bytes().all(|b| b.is_ascii_alphabetic()) {
129                return c;
130            }
131        }
132    }
133    String::new()
134}
135
136/// Dependency-free User-Agent classifier → (device, browser, os).
137pub(crate) fn classify_ua(ua: &str) -> (&'static str, &'static str, &'static str) {
138    let l = ua.to_ascii_lowercase();
139    let device = if l.contains("ipad") || (l.contains("tablet") && !l.contains("mobi")) {
140        "tablet"
141    } else if l.contains("mobi") || l.contains("iphone") || l.contains("android") {
142        "mobile"
143    } else {
144        "desktop"
145    };
146    let browser = if l.contains("edg/") || l.contains("edga/") || l.contains("edgios/") {
147        "Edge"
148    } else if l.contains("opr/") || l.contains("opera") {
149        "Opera"
150    } else if l.contains("firefox") || l.contains("fxios") {
151        "Firefox"
152    } else if l.contains("chrome") || l.contains("crios") {
153        "Chrome"
154    } else if l.contains("safari") {
155        "Safari"
156    } else {
157        "Other"
158    };
159    let os = if l.contains("windows") {
160        "Windows"
161    } else if l.contains("android") {
162        "Android"
163    } else if l.contains("iphone") || l.contains("ipad") || l.contains("ios ") {
164        "iOS"
165    } else if l.contains("mac os") || l.contains("macintosh") {
166        "macOS"
167    } else if l.contains("linux") {
168        "Linux"
169    } else {
170        "Other"
171    };
172    (device, browser, os)
173}
174
175/// Known crawler / preview / automation agents (and empty UA) → drop.
176pub(crate) fn is_bot(ua: &str) -> bool {
177    if ua.trim().is_empty() {
178        return true;
179    }
180    let l = ua.to_ascii_lowercase();
181    [
182        "bot",
183        "crawl",
184        "spider",
185        "slurp",
186        "headless",
187        "lighthouse",
188        "pingdom",
189        "preview",
190        "facebookexternalhit",
191        "python-requests",
192        "curl/",
193        "wget",
194    ]
195    .iter()
196    .any(|p| l.contains(p))
197}
198
199/// Prefetch / prerender navigations (Chrome `Sec-Purpose`, legacy `Purpose`).
200pub(crate) fn is_prefetch(h: &HeaderMap) -> bool {
201    h.get("sec-purpose")
202        .and_then(|v| v.to_str().ok())
203        .is_some_and(|v| v.contains("prefetch") || v.contains("prerender"))
204        || h.get("purpose").and_then(|v| v.to_str().ok()) == Some("prefetch")
205}
206
207/// Honor Do-Not-Track (`DNT: 1`) and Global Privacy Control
208/// (`Sec-GPC: 1`) by default. The beacon JS also self-suppresses, so
209/// this is a server-side backstop for non-JS clients.
210pub(crate) fn privacy_optout(h: &HeaderMap) -> bool {
211    h.get("dnt").and_then(|v| v.to_str().ok()) == Some("1")
212        || h.get("sec-gpc").and_then(|v| v.to_str().ok()) == Some("1")
213}
214
215/// Same-origin path only — strip any accidental scheme/host and the query.
216pub(crate) fn sanitize_path(p: &str) -> String {
217    let no_scheme = p.split("://").last().unwrap_or(p);
218    // If a host slipped in, keep from the first '/'.
219    let path = match no_scheme.find('/') {
220        Some(i) if no_scheme.contains("://") || !p.starts_with('/') => &no_scheme[i..],
221        _ => p,
222    };
223    let path = path.split(['?', '#']).next().unwrap_or("/");
224    let path = if path.is_empty() { "/" } else { path };
225    path.chars().take(512).collect()
226}
227
228/// Referrer → host only (privacy; also keeps the column small).
229pub(crate) fn host_of(referrer: &str) -> String {
230    if referrer.is_empty() {
231        return String::new();
232    }
233    let after = referrer.split("://").nth(1).unwrap_or(referrer);
234    let host = after.split(['/', '?', '#']).next().unwrap_or("");
235    // Drop any userinfo@ and :port.
236    let host = host.rsplit('@').next().unwrap_or(host);
237    let host = host.split(':').next().unwrap_or(host);
238    host.chars().take(255).collect()
239}