Analytics helps you understand how people use your app. Crashlytics tells you when your app crashes and why. Together, they help make your app better and more reliable.
0
0
Analytics and Crashlytics in iOS Swift
Introduction
You want to see which screens users visit most in your app.
You want to know if users complete important actions like signing up or buying something.
You want to find and fix bugs that cause your app to crash.
You want to improve your app based on real user behavior.
You want to monitor app health after releasing updates.
Syntax
iOS Swift
import Firebase // Log an event Analytics.logEvent("event_name", parameters: ["key": "value"]) // Report a non-fatal error Crashlytics.crashlytics().record(error: error)
Make sure to import Firebase to use Analytics and Crashlytics.
Events are logged with a name and optional parameters as key-value pairs.
Examples
This logs that the user viewed the Home screen.
iOS Swift
Analytics.logEvent("screen_view", parameters: ["screen_name": "Home"])
This logs a purchase event with item ID and price.
iOS Swift
Analytics.logEvent("purchase", parameters: ["item_id": "123", "price": 9.99])
This catches an error and reports it to Crashlytics without crashing the app.
iOS Swift
do {
try someRiskyFunction()
} catch {
Crashlytics.crashlytics().record(error: error)
}Sample App
This simple app logs when the main screen is shown and when a purchase button is tapped. It also shows how to catch and report an error to Crashlytics without crashing the app.
iOS Swift
import UIKit import Firebase class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Log screen view event Analytics.logEvent("screen_view", parameters: ["screen_name": "MainView"]) } @IBAction func buyButtonTapped(_ sender: UIButton) { // Log purchase event Analytics.logEvent("purchase", parameters: ["item_id": "001", "price": 4.99]) } func riskyOperation() { do { try performRiskyTask() } catch { // Report error to Crashlytics Crashlytics.crashlytics().record(error: error) } } func performRiskyTask() throws { // Simulate an error throw NSError(domain: "TestError", code: 1, userInfo: nil) } }
OutputSuccess
Important Notes
Always test Analytics and Crashlytics on a real device, as simulators may not send data properly.
Use meaningful event names and parameters to get useful insights.
Crashlytics helps find crashes quickly, but also report non-fatal errors to catch issues early.
Summary
Analytics tracks user actions to help improve your app.
Crashlytics reports crashes and errors to keep your app stable.
Use both together to understand and fix problems in your app.