Firebase Analytics: Getting Started
Learn how Firebase Analytics can help you track iOS app usage. By Danijela Vrzan.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Firebase Analytics: Getting Started
25 mins
- Getting Started
- Setting Up Firebase
- Creating a Firebase Project
- Adding Firebase to Xcode
- Registering the App
- Downloading the Config File
- Adding Firebase SDK Using SwiftPM
- Initializing Firebase in Xcode
- Editing Build Settings
- Firebase Analytics Events
- Viewing Events in Dashboard
- Adding Predefined Events
- Adding Custom Events
- Enabling DebugView
- Viewing Events in DebugView
- Viewing Events in the Firebase Console
- User Properties and Audiences
- Filtering With User Properties
- Adding Custom User Properties
- Viewing User Properties in DebugView
- Adding User Properties in the Firebase Console
- Tailoring User Experiences with Audiences
- Where to Go From Here?
Editing Build Settings
Click Cocoabucks in the Document Outline. Choose your target and, under Build Settings, find Other Linker Flags. Add -ObjC inside:
Build and run. Your app has been set up in the background and is already collecting some default analytics.
Go back to the Firebase console in your browser and click Next. Then click Continue to console. Now, you’ve added your iOS app to the Firebase project.
Firebase Analytics Events
You’ve gone through the process of creating a Firebase project and adding an iOS app to it. You haven’t added any code yet, but Firebase is already collecting some default analytics.
Like most of the other services, Firebase uses a system that’s built around events:
Every event has a name as a String
and an optional Dictionary
of parameters. Their values can be either a String
or an Int
.
When people use your app, the interaction fires off many events. They get tracked, coalesced and reported to you in the Firebase Console.
Firebase collects some basic interactions in any app by default:
- language the app is using
- app_exception if the app crashes
- app_update when the app updates to a new version
- first_open when the user launches the app for the first time
- screen_view when a screen transition occurs
You can find a full list of those events in the Firebase Documentation. You’ll need to log most other events yourself inside the app.
Viewing Events in Dashboard
Open the Firebase Console and go to Analytics ▸ Dashboard:
Instead, Firebase has a DebugView where you can see analytics collected in near real-time. You’ll see it in action in a few sections.
Instead, Firebase has a DebugView where you can see analytics collected in near real-time. You’ll see it in action in a few sections.
Check the Dashboard to see how different events are collected and presented. You can see how stable your app is or how many users you had in the last 30 minutes.
Adding Predefined Events
Firebase suggests you use predefined events in your app. The context behind them is already defined, and you can see some extra information.
You’re building an e-commerce app with a neat list of predefined events suitable for that type. You can find them in the Events: Retail/E-commerce list.
One of those events is purchases. You’ll add some of them to the checkout process.
Go back to Xcode and open CheckoutView.swift. Add the following to the top of the file:
import Firebase
You’ll add a predefined event for completing an order. A perfect time to log that event is when the user taps Confirm order.
Replace // TODO 1
inside the action of Confirm order with:
FirebaseAnalytics.Analytics.logEvent(AnalyticsEventPurchase, parameters: [
AnalyticsParameterPaymentType: Self.paymentTypes[paymentType],
AnalyticsParameterPrice: totalPrice,
AnalyticsParameterSuccess: "1",
AnalyticsParameterCurrency: "USD"
])
Firebase Analytics uses logEvent(_:parameters:)
to log all types of events.
In the code above, you log the predefined event named AnalyticsEventPurchase
. You add the predefined parameters named AnalyticsParameterPaymentType
, AnalyticsParameterPrice
and AnalyticsParameterCurrency
.
You assign them values of paymentType
and totalPrice
, based on user selection. The currency parameter has a single value so you give it a default value of USD.
When you start typing AnalyticsEvent
, Xcode will show you a list of all available options:
You can distinguish between events and parameters by looking at their prefixes. Events have the prefix AnalyticsEvent
, while parameters begin with AnalyticsParameter
.
Build and run. Add a few items to your cart. Then view your cart and select Place Order.
Finally, select Confirm Order. Firebase will log a purchase event every time a user confirms an order in your app.
Adding Custom Events
You can create custom events to analyze an event that isn’t already defined or change an existing one. Tracking screens is one of the most common events app developers track in their apps.
Firebase tracks a default event named AnalyticsEventScreenView, but assigns a generic class name to it.
Having NotifyingMulticolumnSplitViewController as a screen name doesn’t tell you much.
In this case, it’s a good idea to create a custom event.
In Xcode, open ProductDetailView.swift and add the following to the top of the file:
import Firebase
Next, replace // TODO 2
inside .onAppear()
with:
// 1
FirebaseAnalytics.Analytics.logEvent("detail_screen_viewed", parameters: [
// 2
AnalyticsParameterScreenName: "product_detail_view",
// 3
"product_name": product.name
])
Here’s a breakdown:
- You log both custom and predefined events with
logEvent(_:parameters:)
. The only difference is you choose whether you’re using a custom or a predefined name for it. So, you name it something descriptive like detail_screen_viewed. - There’s a predefined parameter,
AnalyticsParameterScreenName
, so you add it to the parametersDictionary
. - Finally, you add one custom parameter and name it product_name. Assign a current product’s name as a value to this parameter.
You can mix and match both custom and predefined events and parameters to your liking. Keep your event names short and distinctive since there’s a limit of 40 characters for event names.
Build and run. Firebase will now log your custom event every time the detailed view appears on the phone’s screen.
It’s time to see some analytics in action and what happens to your app in the background.
Enabling DebugView
As you develop apps, it’s helpful to see what’s happening in the background while you code. In this case, you want to see if events are being logged. Firebase has a tool for that called DebugView.
First, you need to enable it. In Xcode, go to Product ▸ Scheme ▸ Edit Scheme. Within the Run event, select Arguments.
In Arguments Passed On Launch, click + and enter -FIRAnalyticsDebugEnabled.
Make sure you include the dash at the beginning.
Close the dialog. Build and run.
There’ll be a lot of code in your Xcode console. Somewhere among the lines, you’ll see entries like this:
Once you enable debug mode, you’ll see several events, and your Xcode console will populate quickly.
Sending analytics data in near real-time is not typical behavior in production.
To be respectful of your user’s battery, Firebase Analytics only sends out analytics data if:
- it’s been sitting around for more than an hour.
- a conversion event is triggered.
- the app goes into the background.