Skip to main content

systemprompt_analytics/models/
engagement.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use systemprompt_identifiers::{ContentId, EngagementEventId, SessionId, UserId};
5
6#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
7pub struct EngagementEvent {
8    pub id: EngagementEventId,
9    pub session_id: SessionId,
10    pub user_id: UserId,
11    pub page_url: String,
12    pub content_id: Option<ContentId>,
13    pub event_type: String,
14    pub time_on_page_ms: i32,
15    pub time_to_first_interaction_ms: Option<i32>,
16    pub time_to_first_scroll_ms: Option<i32>,
17    pub max_scroll_depth: i32,
18    pub scroll_velocity_avg: Option<f32>,
19    pub scroll_direction_changes: Option<i32>,
20    pub click_count: i32,
21    pub mouse_move_distance_px: Option<i32>,
22    pub keyboard_events: Option<i32>,
23    pub copy_events: Option<i32>,
24    pub focus_time_ms: i32,
25    pub blur_count: i32,
26    pub tab_switches: i32,
27    pub visible_time_ms: i32,
28    pub hidden_time_ms: i32,
29    pub is_rage_click: Option<bool>,
30    pub is_dead_click: Option<bool>,
31    pub reading_pattern: Option<String>,
32    pub created_at: DateTime<Utc>,
33    pub updated_at: DateTime<Utc>,
34}
35
36#[derive(Debug, Clone, Deserialize)]
37#[serde(default)]
38pub struct CreateEngagementEventInput {
39    #[serde(default)]
40    pub page_url: String,
41    #[serde(default = "default_event_type")]
42    pub event_type: String,
43    #[serde(default)]
44    pub time_on_page_ms: i32,
45    #[serde(default)]
46    pub max_scroll_depth: i32,
47    #[serde(default)]
48    pub click_count: i32,
49    #[serde(flatten)]
50    pub optional_metrics: EngagementOptionalMetrics,
51}
52
53impl Default for CreateEngagementEventInput {
54    fn default() -> Self {
55        Self {
56            page_url: String::new(),
57            event_type: default_event_type(),
58            time_on_page_ms: 0,
59            max_scroll_depth: 0,
60            click_count: 0,
61            optional_metrics: EngagementOptionalMetrics::default(),
62        }
63    }
64}
65
66#[derive(Debug, Clone, Default, Deserialize)]
67pub struct EngagementOptionalMetrics {
68    pub time_to_first_interaction_ms: Option<i32>,
69    pub time_to_first_scroll_ms: Option<i32>,
70    pub scroll_velocity_avg: Option<f32>,
71    pub scroll_direction_changes: Option<i32>,
72    pub mouse_move_distance_px: Option<i32>,
73    pub keyboard_events: Option<i32>,
74    pub copy_events: Option<i32>,
75    pub focus_time_ms: Option<i32>,
76    pub blur_count: Option<i32>,
77    pub tab_switches: Option<i32>,
78    pub visible_time_ms: Option<i32>,
79    pub hidden_time_ms: Option<i32>,
80    pub is_rage_click: Option<bool>,
81    pub is_dead_click: Option<bool>,
82    pub reading_pattern: Option<String>,
83}
84
85fn default_event_type() -> String {
86    "page_exit".to_string()
87}