Advertisement
  1. Code
  2. iOS SDK
Code

Mobile Analytics with Mixpanel

by
Difficulty:BeginnerLength:MediumLanguages:

Mobile analytics provides a wide range of services to developers. Used with web and mobile analytics, Mixpanel is an established player. In this tutorial, I will demonstrate how Mixpanel sets itself apart from its competitors. I will show you how to get started with Mixpanel and how it can help you with customer retention and engagement, two critical aspects in the current mobile landscape.


Disclaimer

This tutorial is not sponsored or endorsed by Mixpanel. I have been using Mixpanel for quite some time now and I am very pleased with the results I have gotten so far. I have written this tutorial to demonstrate what Mixpanel can do for you and your business.


Learn from Your Customers

When used correctly, mobile analytics can be a valuable source of information. Mobile analytics can be much more than a tool to keep track of the number of active users of an application. If you aim to create an application that customers love and keep coming back to, then mobile analytics is indispensable.

Collecting data is not the only thing that Mixpanel can be used for. The data Mixpanel collects is carefully analyzed and presented in such a way that trends, patterns, and problems quickly reveal themselves. It provides calls to action to further improve and refine your product.

Mixpanel really shines when it comes to customer retention and engagement. Users can be followed over time by associating each user with a unique identifier. This gives you the data and the tools to study why customers stop using your product or why certain features remain underused.


Privacy

Without mobile analytics, it is virtually impossible to get a realistic view of your application's customer base. The active user base of an application is an important metric, because it gives you a good indication of the application's health. A declining user base is a clear indication that the product has some serious flaws that need to be fixed. A declining user base could be due to a usability problem, but it could just as well be a marketing problem - and it often is.

It is critical to mention privacy when discussing mobile analytics. Apple does not like when the privacy of its customers is violated and it has rejected countless applications for this exact reason. You can use Mixpanel without asking your users for any personal information. However, it is important to remember that the customer needs to be aware that you are collecting data, especially if the data contains personal information. It is critical to always respect the privacy of your customers.

In the rest of this tutorial, I will show you how to get started with Mixpanel by creating an account, integrating Mixpanel into an iOS project, and collecting data for analysis. Integrating Mixpanel into an iOS project is easy and Mixpanel API is very intuitive to use.


Step 1: Project Setup

Create a new project in Xcode by selecting the Single View Application template from the list of templates (figure 1). Name your application Analyzed, enter a company identifier, set iPhone for the device family, and check Use Automatic Reference Counting. The rest of the check-boxes can be left unchecked for this project (figure 2). Tell Xcode where you want to save the project and hit the Create button.

Mobile Analytics with Mixpanel: Choosing a Project Template - Figure 1
Figure 1
Mobile Analytics with Mixpanel: Configuring the New Project - Figure 2
Figure 2

Step 2: Adding the Mixpanel Library

Download the latest version of the Mixpanel library for iOS at GitHub and extract the downloaded archive. After unzipping/extracting, look for the "Mixpanel" folder and import it into your Xcode project. When doing so, make sure to check the check-box labeled Copy items into destination group's folder (if needed) and add the Mixpanel library to the Analyzed target (figure 3).

Mobile Analytics with Mixpanel: Adding the Mixpanel Library for iOS - Figure 3
Figure 3

The Mixpanel library depends on the SystemConfiguration and CoreTelephony frameworks, so let's link our project against these frameworks. Click the project in the Project Navigator on the left, select the target named Analyzed from the list of targets, open the Build Phases tab at the top, and expand the Link Binary With Libraries drawer. Click the small plus button to link your project against both frameworks (figure 4).

Mobile Analytics with Mixpanel: Linking the Project Against the Required Frameworks - Figure 4
Figure 4

Unfortunately at the time of writing, the Mixpanel library doesn't support ARC (Automatic Reference Counting). To remedy this, we must add a compiler flag to each of the files of the Mixpanel library. Click the project in the Project Navigator on the left, select the target named Analyzed from the list of targets, open the Build Phases tab at the top, and expand the Compile Sources drawer. Add a compiler flag with value -fno-objc-arc to each file of the Mixpanel library (figure 5).

Mobile Analytics with Mixpanel: Adding Compiler Flags to Each File of the Library - Figure 5
Figure 5

Since we will be using Mixpanel throughout the project, it is a good idea to import the Mixpanel header file in the project's pre-compiled header file (Analyzed-Prefix.pch) as shown below. This makes working with the Mixpanel library a little easier.


Step 3: Creating a Project in Mixpanel

Mixpanel is free for up to 25,000 data points, so I encourage you to create a new Mixpanel account and follow along with me. It takes less than two minutes to create a Mixpanel account (figure 6).

Mobile Analytics with Mixpanel: Creating a Mixpanel Account - Figure 6
Figure 6

Once you are signed in, you need to create a project for your application. I recommend that you create a new project for each application. Click the button labeled My New Project in the top left, enter the name of your iOS application, and click the Create Project button (figure 7).

Mobile Analytics with Mixpanel: Creating a New Project - Figure 7
Figure 7

To start using Mixpanel in your iOS application, you need to copy the project's token, which you can find in the project's settings panel. Click the gear icon in the bottom left (figure 7) and select the Management tab at the top of the settings panel that appears. This will show you a list of project settings including the token of the project, a long alphanumeric string (figure 8). Copy this string to the clipboard.

Mobile Analytics with Mixpanel: Copying the Project Token - Figure 8
Figure 8

