Analytics in iOS Applications

Bruno Cruz
Apple Developer Academy | Mackenzie
10 min readFeb 7, 2018

Today, we’re talking about Analytics.

Analytics is the discovery, interpretation, and communication of meaningful patterns in data. It aims at supporting decisions and generating insights, findings, conclusions and evaluations about software systems and their implementations composition, behavior, quality, evolution as well as about the activities of various stakeholders of these processes.

Well, what’s the meaning of that, anyways?

We’re gonna try to boil it down to a few basic concepts using, as an example, an online-shopping application from the very beginning of its life cycle in your mobile phone:

  • The moment you download and open it.
  • At this moment, the application logs that one more user has successfully downloaded and opened the app in his mobile device.
  • The moment you sign up and, afterwards, sign in into it. (Or maybe, you just wont sign in into it anymore. We could also keep track of that.)
  • The user might enter the app once and delete it afterwards. The user might also keep the app in his phone and continue using it afterwards. This data can also be tracked with Analytics services and is extremely important to determine the Churn Rate of your business. The Churn Rate is, in simpler terms, the percentage of users who canceled the service you provide.
  • Viewing a product.

You’ve downloaded the app. You’ve created an account and hopefully, you’re a happy, active user.

You keep browsing through the numerous goods offered in it, never quite committing to buying anything.

Every time you access a product, visualize it and leave it afterwards, this data is logged by the Analytics service. This data is extremely important for the service provider and is capable of determining a wide array of interesting insights about your user, such as: What are their interests? What might have kept them from committing to a purchase? Why do a lot of users see this product but never purchase it?

It is also interesting to keep in mind that you can make similar conclusions from search results in the app’s search bar.

  • Add new product to the Shopping Cart.

After hours of uncommitted browsing, you might just have found the one thing you were looking for. What do you do then? Well, you add it to your Shopping Cart. You might, however, not go though with the purchase and in the end, the article might be left forgotten in the darkness of your Shopping Cart until the next time you actually decide to buy something, for real this time.

One of the most important metrics for eCommerce profit is the Shopping Cart abandonment rate since the average eCommerce store loses over 77% of sales that are initiated. So this kind of data not only can, but should be stored for further analysis.

  • Buying a product.

We don’t really need to mention how important it is to keep track of all of the information relevant to a purchase, right?

We just brushed through a few simple concepts and uses of Analytics tools when it comes to Marketing Optimization and eCommerce. There are, however, a multitude of uses and applications to Analytics Tools.

Still with us? Cool. We’re going to quickly implement an iOS application making use of the concepts we’re learning here.

Oh, and before you ask, we’re not going to implement a full-fledged eCommerce. We’re mocking one: we’ll create basic relationship between different views, data and use flows to ease you understanding, so hold on to your seats!

We’ll kick this tutorial off using our made-available base project — Exotics.

Exotics is a made-up online pet-store. It’s the only place online where you can buy and order the elusive Capybara.

Oh, feel free to clone the repository on Git Hub through https://github.com/KaMagno/handcode-analytics

The project is set up like this :

There are Sign In/ Sign Up screens. Once the user creates an account and signs in, he’ll have access to a product showcase. The user should be able to select a product in the Collection View and go to another screen containing its details.

In the detail screen, the user will be able to send the product to the Shopping Cart, which, by the way, should always be accessible through the App’s tab bar.

Inside the Shopping Cart, the user is able to commit to buy a Product, and they will then be taken to a Checkout screen.

The first step is creating an account on Firebase. If you have one, you can skip this step. If you don’t, you can create one at https://firebase.google.com

After creating your account, create a new project on your Firebase console, as shown below :

When you click it, you’ll be shown the window below.

In the field “Project Name”, insert the name of your firebase project.

You can leave the “Project ID” field as it is.

In “Country/Region”, set the country you wish (like the one you live in).

The screen shown below is your Firebase Project Overview.

Click “Add Firebase to your iOS app.”

This window will pop up.

In “iOS bundle ID”, fill it out exactly as it is filled in your Xcode project’s bundle ID.

The “App Nickname” is optional. Leave it as you wish.

The App Store ID is also optional.

When you’re done, click “Register App”

You will then be taken to the second step : Downloading and adding the .plist File to your Xcode Project.

DEVS BEWARE : You have to add the EXACT same .plist file Firebase has provided you for your project. Inserting any other .plist will result in linking your Xcode Project to another entirely different Firebase project.

This step concerns adding Firebase’s dependencies and libraries on your Xcode project . Google (Firebase’s owner in case you didn’t knew) uses CocoaPods to manage these dependencies. To do so, you’ll have to go through the following steps :

  1. Open your computer’s terminal.
  2. Navigate, through the terminal, to your project’s folder.
  3. Run the following commands :

$ pod init

  1. You’ve just created the Project’s Podfile. Now, double-click on your Podfile (Located in the project’s folder) and open it with a text editor of your preference. Here’s where you dictate which of Firebase’s dependencies you wish to include on your Xcode Project. Type in the following pods, just before the last line of the file :

