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

The day has finally come. Your Xcode project is building, and you’ve submitted your app to the App Store.

Now what? You brag on Twitter and beg your followers to install your app.

A few days later, you receive your first review. Someone named RayFromVA gave you a two-star review with a note: It could be better.

What does that mean?

You open your app. Everything works the way you intended. How do you know what could be better?

You could bribe your family to give you reviews, but Firebase Analytics offers a better solution.

In this Firebase Analytics tutorial, you’ll build Cocoabucks, a mobile storefront for a new coffee chain.

Along the way, you’ll learn how to:

  • Set up Firebase SDK with SwiftUI using Swift Package Manager
  • Log custom and predefined events
  • Analyze user behavior with custom user properties
  • Provide personalized user experiences using audiences

It’s worth noting that Google Analytics for Firebase has gone through many iterations over the years. Google renamed it Google Analytics in 2019, but everyone still calls it Firebase Analytics.

Note: This tutorial assumes you know the basics of SwiftUI. If you’re new to SwiftUI, check out the SwiftUI: Getting Started tutorial first. You must have Xcode 12 installed to follow this tutorial. Firebase Analytics only works with SwiftPM on Xcode 12 or later.

Getting Started

Download the starter project by clicking the Download Materials button at the top or bottom of the tutorial.

Open Cocoabucks.xcodeproj inside starter. Build and run to see the app in action:

Cocoabucks app walkthrough in the simulator gif

In Xcode, take a look at the main files:

  • products.json contains the app’s data.
  • Cocoabucks.swift is the app’s entry point. All the life-cycle methods go here.
  • ProductListView.swift is the app’s main view and displays a list of products. It has two buttons. One is for an analytics questionnaire you’ll use to assign custom user properties. The other is a cart button that leads to a checkout view.
  • ProductDetailView.swift shows detailed information about a selected product. Add to cart adds the product to the cart.
  • CheckoutView.swift is a Form for displaying payment information and finalizing the order.
Note: If you’re not familiar with the concept of mobile analytics, checkout this tutorial on Getting Started with Mobile Analytics.

To use Firebase Analytics in your app, you first need to have a Firebase project set up.

Setting Up Firebase

Go to the Firebase homepage and click Get Started.

If you aren’t signed in with your Google account, enter your credentials. If you don’t have an account, create a new one.

You’ll then see a Firebase Console welcome screen:

Firebase welcome screen with first visit

Creating a Firebase Project

Creating a Firebase project is free. Click Create a project. A new window will appear:

Step 1 screen of adding new Firebase project and giving it a name

Name your project Cocoabucks. Accept the Firebase terms and click Continue.

Firebase will ask you to add Google Analytics to your Firebase project.

Step 2 screen of adding a new project to Firebase and enabling Google Analytics

Leave it enabled and click Continue.

Note: If you have existing Firebase projects, some of these steps may not appear as you’ve already made the selections for your account.

The final step is setting your analytics location.

Step 3 of adding new project to the Firebase and selecting your location or the location of your organization

Setting a location doesn’t mean Google won’t track users from other countries. It only wants to know where your organization is or where you live, like it doesn’t know that already.

Accept the data-sharing terms and click Create project.

Click Continue, wait a bit, and you’ll see your project’s Console.

Adding Firebase to Xcode

Firebase Console is a container for your project’s Firebase services. You’ll use it to see your app’s analytics. Click the circle iOS button below Get started by adding Firebase to your app.

On Firebase Console screen an arrow pointing up to the iOS button that creates a new iOS app in the Firebase project

Registering the App

A new form will open asking you to register your app and enter your app’s bundle ID:

Step 1 screen of adding an iOS app to the Firebase project and giving it a name

To set your app’s bundle ID, first head back to Xcode. Select Cocoabucks.xcodeproj. Then select your target and choose Signing & Capabilities.

Next, set your Apple Developer account under the Team drop-down menu. Finally, set the Bundle Identifier for your app following this format: com.[your name].cocoabucks. Be sure to fill in your name with all lowercase letters and no spaces.

Xcode project screen showing steps how to find your apps bundle ID under Settings & Capabilities tab

Note: Not sure how to set up your personal team and bundle ID in Xcode? Check out this Your First App in the App Store tutorial.

Copy your bundle ID from Xcode. Go back to Firebase and paste it under iOS Bundle ID.

Click Register app.

Downloading the Config File

Next, download GoogleService-Info.plist. Drag it to your Xcode project by following the outlined instructions.

Step 2 screen of adding an iOS app to the Firebase project and downloading config file to import it in your Xcode project

When prompted by Xcode, check Copy Items if needed.

Click Next and you’ll see a prompt to Add Firebase SDK to your Xcode.

Adding Firebase SDK Using SwiftPM

By default, Firebase tells you to add the SDK using CocoaPods:

Step 3 screen of adding an iOS app to the Firebase project and showing how to import Firebase SDK using CocoaPods

In this tutorial, you’ll install Firebase SDK using SwiftPM.

Note: At the time of writing, Firebase supports installation via SwiftPM in Beta. There are some known issues and you might get an error if you try running the app on your device. If you do, please refer to this workaround to fix it.

Open Xcode and go to FileSwift PackagesAdd Package Dependency:

Xcode project screen showing how to add Swift Package to Xcode project

A new window will open prompting you to enter the package repository URL. Copy and paste https://github.com/firebase/firebase-ios-sdk inside:

Xcode project screen showing step 1 in adding Swift Package and a field to enter package url

Click Next. You’ll see a new window asking you to choose package options:

Xcode project screen showing step 2 in adding Swift Package and suggested settings for import

Since these are the recommended options, leave everything as-is. Click Next.

Now, SwiftPM will fetch a list of all available libraries. Once it finishes, check FirebaseAnalytics:

Xcode project screen showing step 3 in adding Swift Package and a list of all Firebase tools

Click Finish and SwiftPM will do everything for you. In the Document Outline, you’ll see it added Swift Package Dependencies to your project. Xcode might take a while to process everything:

Document Outline in Xcode showing a list of newly added Swift Package Dependencies

Now go back to the Firebase Console and click Next.

Initializing Firebase in Xcode

To connect Firebase when your app starts, you need to initialize it inside your main class:

Step 4 screen of adding an iOS app to the Firebase project and how to initialize Firebase SDK in Xcode project using UIKit

Firebase documentation doesn’t address SwiftUI or it’s new App life cycle yet. It has instructions for UIKit. You’ll learn how to do it in SwiftUI.

Go back to Xcode and open Cocoabucks.swift. Add the following to the top of the file:

import Firebase

Next, add the following below // Initialize Firebase:

init() {
  FirebaseApp.configure()
}

The code above starts the Firebase service once your app finishes launching.

Before you build your project, you need to add a few more things to avoid getting errors or warnings from Xcode.

Note: Make sure you don’t skip this step. If you do, Analytics won’t work.