From 8fce430f007c51b6349e15887b3891c884bc4d0c Mon Sep 17 00:00:00 2001 From: Josh Brown Date: Tue, 14 Jan 2025 12:38:03 -0500 Subject: [PATCH 1/3] add breadcrumbs to Sentry for all analytics events --- Nos/Service/Analytics.swift | 38 ++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Nos/Service/Analytics.swift b/Nos/Service/Analytics.swift index 945396d5e..9ceeab57b 100644 --- a/Nos/Service/Analytics.swift +++ b/Nos/Service/Analytics.swift @@ -2,6 +2,7 @@ import Foundation import PostHog import Dependencies import Logger +import Sentry import Starscream /// An object to manage analytics data, currently wired up to send data to PostHog and registered as a global @@ -145,15 +146,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 when the user submits a search on the Discover screen. func searchedDiscover() { @@ -302,3 +294,31 @@ class Analytics { track("Mentions Autocomplete Opened") } } + +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). + /// - 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) + + 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) + } +} From b99b51b8a105acd6b9cb90254f78bd44086e443f Mon Sep 17 00:00:00 2001 From: Josh Brown Date: Tue, 21 Jan 2025 15:40:03 -0500 Subject: [PATCH 2/3] refactor breadcrumbs --- Nos/Service/Analytics.swift | 22 +++++++--------------- Nos/Service/CrashReporting.swift | 12 +++++++++++- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Nos/Service/Analytics.swift b/Nos/Service/Analytics.swift index c363d60e0..50f23e8c7 100644 --- a/Nos/Service/Analytics.swift +++ b/Nos/Service/Analytics.swift @@ -1,9 +1,8 @@ -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. @@ -11,6 +10,9 @@ 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 { @@ -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. @@ -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) } } diff --git a/Nos/Service/CrashReporting.swift b/Nos/Service/CrashReporting.swift index ada1b0d0a..60f38aa17 100644 --- a/Nos/Service/CrashReporting.swift +++ b/Nos/Service/CrashReporting.swift @@ -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) } From 636f84b53633b324697e3dcbef8422ed8937e99f Mon Sep 17 00:00:00 2001 From: Josh Brown Date: Tue, 21 Jan 2025 15:40:59 -0500 Subject: [PATCH 3/3] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5289dba8f..d997f9608 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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