forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-events.ts
More file actions
205 lines (175 loc) · 5.01 KB
/
Copy pathanalytics-events.ts
File metadata and controls
205 lines (175 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Analytics event types for product analytics
// These events track user actions and business metrics for product analytics
export enum AnalyticsEventType {
// Split events
SPLIT_CREATED = 'split.created',
SPLIT_UPDATED = 'split.updated',
SPLIT_COMPLETED = 'split.completed',
SPLIT_DELETED = 'split.deleted',
SPLIT_SHARED = 'split.shared',
// Item events
ITEM_ADDED = 'item.added',
ITEM_UPDATED = 'item.updated',
ITEM_DELETED = 'item.deleted',
// Payment events
PAYMENT_INITIATED = 'payment.initiated',
PAYMENT_COMPLETED = 'payment.completed',
PAYMENT_FAILED = 'payment.failed',
PAYMENT_REFUNDED = 'payment.refunded',
// Participant events
PARTICIPANT_JOINED = 'participant.joined',
PARTICIPANT_LEFT = 'participant.left',
PARTICIPANT_SETTLED = 'participant.settled',
// User events
USER_REGISTERED = 'user.registered',
USER_LOGIN = 'user.login',
USER_LOGOUT = 'user.logout',
USER_PROFILE_UPDATED = 'user.profile.updated',
// Discovery events
SEARCH_PERFORMED = 'search.performed',
FILTER_APPLIED = 'filter.applied',
// Engagement events
PAGE_VIEWED = 'page.viewed',
BUTTON_CLICKED = 'button.clicked',
ERROR_OCCURRED = 'error.occurred',
}
export interface AnalyticsEvent {
// Event metadata
id: string;
type: AnalyticsEventType;
timestamp: string; // ISO 8601
// Actor (who performed the action)
actor: {
userId?: string;
sessionId?: string;
ipAddress?: string;
userAgent?: string;
};
// Context (where the action happened)
context: {
source?: string; // 'web', 'mobile', 'api'
platform?: string;
version?: string;
locale?: string;
};
// Event-specific payload
payload: Record<string, unknown>;
// Resource being acted upon
resource?: {
type: 'split' | 'item' | 'payment' | 'user';
id: string;
};
}
export interface SplitCreatedEvent extends AnalyticsEvent {
type: AnalyticsEventType.SPLIT_CREATED;
payload: {
splitId: string;
totalAmount: number;
currency: string;
participantCount: number;
itemCount: number;
categoryId?: string;
isRecurring: boolean;
source: 'api' | 'web' | 'mobile';
};
}
export interface PaymentCompletedEvent extends AnalyticsEvent {
type: AnalyticsEventType.PAYMENT_COMPLETED;
payload: {
paymentId: string;
splitId: string;
amount: number;
currency: string;
paymentMethod: string;
settlementTimeMs: number;
txHash?: string;
};
}
export interface SearchPerformedEvent extends AnalyticsEvent {
type: AnalyticsEventType.SEARCH_PERFORMED;
payload: {
query: string;
resultCount: number;
filtersApplied: string[];
searchDurationMs: number;
sortOrder?: string;
};
}
// ClickHouse table schema for product analytics events
export const ANALYTICS_EVENTS_TABLE_SCHEMA = `
CREATE TABLE IF NOT EXISTS analytics_events (
id String,
type Enum8(
'split.created' = 1,
'split.updated' = 2,
'split.completed' = 3,
'split.deleted' = 4,
'split.shared' = 5,
'item.added' = 6,
'item.updated' = 7,
'item.deleted' = 8,
'payment.initiated' = 9,
'payment.completed' = 10,
'payment.failed' = 11,
'payment.refunded' = 12,
'participant.joined' = 13,
'participant.left' = 14,
'participant.settled' = 15,
'user.registered' = 16,
'user.login' = 17,
'user.logout' = 18,
'user.profile.updated' = 19,
'search.performed' = 20,
'filter.applied' = 21,
'page.viewed' = 22,
'button.clicked' = 23,
'error.occurred' = 24
),
timestamp DateTime64(3),
-- Actor fields
actor_user_id Nullable(String),
actor_session_id Nullable(String),
actor_ip_address Nullable(String),
actor_user_agent Nullable(String),
-- Context fields
context_source Nullable(String),
context_platform Nullable(String),
context_version Nullable(String),
context_locale Nullable(String),
-- Payload (JSON)
payload String,
-- Resource fields
resource_type Nullable(String),
resource_id Nullable(String),
-- Processing metadata
processed_at Nullable(DateTime64(3))
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(timestamp)
ORDER BY (type, timestamp, id)
TTL timestamp + INTERVAL 90 DAY
SETTINGS index_granularity = 8192;
`;
// Retention configuration
export const ANALYTICS_RETENTION_CONFIG = {
// Raw events retention (90 days for detailed analysis)
rawEventsRetentionDays: 90,
// Aggregated metrics retention (1 year for dashboards)
aggregatedMetricsRetentionDays: 365,
// Daily aggregates retention (3 years for trends)
dailyAggregatesRetentionDays: 1095,
// ClickHouse materialized views TTL
materializedViewsTTL: 365,
};
// Aggregation intervals for materialized views
export const ANALYTICS_AGGREGATION_INTERVALS = {
// Real-time (5-minute windows)
realtime: '5m',
// Hourly aggregates
hourly: '1h',
// Daily aggregates
daily: '1d',
// Weekly aggregates
weekly: '1w',
// Monthly aggregates
monthly: '1M',
};