-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathAnalyticsService.swift
More file actions
267 lines (237 loc) Β· 9.32 KB
/
AnalyticsService.swift
File metadata and controls
267 lines (237 loc) Β· 9.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// AnalyticsService.swift
// Plotwist
//
import Foundation
// MARK: - Analytics Events
enum AnalyticsEvent {
// Auth
case signUp(method: String)
case login(method: String)
case logout
// App Lifecycle
case appOpen
// Onboarding
case onboardingStart
case onboardingContentTypeSelected(type: String)
case onboardingGenresSelected(count: Int)
case onboardingTitleAdded(tmdbId: Int, mediaType: String, status: String)
case onboardingComplete(titlesAdded: Int)
// Screens
case screenView(name: String)
// Content Discovery
case searchPerformed(query: String, resultsCount: Int)
case mediaViewed(tmdbId: Int, mediaType: String, title: String)
case categoryViewed(category: String, subcategory: String)
// Core Actions
case mediaStatusChanged(tmdbId: Int, mediaType: String, status: String, source: String)
case mediaStatusRemoved(tmdbId: Int, mediaType: String)
case rewatchAdded(tmdbId: Int, mediaType: String, count: Int)
case episodeCheckin(showId: Int, season: Int, episode: Int)
// Reviews
case reviewStarted(tmdbId: Int, mediaType: String)
case reviewSubmitted(tmdbId: Int, mediaType: String, rating: Double, hasText: Bool)
case reviewDeleted(tmdbId: Int, mediaType: String)
// Stats
case statsView
// Feedback
case feedbackOpen(contextScreen: String)
case feedbackSubmit(type: String)
// Error tracking
case errorAPI(endpoint: String, statusCode: Int)
// Engagement
case shareContent(tmdbId: Int, mediaType: String)
case profileViewed(userId: String, isOwnProfile: Bool)
var name: String {
switch self {
case .signUp: return "sign_up"
case .login: return "login"
case .logout: return "logout"
case .appOpen: return "app_open"
case .onboardingStart: return "onboarding_start"
case .onboardingContentTypeSelected: return "onboarding_content_type"
case .onboardingGenresSelected: return "onboarding_genres"
case .onboardingTitleAdded: return "onboarding_title_added"
case .onboardingComplete: return "onboarding_complete"
case .screenView: return "screen_view"
case .searchPerformed: return "search"
case .mediaViewed: return "media_viewed"
case .categoryViewed: return "category_viewed"
case .mediaStatusChanged: return "status_changed"
case .mediaStatusRemoved: return "status_removed"
case .rewatchAdded: return "rewatch_added"
case .episodeCheckin: return "episode_checkin"
case .reviewStarted: return "review_started"
case .reviewSubmitted: return "review_submitted"
case .reviewDeleted: return "review_deleted"
case .statsView: return "stats_view"
case .feedbackOpen: return "feedback_open"
case .feedbackSubmit: return "feedback_submit"
case .errorAPI: return "error_api"
case .shareContent: return "share"
case .profileViewed: return "profile_viewed"
}
}
var properties: [String: Any] {
switch self {
case .signUp(let method), .login(let method):
return ["method": method]
case .logout, .appOpen, .onboardingStart, .statsView:
return [:]
case .onboardingContentTypeSelected(let type):
return ["content_type": type]
case .onboardingGenresSelected(let count):
return ["genres_count": count]
case .onboardingTitleAdded(let tmdbId, let mediaType, let status):
return ["tmdb_id": tmdbId, "media_type": mediaType, "status": status]
case .onboardingComplete(let titlesAdded):
return ["titles_added": titlesAdded]
case .screenView(let name):
return ["screen_name": name]
case .searchPerformed(let query, let resultsCount):
return ["query": query, "results_count": resultsCount]
case .mediaViewed(let tmdbId, let mediaType, let title):
return ["tmdb_id": tmdbId, "media_type": mediaType, "title": title]
case .categoryViewed(let category, let subcategory):
return ["category": category, "subcategory": subcategory]
case .mediaStatusChanged(let tmdbId, let mediaType, let status, let source):
return ["tmdb_id": tmdbId, "media_type": mediaType, "status": status, "source": source]
case .mediaStatusRemoved(let tmdbId, let mediaType):
return ["tmdb_id": tmdbId, "media_type": mediaType]
case .rewatchAdded(let tmdbId, let mediaType, let count):
return ["tmdb_id": tmdbId, "media_type": mediaType, "rewatch_count": count]
case .episodeCheckin(let showId, let season, let episode):
return ["show_id": showId, "season": season, "episode": episode]
case .reviewStarted(let tmdbId, let mediaType):
return ["tmdb_id": tmdbId, "media_type": mediaType]
case .reviewSubmitted(let tmdbId, let mediaType, let rating, let hasText):
return ["tmdb_id": tmdbId, "media_type": mediaType, "rating": rating, "has_text": hasText]
case .reviewDeleted(let tmdbId, let mediaType):
return ["tmdb_id": tmdbId, "media_type": mediaType]
case .feedbackOpen(let contextScreen):
return ["context_screen": contextScreen]
case .feedbackSubmit(let type):
return ["type": type]
case .errorAPI(let endpoint, let statusCode):
return ["endpoint": endpoint, "status_code": statusCode]
case .shareContent(let tmdbId, let mediaType):
return ["tmdb_id": tmdbId, "media_type": mediaType]
case .profileViewed(let userId, let isOwnProfile):
return ["user_id": userId, "is_own_profile": isOwnProfile]
}
}
}
// MARK: - Analytics Service
class AnalyticsService {
static let shared = AnalyticsService()
private var apiKey: String { Env.posthogAPIKey }
private var ingestionURL: String {
let host = Env.posthogHost
if host.contains("app.posthog.com") {
return "https://us.i.posthog.com/i/v0/e/"
}
let base = host.hasSuffix("/") ? String(host.dropLast()) : host
return "\(base)/i/v0/e/"
}
private var distinctId: String {
if let userId = UserDefaults.standard.string(forKey: "analyticsUserId") {
return userId
}
if let deviceId = UserDefaults.standard.string(forKey: "analyticsDeviceId") {
return deviceId
}
let newDeviceId = UUID().uuidString
UserDefaults.standard.set(newDeviceId, forKey: "analyticsDeviceId")
return newDeviceId
}
private init() {}
func track(_ event: AnalyticsEvent) {
#if DEBUG
print("π Analytics: \(event.name) - \(event.properties)")
#endif
sendEvent(name: event.name, properties: event.properties)
}
func identify(userId: String, properties: [String: Any] = [:]) {
let previousId = UserDefaults.standard.string(forKey: "analyticsDeviceId")
UserDefaults.standard.set(userId, forKey: "analyticsUserId")
#if DEBUG
print("π Analytics: Identified user \(userId)")
#endif
sendIdentify(userId: userId, previousAnonymousId: previousId, properties: properties)
}
func reset() {
UserDefaults.standard.removeObject(forKey: "analyticsUserId")
#if DEBUG
print("π Analytics: Reset user")
#endif
}
/// Convenience for tracking API errors
static func trackAPIError(endpoint: String, statusCode: Int) {
shared.track(.errorAPI(endpoint: endpoint, statusCode: statusCode))
}
// MARK: - Private
private func sendEvent(name: String, properties: [String: Any]) {
guard !apiKey.isEmpty, !apiKey.contains("$(") else { return }
var allProperties = properties
allProperties["$lib"] = "ios"
allProperties["$lib_version"] = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0"
allProperties["$platform"] = "iOS"
allProperties["$device_type"] = "Mobile"
Task {
await sendToPostHog(payload: [
"api_key": apiKey,
"event": name,
"distinct_id": distinctId,
"properties": allProperties,
"timestamp": ISO8601DateFormatter().string(from: Date()),
])
}
}
private func sendIdentify(userId: String, previousAnonymousId: String?, properties: [String: Any]) {
guard !apiKey.isEmpty, !apiKey.contains("$(") else { return }
var personProperties = properties
personProperties["$platform"] = "iOS"
personProperties["$lib"] = "ios"
var identifyProperties: [String: Any] = ["$set": personProperties]
if let previousId = previousAnonymousId, previousId != userId {
identifyProperties["$anon_distinct_id"] = previousId
}
Task {
await sendToPostHog(payload: [
"api_key": apiKey,
"event": "$identify",
"distinct_id": userId,
"properties": identifyProperties,
"timestamp": ISO8601DateFormatter().string(from: Date()),
])
}
}
private func sendToPostHog(payload: [String: Any]) async {
guard let url = URL(string: ingestionURL) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: payload)
let (_, response) = try await URLSession.shared.data(for: request)
#if DEBUG
if let http = response as? HTTPURLResponse, http.statusCode != 200 {
print("π Analytics HTTP \(http.statusCode): \(ingestionURL)")
}
#endif
} catch {
#if DEBUG
print("π Analytics error: \(error)")
#endif
}
}
}
// MARK: - SwiftUI View Extension
import SwiftUI
extension View {
func trackScreen(_ name: String) -> some View {
self.onAppear {
AnalyticsService.shared.track(.screenView(name: name))
}
}
}