Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add breadcrumbs to Sentry for all analytics events #1733

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Localized strings on the feed filter drop-down view.
- Disabled automatic tracking in Sentry. [#126](https://github.com/verse-pbc/issues/issues/126)
- Track TestFlight vs AppStore installations in Posthog. [#130](https://github.com/verse-pbc/issues/issues/130)
- Track breadcrumbs in Sentry for all analytics events. [#125](https://github.com/verse-pbc/issues/issues/125)

## [1.1] - 2025-01-03Z

Expand Down
34 changes: 23 additions & 11 deletions Nos/Service/Analytics.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import UIKit
import PostHog
import Dependencies
import Logger
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 @@ -145,15 +148,6 @@ class Analytics {
track("Logged out")
postHog?.reset()
}

private func track(_ eventName: String, properties: [String: Any] = [:]) {
if properties.isEmpty {
Log.info("Analytics: \(eventName)")
} else {
Log.info("Analytics: \(eventName): \(properties)")
}
postHog?.capture(eventName, properties: properties)
}

/// Tracks the source of the app download when the user launches the app.
func trackInstallationSourceIfNeeded() {
Expand Down Expand Up @@ -327,3 +321,21 @@ class Analytics {
track("Mentions Autocomplete Opened")
}
}

extension Analytics {
/// Tracks the event with the given properties in the analytics provider.
/// 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.
private func track(_ eventName: String, properties: [String: Any] = [:]) {
if properties.isEmpty {
Log.info("Analytics: \(eventName)")
} else {
Log.info("Analytics: \(eventName): \(properties)")
}
postHog?.capture(eventName, properties: properties)

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
Loading