Admin statistics page, and where new members come from
CI / test (push) Successful in 3m31s
CI / build (push) Skipped
CI / deploy (push) Skipped

Admin → Statisztika shows sign-ups, own charts, purchases, analyses, the
weekly horoscope's readers, forum posts and the AI cost for 7, 30 or 90
days (or since the start), day by day, the way from sign-up to payment,
and the members' sources, devices and sign-up methods.

At sign-up the site now records where the member came from (Facebook,
a search engine, another site's address, the link's campaign tags) and
the device type: the page keeps what it saw when the visit began in
memory only and sends it with the sign-up; nothing is stored in the
browser or about visitors who do not sign up. The privacy notice and
the data export include it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-27 12:10:25 +00:00
co-authored by Claude Opus 5.5
parent b37094e481
commit 08ad7b0a79
23 changed files with 1576 additions and 11 deletions
+45 -9
View File
@@ -25,6 +25,7 @@ use crate::auth::{
validate_password, verify_password,
};
use crate::error::{ApiError, ApiResult};
use crate::signup_source::{self, CameFrom};
use crate::views::UserView;
const VERIFY_EMAIL_TTL: time::Duration = time::Duration::days(3);
@@ -230,6 +231,9 @@ pub struct RegisterRequest {
/// Cloudflare Turnstile's token (see [`crate::turnstile`]).
#[serde(default)]
turnstile_token: Option<String>,
/// Where the visit began, for the statistics.
#[serde(default)]
came_from: Option<CameFrom>,
}
/// Before a sign-up or reset e-mail goes out: at most a few per visitor
@@ -242,6 +246,12 @@ async fn not_a_robot(state: &AppState, headers: &axum::http::HeaderMap, token: O
crate::turnstile::verify(state.config.turnstile.as_ref(), token, ip.as_deref()).await
}
/// Where a new member came from (see [`signup_source`]).
fn arrival(state: &AppState, headers: &HeaderMap, came: Option<&CameFrom>) -> signup_source::Arrival {
let ua = user_agent(headers);
signup_source::classify(ua.as_deref(), came, &signup_source::host_of(&state.config.public_base_url))
}
/// Password endpoints answer 403 while `PASSWORD_LOGIN` is off.
pub fn password_login_enabled(state: &crate::AppState) -> Result<(), ApiError> {
if state.config.password_login {
@@ -279,13 +289,20 @@ pub async fn register(
None => {
super::invitations::admit(&state, &mut tx, &email, req.invite_token.as_deref()).await?;
let user_id = Uuid::now_v7();
sqlx::query("INSERT INTO users (id, email, display_name, locale) VALUES ($1, $2, $3, $4)")
.bind(user_id)
.bind(&email)
.bind(clean_name(req.display_name))
.bind(&locale)
.execute(&mut *tx)
.await?;
let arrival = arrival(&state, &headers, req.came_from.as_ref());
sqlx::query(
"INSERT INTO users (id, email, display_name, locale, signup_source, signup_detail, signup_device) \
VALUES ($1, $2, $3, $4, $5, $6, $7)",
)
.bind(user_id)
.bind(&email)
.bind(clean_name(req.display_name))
.bind(&locale)
.bind(arrival.source)
.bind(&arrival.detail)
.bind(arrival.device)
.execute(&mut *tx)
.await?;
// Packages of invitations go to this account; only whoever opens
// the link in the mailbox can ever log in to it.
super::invitations::mark_accepted(
@@ -369,6 +386,9 @@ pub struct GoogleRequest {
invite_token: Option<String>,
/// The slug of the shared link the visitor came from.
referral: Option<String>,
/// Where the visit began, for the statistics of a new account.
#[serde(default)]
came_from: Option<CameFrom>,
}
/// Logs in with Google. An unknown Google account is linked to the existing
@@ -385,6 +405,7 @@ pub async fn google(
locale: claims.locale.clone(),
invite_token: req.invite_token,
referral: req.referral,
came_from: req.came_from,
};
social_login(&state, headers, jar, "google", claims, signup).await
}
@@ -401,6 +422,8 @@ pub struct AppleRequest {
name: Option<String>,
invite_token: Option<String>,
referral: Option<String>,
#[serde(default)]
came_from: Option<CameFrom>,
}
/// Logs in with Apple, like [`google`]. The address may be an Apple relay
@@ -412,7 +435,13 @@ pub async fn apple(
WithRejection(Json(req), _): Body<AppleRequest>,
) -> ApiResult<impl IntoResponse> {
let claims = state.apple.verify(&req.id_token).await?;
let signup = Signup { name: req.name, locale: None, invite_token: req.invite_token, referral: req.referral };
let signup = Signup {
name: req.name,
locale: None,
invite_token: req.invite_token,
referral: req.referral,
came_from: req.came_from,
};
social_login(&state, headers, jar, "apple", claims, signup).await
}
@@ -422,6 +451,7 @@ struct Signup {
locale: Option<String>,
invite_token: Option<String>,
referral: Option<String>,
came_from: Option<CameFrom>,
}
/// Logs in with a verified provider identity: the linked user, else the user
@@ -500,13 +530,19 @@ async fn social_login(
.filter(|l| state.config.supported_locales.iter().any(|s| s == l))
.unwrap_or(&state.config.supported_locales[0])
.to_string();
let arrival = arrival(state, &headers, signup.came_from.as_ref());
sqlx::query(
"INSERT INTO users (id, email, email_verified_at, display_name, locale) VALUES ($1, $2, now(), $3, $4)",
"INSERT INTO users (id, email, email_verified_at, display_name, locale, \
signup_source, signup_detail, signup_device) \
VALUES ($1, $2, now(), $3, $4, $5, $6, $7)",
)
.bind(id)
.bind(&email)
.bind(clean_name(signup.name))
.bind(locale)
.bind(arrival.source)
.bind(&arrival.detail)
.bind(arrival.device)
.execute(&mut *tx)
.await?;
super::invitations::mark_accepted(