Skip to content

Commit

Permalink
add breadcrumbs to Sentry for all analytics events
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatbrown committed Jan 14, 2025
1 parent 92ae8f6 commit 8fce430
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions Nos/Service/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 8fce430

Please sign in to comment.