pod 'Firebase/Core'

  1. Save the file and go back to your terminal. Run the following command :

$ pod install

A .xcworkspace file will have been created in your project’s folder. You will no longer use the .xcodeproj to access your Project, instead, use the .xcworkspace.

The last step concerns adding a small snippet of code in the project’s AppDelegate file.

First, you’ll have to include the import declaration on top of the class, just below “Import UIKit” ,as shown :

import Firebase

Add the following line just before the return expression in the didFinishLaunchingWithOptions function, as shown below :

FirebaseApp.configure()

Build and run your Project. Check out for any errors popping up on your terminal.

Analytics, by the way, is already included in the Firebase/Core library. If everything worked well, you should be good to go.

Keep in mind, however, that data takes about 24 hours to start being processed on your Analytics dashboard.

Talking about Analytics Events :

Events provide insight on what’s happening on your Application. Event’s triggers can be user interactions, system events and errors. Some events are automatically logged by Analytics, while other events can be customized. You’’ll find proper information on automatically collected events here. You can also find a list of common applications for Analytics events here.

Enough small talk! Let’s code!

Before you begin, import the Firebase dependencies on every class you work with as shown :

import Firebase

Open SignInTableViewController.swift

Lookup for the func called login (line 42). This is what it looks like now :

This func is called the moment the user taps the login button. All logic concerning signing in should be contained in it.

See that TODO comment on ? We’re going to replace that with the logic to track a custom event, as shown :

This way, we can track the event of a user sign-in on the Analytics dashboard.

Now, we’re going to do the same to other events spread around the App. Go to SignUpTableViewController.swift and modify the func called signUp, on line 8, as shown :

Now, go to ProductDetailTableViewController.swift

We’re going to use a pre-defined event to track the moment a user views something specific. In this case, the moment the user views a Product Detail.

Modify the func viewDidAppear as such :

Our Product class has no LocationId attribute, because it simply didn’t fit the context as we’re dealing with a mocked-up online store. This parameter is extremely important, as we’re dealing with a Pre-Defined Firebase Analytics Event.

Now, stay in ProductDetailTableViewController.swift yet and look for the func called didPressBuyButton. Modify as shown below :

We’re now going to track wether a Product, once it was added to the Shopping cart, was removed.

Go to ShoppingCartTableViewController.swift and look for the function tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath).

Modify it like this :

Finally, we’re going to track how many purchases were completed. To do that, go to CheckoutTableViewController.swift and, right up in function checkOut, modify it as such :

A good idea is activating Analytics in your debugging console. Here’s how you do that :

In Xcode, select the Product tab, right on top.

Navigate : Product > Scheme > Edit Scheme

Select the Tab Arguments, then, under Arguments Passed on Launch, add a new entry named (Just click on the “+” icon)

-FIRAnalyticsDebugEnabled

It should look like this :

Close the window.

You can now run the Application. If you check the console, you’ll see that Analytics is up and running.

Analytics will only send data to the console when the Application goes into background mode, or if the data is more than an hour old. So, in order to test it properly, we highly recommend you do the following procedure :

  1. Run the project (Either actual device or simulator)
  2. Stop it.
  3. Run the project again and perform an event (Log in, for example).
  4. Tap the home button to go into background mode.

The events are now being tracked and you should be able to see them at Firebase’s console Analytics Debug View since it takes some time for the data to appear at the Events tab.

Talking about User Properties:

Sometimes you can wonder about some characteristics of the users and more often these characteristics could help you to improve your app. Like the devices, gender or even the payment method most used. But remember, “User Properties” are suggested to use in information that user won’t change with such frequency inside the app.

Let’s get some user details now…

Now we will be able to discover the payment method that users selected when are finishing a purchase. Go to CheckoutTableViewController.swift and modify the func checkOut as shown :

Don’t run it yet. First for Firebase Analytics track this information, you must to add it in User Properties tab. Click on that in your console (it is on the left inside of Analytics field ). After select the button “CREATE YOUR FIRST USER PROPERTY”. And add the informations like the figure below.

It is important to pay attention to the String in the first field be the same as the String wrote in code. Description is important once you have a lot of properties to analyze as it can help you understand what this one is. Click on the “CREATE” button.

If you run your application now, you should be able to see in the console that your property is being tracked. But again, you won’t be able to see it in the Dashboard yet, because there is a minimum number necessary to Analytics start to count it. So, you can check if it is correct by going to a DebugView session again.

After you achieve the necessary number of a specific user property, you can use it as a filter in Dashboard tab.

Conclusion:

  • Analytics in an app is a tool for developers to see how their user base behaves. This data can be used in the future to get insights on the app usage, get a better picture of what is effective, what can be changed to lead to more purchases, further engage users, or reduce user rejection of the app. If you see that a feature is commonly used by a lot of users, you give it easier access. A feature is used by few users? You could scrap it entirely or revamp it so more users feel compelled to use it, maybe it is just in a difficult place to find, who knows? Analytics can help give you the needed answers.

--

--