Skip to content

Commit 8b9a725

Browse files
committed
Add user id analytics
1 parent 9553fdb commit 8b9a725

6 files changed

Lines changed: 50 additions & 18 deletions

File tree

Bluerage/Sources/Kit/Managers/KnockManager.swift renamed to Bluerage/Sources/Kit/AuthObserver.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,53 @@ import Foundation
22
import FactoryKit
33
import Combine
44
import Knock
5+
import PostHog
56
import OSLog
7+
import Sentry
8+
import Queue
69

7-
final class KnockManager {
10+
final class AuthObserver {
811

912
@Injected(\.authSession) private var authSession
1013

14+
@Injected(\.knock) private var knock
15+
16+
@Injected(\.postHog) private var postHog
17+
18+
private let queue = AsyncQueue()
19+
1120
private var connection: AnyCancellable?
1221

1322
func start() {
1423
self.connection?.cancel()
1524
self.connection = nil
1625

1726
self.connection = self.authSession.authStatePublisher
18-
.sink { authState in
19-
Task {
27+
.sink { [weak self] authState in
28+
self?.queue.addOperation { [ weak self] in
29+
guard let self else {
30+
return
31+
}
32+
2033
if case .authenticated(let id) = authState {
2134
await Knock.shared.signIn(userId: id, userToken: nil)
35+
36+
self.postHog.identify(id, userProperties: nil)
37+
38+
SentrySDK.setUser(User(userId: id))
2239
} else if case .unauthenticated = authState {
2340
do {
2441
try await Knock.shared.signOut()
2542
} catch {
2643
Logger.knockManager.error("Error signing out of knock")
2744
}
45+
46+
self.postHog.reset()
47+
48+
SentrySDK.setUser(nil)
2849
}
2950
}
30-
}
31-
}
3251

33-
func setup(publishableKey: String, pushChannelId: String?, options: Knock.KnockStartupOptions? = nil) async throws {
34-
try await Knock.shared.setup(publishableKey: publishableKey, pushChannelId: pushChannelId, options: options)
52+
}
3553
}
36-
3754
}

Bluerage/Sources/Kit/Dependencies/KnockManager+Dependancy.swift renamed to Bluerage/Sources/Kit/Dependencies/Knock+Dependancy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import FactoryKit
44

55
extension Container {
66

7-
var knockManager: Factory<KnockManager> {
7+
var knock: Factory<Knock> {
88
self {
9-
return KnockManager()
9+
return Knock.shared
1010
}.shared
1111
}
1212

Bluerage/Sources/UI/AppDelegate.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ final class AppDelegate: KnockAppDelegate {
1717

1818
@Injected(\.authSession) private var authSession
1919

20-
@Injected(\.knockManager) private var knockManager
20+
@Injected(\.knock) private var knock
21+
22+
private let authObserver = AuthObserver()
2123

2224
override func application(_ application: UIApplication,
2325
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
@@ -29,6 +31,8 @@ final class AppDelegate: KnockAppDelegate {
2931

3032
self.setUpPostHog()
3133

34+
self.setUpAuthObserver()
35+
3236
self.setUpAuthSession()
3337

3438
self.setUpNuke()
@@ -52,6 +56,10 @@ final class AppDelegate: KnockAppDelegate {
5256
return [options]
5357
}
5458

59+
private func setUpAuthObserver() {
60+
self.authObserver.start()
61+
}
62+
5563
private func setUpClerk() {
5664
self.clerk.configure(publishableKey: self.env.CLERK_PUBLISHABLE_KEY)
5765

@@ -78,11 +86,9 @@ final class AppDelegate: KnockAppDelegate {
7886
}
7987

8088
private func setUpKnock() {
81-
self.knockManager.start()
82-
8389
Task {
8490
do {
85-
try await self.knockManager.setup(publishableKey: self.env.KNOCK_PUBLISHABLE_KEY,
91+
try await self.knock.setup(publishableKey: self.env.KNOCK_PUBLISHABLE_KEY,
8692
pushChannelId: self.env.KNOCK_CHANNEL_ID)
8793
} catch {
8894
Logger.knockManager.error("Error setting up remote notifications: \(error.localizedDescription, privacy: .public)")

Project.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ let project = Project(
2222
"UIAppFonts": [
2323
"InstrumentSerif-Regular.ttf"
2424
],
25-
"CADisableMinimumFrameDurationOnPhone": true,
26-
"PHPhotoLibraryPreventAutomaticLimitedAccessAlert": true,
2725
"CFBundleDisplayName": "$(BUNDLE_DISPLAY_NAME)",
2826
"CFBundleName": "$(BUNDLE_NAME)",
2927
"POSTHOG_API_KEY": "$(POSTHOG_API_KEY)",
@@ -84,6 +82,7 @@ let project = Project(
8482
.external(name: "SwiftUIIntrospect"),
8583
.external(name: "Get"),
8684
.external(name: "Knock"),
85+
.external(name: "Queue")
8786
],
8887
settings: .settings(
8988
base: .init()

Tuist/Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tuist/Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ let package = Package(
2828
.package(url: "https://github.com/hmlongco/Navigator", from: "1.2.2"),
2929
.package(url: "https://github.com/siteline/swiftui-introspect", from: "26.0.0"),
3030
.package(url: "https://github.com/kean/Get", from: "2.2.1"),
31-
.package(url: "https://github.com/knocklabs/knock-swift.git", from: "1.2.4")
31+
.package(url: "https://github.com/knocklabs/knock-swift", from: "1.2.4"),
32+
.package(url: "https://github.com/mattmassicotte/Queue", from: "0.2.2")
3233
]
3334
)

0 commit comments

Comments
 (0)