Skip to content

Commit

Permalink
refactor breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatbrown committed Jan 21, 2025
1 parent b0d1e94 commit b99b51b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
22 changes: 7 additions & 15 deletions Nos/Service/Analytics.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import UIKit
import PostHog
import Dependencies
import Logger
import Sentry
import PostHog
import Starscream
import UIKit

/// An object to manage analytics data, currently wired up to send data to PostHog and registered as a global
/// dependency using the Dependencies library.
class Analytics {

private let postHog: PostHogSDK?

/// When an analytics event is tracked, we also create a breadcrumb in our crash reporter.
@Dependency(\.crashReporting) private var crashReporting

required init(mock: Bool = false) {
let apiKey = Bundle.main.infoDictionary?["POSTHOG_API_KEY"] as? String ?? ""
if !mock && !apiKey.isEmpty {
Expand Down Expand Up @@ -322,7 +324,7 @@ class Analytics {

extension Analytics {
/// Tracks the event with the given properties in the analytics provider.
/// Also calls `trackBreadcrumb` to track this event as a breadcrumb in our error reporting tool (Sentry).
/// Also calls ``CrashReporting/trackBreadcrumb(_:)`` to track this event as a breadcrumb in our crash reporter.
/// - Parameters:
/// - eventName: The event name to track.
/// - properties: The properties to include with the event.
Expand All @@ -334,16 +336,6 @@ extension Analytics {
}
postHog?.capture(eventName, properties: properties)

trackBreadcrumb(eventName)
}

/// Adds a breadcrumb for the given event name for tracking in our error reporting tool (Sentry).
/// - Parameter eventName: The event for which to add a breadcrumb.
private func trackBreadcrumb(_ eventName: String) {
let crumb = Breadcrumb()
crumb.level = SentryLevel.info
crumb.category = "analytics"
crumb.message = eventName
SentrySDK.addBreadcrumb(crumb)
crashReporting.trackBreadcrumb(eventName)
}
}
12 changes: 11 additions & 1 deletion Nos/Service/CrashReporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ class CrashReporting {
Log.error("Reporting error to Crash Reporting service: \(errorMessage)")
sentry.capture(message: errorMessage)
}


/// Adds a breadcrumb for the given event name for tracking in our error reporting tool (Sentry).
/// - Parameter eventName: The event for which to add a breadcrumb.
func trackBreadcrumb(_ eventName: String) {
let crumb = Breadcrumb()
crumb.level = SentryLevel.info
crumb.category = "analytics"
crumb.message = eventName
SentrySDK.addBreadcrumb(crumb)
}

func logout() {
SentrySDK.setUser(nil)
}
Expand Down

0 comments on commit b99b51b

Please sign in to comment.