Skip to content

Commit

Permalink
Merge pull request #1733 from planetary-social/analytics-breadcrumbs
Browse files Browse the repository at this point in the history
Add breadcrumbs to Sentry for all analytics events
  • Loading branch information
joshuatbrown authored Jan 21, 2025
2 parents 2d59c9f + 636f84b commit a374614
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
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

0 comments on commit a374614

Please sign in to comment.