Git hosting and a container registry in one Rust binary (axum + Astro)
Analytics: omit user_id for anonymous server-side events
1 file changed, +5 -2
+5-2backend/src/analytics.rs
| @@ -13,16 +13,19 @@ pub fn track(state: &AppState, event: &str, user: Option<&str>, path: &str, prop | ||
| 13 | 13 | }; |
| 14 | 14 | let http = state.http.clone(); |
| 15 | 15 | let hostname = state.config.public_url.host_str().unwrap_or("localhost").to_string(); |
| 16 | - let body = json!({ | |
| 16 | + let mut body = json!({ | |
| 17 | 17 | "type": "custom_event", |
| 18 | 18 | "site_id": rybbit.site_id, |
| 19 | 19 | "event_name": event, |
| 20 | 20 | "properties": properties.to_string(), |
| 21 | 21 | "hostname": hostname, |
| 22 | 22 | "pathname": path, |
| 23 | - "user_id": user, | |
| 24 | 23 | "user_agent": "irongit-server", |
| 25 | 24 | }); |
| 25 | + // The tracker rejects `user_id: null`; anonymous events leave it out. | |
| 26 | + if let Some(user) = user { | |
| 27 | + body["user_id"] = json!(user); | |
| 28 | + } | |
| 26 | 29 | let event = event.to_string(); |
| 27 | 30 | tokio::spawn(async move { |
| 28 | 31 | let mut url = format!("{}/api/track", rybbit.host.trim_end_matches('/')); |