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?
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.
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:
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.
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:
Creating a Firebase Project
Creating a Firebase project is free. Click Create a project. A new window will appear:
Name your project Cocoabucks. Accept the Firebase terms and click Continue.
Firebase will ask you to add Google Analytics to your Firebase project.
Leave it enabled and click Continue.
The final step is setting your analytics location.
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.
Registering the App
A new form will open asking you to register your app and enter your app’s bundle ID:
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.
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.
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:
In this tutorial, you’ll install Firebase SDK using SwiftPM.
Open Xcode and go to File ▸ Swift Packages ▸ Add Package Dependency:
A new window will open prompting you to enter the package repository URL. Copy and paste https://github.com/firebase/firebase-ios-sdk inside:
Click Next. You’ll see a new window asking you to choose package options:
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:
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:
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:
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.