Step 4: Mixpanel Setup

Mixpanel needs to be initialized each time the application launches. The designated place to do this is in the application delegate's application:didFinishLaunchingWithOptions: method. Initializing Mixpanel is as easy as calling the sharedInstanceWithToken: class method on the Mixpanel class and passing the project token we copied from the project's settings panel a moment ago. Mixpanel is now aware of the project you just created in the Mixpanel dashboard.


Step 5: Identifying Users

As stated earlier in this tutorial, it is important to identify each user as a separate individual. Mixpanel does this for you automatically by using a hash of the MAC address of the device.

You can fetch this unique identifier by asking the Mixpanel singleton for its distinctId. The setter method of the distinctId property is named identify: (not setDistinctId:). It is possible to set the distinctId property with a unique identifier that you provide. This is important to do because if you store your customers in a remote database, then I recommend that you synchronize that remote database with Mixpanel's database by setting the unique identifier of the customer accordingly.

In addition, you may want to generate your own unique identifier for practical reasons. The hash of the MAC address used by Mixpanel is tied to the device. In other words, if one user has your application installed on multiple devices (e.g., universal applications), then that user will show up as multiple separate users. I usually solve this issue by generating my own unique identifier and storing that identifier in iCloud. Not only does it solve the above issue, it also makes sure that the customer persists across installs. How neat is that?

In the example below, I demonstrate how to generate your own unique identifier and store it in the user defaults database. After generating the unique identifier, we set the distinctId property of the Mixpanel singleton object by sending it a message of identify: and pass the unique identifier. Take a look at the code snippet below for clarification. I have also created a helper method, setupMixpanel, to keep everything organized. Note that I make use of the new NSUUID class that makes generating a unique identifier simple.

 


Step 6: Tracking Events

Tracking events makes Mixpanel interesting. Start by opening MTViewController.xib and add two buttons to the view controller's view. Give one button the title Milestone 1 and the other button the title Milestone 2 (figure 9). Imagine that each button represents a feature or function in your application that you are interested in.

Mobile Analytics with Mixpanel: Creating the User Interface - Figure 9
Figure 9

In the view controller's implementation file, add an action for each button and in Interface Builder connect each action with the corresponding button. For simplicity reasons, I have named the actions reachedMilestone1: and reachedMilestone2:respectively.

To track an event in Mixpanel, send the shared Mixpanel object a message of track: and pass it a string, that is, the name of the event that you would like to track. Take a look at the implementation of the reachedMilestone1: action below.

In addition to tracking an event, it is also possible to associate parameters or properties with an event by using the track:properties: method. The second argument of track:properties: is a dictionary containing the properties you want to link to the event. Keep in mind that only valid property list classes (NSString, NSDate, NSNumber, etc.) can be stored in the properties dictionary. Take a look at the example below for clarification.

Mixpanel sends a number of parameters by default, such as the device model (iPad, iPhone, etc.), the operating system version, the application version, etc. In some cases, you may want to send a fixed set of properties with every event that Mixpanel tracks. To make this easier, you can register so-called super properties. Super properties are sent with every event that Mixpanel tracks, like the default properties I mentioned above. In the example below, I have registered a dictionary of super properties in the setupMixpanel helper method.

Super properties can be cleared by sending the Mixpanel object a message of clearSuperProperties. There are several other methods related to setting and reading super properties, which you can find in the Mixpanel documentation.


Step 7: Mixpanel Dashboard

Trends

You can build and run your application in the iOS Simulator or on a device to test what we have created so far. Mixpanel collects data whenever a user triggers one of the events that we defined earlier. In the background, Mixpanel sends the collected data to its servers whenever possible. Even though Mixpanel does not provide a live view like Google Analytics does, the data usually shows up in a matter of minutes (figure 10).

Mobile Analytics with Mixpanel: The Trends View in Mixpanel - Figure 10
Figure 10

The Trends view gives you a summary of how your application is being used by your customers based on the actions they take. The Trends view is great for studying user engagement. Mixpanel conveniently compares user engagement over time, which gives you an accurate sense of how your application is being used as well as the state and growth of your customer base. Keep in mind that by default the Trends view shows the total number of events tracked. Depending on the event, it can be more useful to only show unique events.

Segmentation

The Segmentation view is an incredibly powerful aspect of the Mixpanel dashboard. It lets you segment customers based on an event parameter or property. In the example below (figure 11), I have segmented customers based on device model, a property that Mixpanel aggregates by default. With Segmentation, the possibilities are virtually endless. For example, the segmentation view tells you what percentage of your customer base is running the latest version of your application. It also tells you what version of iOS your customers are using, which is incredibly helpful if you are thinking about abandoning support for an older iOS version. Such decisions are always difficult to make, but with a tool like Mixpanel you at least have an accurate view of the impact it will have on your customers.

Mobile Analytics with Mixpanel: The Segmentation View in Mixpanel - Figure 11
Figure 11

Conclusion

Mixpanel is one of the top analytics tools that I work with. Mixpanel's success is partly due to the fact that a majority of the data processing is done for you. Data is presented in such a way that makes it easy to pick up on trends and patterns.

Remember that Mixpanel has much more to offer than what I have covered in this tutorial. For example, Mixpanel's push notification integration is a powerful tool to keep customers engaged. The App Store has become incredibly crowded and nowadays customers should not be taken for granted. It is important to do whatever you can to keep your customers engaged. Mixpanel can help you with this.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.