From 8fce430f007c51b6349e15887b3891c884bc4d0c Mon Sep 17 00:00:00 2001 From: Josh Brown Date: Tue, 14 Jan 2025 12:38:03 -0500 Subject: [PATCH] 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) + } +}