Firebase Events in a Swift project. How to implement them?

Firebase Events can enhance your experience with Google Analytics. Are you an iOS developer who likes to measure your app’s adoption, UX, etc.? Read this tutorial and get to know how your mobile application is used, and monitor custom events.

Firebase Events can add a new dimension to the data you collect via Google Analytics. In this tutorial, you will learn how to get started with Firebase Events, how to add custom actions, and how to handle them with wrappers. OK, without further ado, let’s start!

Table of content

  1. Google Analytics vs Firebase
  2. What Are Firebase Events?
  3. Importing Analytics and Firebase into a Swift project
  4. Firebase Events in Swift: Adding a wrapper
  5. Adding methods for Event Tracking

Google Analytics vs Firebase

If you’re reading this article, you probably know a little bit about Google Analytics.

In short, this platform lets you monitor your app. Not only app crashes (Crashlytics) but also version adoption, audiences (by demographics such as country, age or sex), as well as real-time usage. Analytics also offers information about conversions or performance.

This can be very useful in learning how people use your app or what do they need. But sometimes clients want to get even more information. This is where Google Events comes in handy.

What Are Firebase Events?

As the name suggests, Google Events in Firebase are an option to handle special events in an iOS app. It means that an iOS developer can add new actions that will be observed and reported by Google Analytics. Those can be, for example, a button’s actions, an open UIViewController, or a selected cell in the UITableViewController.

Importing Analytics and Firebase into a Swift project

How to get started with Google Events? Simply, import Google Analytics and Firebase into your project. If you haven’t installed it before, use Cocoa Pods or Swift Package Manager (still in beta!). Don’t forget to check if everything works fine. Firebase should be implemented in AppDelegate (or SceneDelegate) and if it works correctly, you’ll see it in the logs of the Xcode debug area.

Firebase Events

Event Tracking allows you to add four fields that you may use to describe a user's interaction:

  • kGAIEventCategory - for describing event category,
  • kGAIEventAction - for describing actions,
  • kGAIEventLabel - for describing the label and
  • kGAIEventValue - for describing value

Knowing this you can start adding events in your app. I’ll show you how to track the current UIViewController and custom actions. But first, we need to add a wrapper for managing Google Events.

Firebase Events in Swift: Adding a wrapper

In my opinion, adding a wrapper for Google Events is necessary. Why? It’s more elegant, useful and will help you manage all the stuff if you want to remove or replace Google Events in your apps. Using a wrapper is also better because you don’t need to import Firebase everywhere you want to use it.

So firstly, let’s create a new Swift file and call it GoogleAnalyticsWrapper.swift. Then we will create two functions: the first one for tracking the current UIViewController, second - for handling custom actions.

It can look like this:

func logCurrentView(title: String, class: String)

where title is the name that we want to use for an open UIViewController, and class is the class name. Both are optional. Personally, I prefer to create a struct with default names for title - just for easier handling of this function.

The second method (for tracking actions) will look like this:

func logActionStatus(title: String, status: ActionStatus)

In this method you can also customize statuses - for example like this:

enum ActionStatus: String {
    case button_pressed
    case switch_selected
    case navigation_bar_action
    case text_view_started 

Of course, instead of ActionStatus, you can simply use String. And the title for this action can be for example name of the used UIButton. You should also remember that these functions can be easily modified. Google allows adding many parameters for its methods. Use it if you want to pass more information.

Adding methods for Event Tracking

Right now we can implement Google Events methods. Our first function for monitoring UIViewController will look like this:

func logCurrentView(title: String, class: String) {
                           parameters: [AnalyticsParameterScreenName: title,
                                        AnalyticsParameterScreenClass: class])

Let's add a second method:

func logActionStatus(title: String, status: ActionStatus) {
                           parameters: ["name": title,
                                        "status": status])

That’s all for GoogleAnalyticsWrapper, we can move for places where these functions will be used. For example, if we want to track an open UIViewController, we should add the first function from GoogleAnalyticsWrapper.

    override func viewWillAppear(_ animated: Bool) {
        AnalyticsWrapper.logCurrentView(title: "Home", screenClass: "MainViewController")

And if we want to add action - for example for a custom button, this should look like this:

    @objc private func onLogoutAction() {
        AnalyticsWrapper.logActionStatus(title: "onLogoutAction", status: ActionStatus.button_pressed)

Easy peasy lemon squeezy, as they say. Of course, all methods should be tested and checked if they work for your app. You should also remember to test Google Events on a real device because it won’t be launched on Simulator, even if rightfully added.

Start a project with your dedicated software house



We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!