-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathStatsEvent.swift
More file actions
191 lines (157 loc) · 5.32 KB
/
Copy pathStatsEvent.swift
File metadata and controls
191 lines (157 loc) · 5.32 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
import Foundation
/// Analytics events for tracking user interactions within the Stats module
///
/// IMPORTANT: Do not include personally identifiable information (PII) in analytics events.
/// This includes but is not limited to:
/// - User IDs, author IDs, or any unique identifiers
/// - Email addresses
/// - URLs that might contain sensitive information
/// - Post IDs or content identifiers
///
/// Instead, track only:
/// - Event types and categories
/// - Navigation sources
/// - UI states and configurations
/// - Aggregated metrics
public enum StatsEvent {
// MARK: - Screen View Events
/// Main stats screen shown
case statsMainScreenShown
/// Traffic tab shown
case trafficTabShown
/// Realtime tab shown
case realtimeTabShown
/// Subscribers tab shown
case subscribersTabShown
/// Post details screen shown
case postDetailsScreenShown
/// Author stats screen shown
case authorStatsScreenShown
/// Archive stats screen shown
case archiveStatsScreenShown
/// External link stats screen shown
case externalLinkStatsScreenShown
/// Referrer stats screen shown
case referrerStatsScreenShown
// MARK: - Date Range Events
/// Date range preset selected
/// - Parameters:
/// - "selected_preset": The preset selected (e.g., "last_7_days", "last_28_days", "last_90_days", "last_365_days")
case dateRangePresetSelected
/// Custom date range selected
/// - Parameters:
/// - "start_date": Start date in ISO format
/// - "end_date": End date in ISO format
case customDateRangeSelected
// MARK: - Card Events
/// Card shown on screen
/// - Parameters:
/// - "card_type": Type of card (e.g., "chart", "top_list")
/// - "configuration": Card configuration details (e.g., metrics, item type)
case cardShown
/// Card added to dashboard
/// - Parameters:
/// - "card_type": Type of card (e.g., "chart", "top_list")
case cardAdded
/// Card removed from dashboard
/// - Parameters:
/// - "card_type": Type of card
case cardRemoved
// MARK: - Chart Events
/// Chart type changed
/// - Parameters:
/// - "from_type": Previous chart type (e.g., "line", "bar")
/// - "to_type": New chart type
case chartTypeChanged
/// Chart metric selected
/// - Parameters:
/// - "metric": The metric selected (e.g., "visitors", "views", "likes")
case chartMetricSelected
// MARK: - List Events
/// Top list item tapped
/// - Parameters:
/// - "item_type": Type of item (e.g., "posts_and_pages", "authors", "locations", "referrers")
/// - "metric": The metric being sorted by
case topListItemTapped
// MARK: - Navigation Events
/// Stats tab selected
/// - Parameters:
/// - "tab_name": Name of the tab selected
/// - "previous_tab": Name of the previous tab
case statsTabSelected
// MARK: - Error Events
/// Error encountered
/// - Parameters:
/// - "error_type": Type of error (e.g., "network", "parsing", "permission")
/// - "error_code": Specific error code if available
/// - "screen": Where the error occurred
case errorEncountered
}
// MARK: - StatsTracker Protocol
/// Protocol for tracking analytics events in the Stats module
public protocol StatsTracker: Sendable {
/// Send an analytics event
/// - Parameters:
/// - event: The event to track
/// - properties: Additional properties for the event
func send(_ event: StatsEvent, properties: [String: String])
}
// MARK: - StatsTracker Convenience
extension StatsTracker {
/// Convenience method to send events without properties
func send(_ event: StatsEvent) {
send(event, properties: [:])
}
}
// MARK: - Private Extensions
extension DateIntervalPreset {
/// Analytics tracking name for the preset
var analyticsName: String {
switch self {
case .today: "today"
case .thisWeek: "this_week"
case .thisMonth: "this_month"
case .thisQuarter: "this_quarter"
case .thisYear: "this_year"
case .last7Days: "last_7_days"
case .last28Days: "last_28_days"
case .last30Days: "last_30_days"
case .last90Days: "last_90_days"
case .last6Months: "last_6_months"
case .last12Months: "last_12_months"
case .last3Years: "last_3_years"
case .last10Years: "last_10_years"
}
}
}
extension TopListItemType {
/// Analytics tracking name for the item type
var analyticsName: String {
switch self {
case .postsAndPages: "posts_and_pages"
case .authors: "authors"
case .referrers: "referrers"
case .locations: "locations"
case .videos: "videos"
case .externalLinks: "external_links"
case .searchTerms: "search_terms"
case .fileDownloads: "file_downloads"
case .archive: "archive"
}
}
}
extension SiteMetric {
/// Analytics tracking name for the metric
var analyticsName: String {
switch self {
case .views: "views"
case .visitors: "visitors"
case .likes: "likes"
case .comments: "comments"
case .posts: "posts"
case .timeOnSite: "time_on_site"
case .bounceRate: "bounce_rate"
case .downloads: "downloads"
}
}
}