Skip to main content

systemprompt_analytics/models/
funnel.rs

1//! Conversion-funnel aggregate models.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use sqlx::FromRow;
9use systemprompt_identifiers::{FunnelId, FunnelProgressId, SessionId};
10
11#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
12pub struct Funnel {
13    pub id: FunnelId,
14    pub name: String,
15    pub description: Option<String>,
16    pub is_active: bool,
17    pub created_at: DateTime<Utc>,
18    pub updated_at: DateTime<Utc>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
22pub struct FunnelStep {
23    pub funnel_id: FunnelId,
24    pub step_order: i32,
25    pub name: String,
26    pub match_pattern: String,
27    pub match_type: FunnelMatchType,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
31#[sqlx(type_name = "text", rename_all = "snake_case")]
32#[serde(rename_all = "snake_case")]
33pub enum FunnelMatchType {
34    UrlExact,
35    UrlPrefix,
36    UrlRegex,
37    EventType,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
41pub struct FunnelProgress {
42    pub id: FunnelProgressId,
43    pub funnel_id: FunnelId,
44    pub session_id: SessionId,
45    pub current_step: i32,
46    pub completed_at: Option<DateTime<Utc>>,
47    pub dropped_at_step: Option<i32>,
48    pub step_timestamps: serde_json::Value,
49    pub created_at: DateTime<Utc>,
50    pub updated_at: DateTime<Utc>,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct CreateFunnelInput {
55    pub name: String,
56    pub description: Option<String>,
57    pub steps: Vec<CreateFunnelStepInput>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct CreateFunnelStepInput {
62    pub name: String,
63    pub match_pattern: String,
64    pub match_type: FunnelMatchType,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct FunnelWithSteps {
69    pub funnel: Funnel,
70    pub steps: Vec<FunnelStep>,
71}
72
73#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
74pub struct FunnelStepStats {
75    pub step_order: i32,
76    pub entered_count: i64,
77    pub exited_count: i64,
78    pub conversion_rate: f64,
79    pub avg_time_to_next_ms: Option<i64>,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct FunnelStats {
84    pub funnel_id: FunnelId,
85    pub funnel_name: String,
86    pub total_entries: i64,
87    pub total_completions: i64,
88    pub overall_conversion_rate: f64,
89    pub step_stats: Vec<FunnelStepStats>,
90}