Firebase Analytics: Getting Started

Learn how Firebase Analytics can help you track iOS app usage. By Danijela Vrzan.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Editing Build Settings

Click Cocoabucks in the Document Outline. Choose your target and, under Build Settings, find Other Linker Flags. Add -ObjC inside:

Xcode project showing steps how to add -ObjC flag to Other Linker Flags in the Build Settings tab

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:

Custom image of an event anatomy and how an event has a name that can be a String and parameters that are Dictionary with key value pairs

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 AnalyticsDashboard:

Firebase Console screen showing Dashboard with events that were recorded in the past 30 days

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.

Note: It’ll take approximately 24 hours to see any analytics in the Dashboard. You don’t have to wait to continue with the tutorial but note that you won’t see any analytics inside it.

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:

Xcode project showing a code file and how typing for AnalyticsEvents shows a list of all available predefined events

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.

Confirm your order in Cocoabucks

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:

  1. 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.
  2. There’s a predefined parameter, AnalyticsParameterScreenName, so you add it to the parameters Dictionary.
  3. 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.

Cocoabucks detail view with analytics

It’s time to see some analytics in action and what happens to your app in the background.

Xcode mascot doing magic and showing a bunny from a hat

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 ProductSchemeEdit Scheme. Within the Run event, select Arguments.

In Arguments Passed On Launch, click + and enter -FIRAnalyticsDebugEnabled.

Xcode project screen showing how to enable debug view mode in Edit Scheme

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:

DebugView logs in the Cocoabucks console

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.
Note: Leave debug mode on because you’ll use it for the rest of this tutorial. You can disable debug mode anytime by changing the argument passed on launch to -noFIRAnalyticsDebugEnabled. Then delete and reinstall your app in the simulator since enabling the debug mode is saved to disk.