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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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)
}
Comment on lines +315 to +323
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in CrashReporting, and perhaps Analytics can call it. Still not sure if Analytics should depend on CrashReporting but I am sure this code goes in CrashReporting.

}
Loading