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.
Analytics and Crashlytics in 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.
Analytics.logEvent("screen_view", parameters: ["screen_name": "Home"])
Analytics.logEvent("purchase", parameters: ["item_id": "123", "price": 9.99])
do {
try someRiskyFunction()
} catch {
Crashlytics.crashlytics().record(error: error)
}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.
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) } }
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.
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.
Practice
Solution
Step 1: Understand Analytics role
Analytics collects data about how users interact with the app, like button taps or screen views.Step 2: Differentiate from Crashlytics
Crashlytics focuses on crash reports, not user behavior tracking.Final Answer:
To track user actions and app usage patterns -> Option AQuick Check:
Analytics = user action tracking [OK]
- Confusing Analytics with Crashlytics
- Thinking Analytics fixes crashes
- Assuming Analytics manages permissions
"purchase_complete" using Firebase Analytics?Solution
Step 1: Check method signature
The correct method requires event name as a string and parameters as a dictionary (can be empty).Step 2: Validate syntax correctness
Analytics.logEvent("purchase_complete", parameters: [:]) uses a string event name and an empty dictionary, matching the required syntax.Final Answer:
Analytics.logEvent("purchase_complete", parameters: [:]) -> Option AQuick Check:
Correct syntax = Analytics.logEvent("purchase_complete", parameters: [:]) [OK]
- Missing quotes around event name
- Passing nil instead of dictionary
- Omitting parameters argument
Crashlytics.crashlytics().log("User tapped buy button")
fatalError("Simulated crash")Solution
Step 1: Understand Crashlytics log behavior
Callinglogrecords a message that is sent to Crashlytics on next app launch after a crash.Step 2: Analyze crash trigger
fatalErrorcauses an immediate crash, but the log message is preserved for reporting.Final Answer:
The log message is sent to Crashlytics before the app crashes -> Option DQuick Check:
Crashlytics logs persist through crash [OK]
- Assuming logs are lost on crash
- Thinking log prevents crash
- Confusing log with error throwing
Solution
Step 1: Check Crashlytics setup requirements
Crashlytics requires enabling data collection explicitly or via Info.plist to send crash reports.Step 2: Identify missing configuration
Not callingsetCrashlyticsCollectionEnabled(true)disables crash report sending.Final Answer:
You forgot to call Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(true) -> Option BQuick Check:
Enable collection to send crash reports [OK]
- Confusing Analytics logging with Crashlytics setup
- Missing import statements
- Thinking crash method choice affects reporting
"user_level" with value "gold" in Firebase Analytics and also log a non-fatal error in Crashlytics when a payment fails. Which Swift code snippet correctly does both?Solution
Step 1: Set user property correctly
UsesetUserProperty(_:forName:)with value first, then property name.Step 2: Log non-fatal error properly
Userecord(error:)with an NSError instance to report non-fatal errors.Final Answer:
Analytics.setUserProperty("gold", forName: "user_level") Crashlytics.crashlytics().record(error: NSError(domain: "Payment", code: 1, userInfo: nil)) -> Option CQuick Check:
User property + non-fatal error = Analytics.setUserProperty("gold", forName: "user_level") Crashlytics.crashlytics().record(error: NSError(domain: "Payment", code: 1, userInfo: nil)) [OK]
- Swapping user property name and value
- Using log() instead of record(error:) for errors
- Passing wrong types to methods
Ready to prove it?
Pass the test and this pattern moves unknown
