An often unexplored facet of iOS or mobile app development in general is app analytics. While you can get analytics or summary of app downloads on iTunesConnect, I find that app analytics can help improve the app experience by a mile. Purely because it lets you determine what in your app engages users & what doesn’t. This gives you a plethora of insight which will lead to you creating a better app experience for end users. This post mainly deals with Google Analytics, my preferred analytics tool compared to others available.
First up you need to obtain a Tracking ID
for your app. This ID is what your app analytics will coalesce under. Let’s get one for this tutorial.
Sign in to the Google Analytics page and create a new account. Select ‘Mobile app’, fill up the details (ignore the bit that talks about already tracking an app with GA) & scroll to the bottom to get your Tracking ID.
You will then be taken to your analytics admin page where you will find your Tracking ID of the form UA-XXXXXXXX-Y
. That’s all it takes to setup your analytics on the ‘web’ side of things. Next up, lets get this all working within an app. From the admin page, download Google Analytics iOS SDK. Ignore the ‘Download with admob features’ button as that adds AdMob related stuff to the analytics download package which we won’t be dealing with.
While writing this post, the latest version of Google Analytics or GA in short is 3.10. Locate the downloaded GoogleAnalyticsServicesiOS_3.10.zip
file on your computer and unzip the contents. The important bits from the contents of the folder GoogleAnalyticsServicesiOS_3.10 are the Google Analytics Library header files & the libGoogleAnalyticsServices.a library.
Create or open a new project in Xcode & do the following:
- Add GA headers
Project navigator > Right click > Add files to '[Your Project]' > Select the aforementioned GA Library folder containing the GA headers > Select the 'Copy items if needed' destination checkbox (if unchecked) > Click Add.
- Add the libGoogleAnalyticsServices.a library.
Select your project in the Project navigator > Select Target > Build Phases > Link Binary With Libraries > + > Add Other... > Browse to the downloaded GA folder > Select libGoogleAnalyticsServices.a > Click Open.
- Add the required system frameworks for GA viz.
CoreData.framework, SystemConfiguration.framework & libz.dylib
. Follow similar steps to adding the GA lib file from the above except that you just add the frameworks from the system frameworks popup list.
If you’ve followed correctly, your project structure should resemble the screenshot below.
Time to add some code to get analytics up & running. Open your application delegate .m file & #import "GAI.h"
. Add the following lines inside your didFinishLaunchingWithOptions:
method.
[[GAI sharedInstance] setDispatchInterval:10.0];
[[GAI sharedInstance] trackerWithTrackingId:@"Your GA ID here(UA-XXXXXXXX-Y)"];
This creates a tracker with your given tracking id & sets the dispatch interval at which analytics reporting will be performed. We’ve set it to 10 seconds. The created tracker will persist & can later on be obtained for other purposes which we shall see a little later in this post. The most basic usage of analytics is to track page or screen views & events to gauge user interaction. Lets see how to do the former.
GA provides a GAITrackedViewController
class for automatic screen analytics. It’s as easy as subclassing your view controller & setting it’s screenName property which identifies the screen in the analytics reports. Setting the screenName is a must for tracking if you take the subclassing route. Open up your view controller .h file & #import "GAITrackedViewController.h"
at the top. Change your view controller parent class from UIViewController
to GAITrackedViewController. Open up the view controller .m file & in its viewWillAppear:
method, add the following line
self.screenName = @"Main Screen";
Thats it! Each time your view controller view appears on screen, a screen view tracking hit or record will be created & sent to analytics with the screenName value. If you don’t want to subclass you can also do the same manually. In the view controller .m file #import "GAIDictionaryBuilder.h"
& #import "GAIFields.h"
. Add the block of code below in viewWillAppear:
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Detail Screen"];
[[GAI sharedInstance].defaultTracker send:[[GAIDictionaryBuilder createScreenView] build]];
The above piece of code gets the default tracker which is your tracker with your Tracking ID that was created in the application delegate, builds a dictionary of hit parameters & values for a screen view sent via the tracker instance. Note that it is important to set the kGAIScreenName
or kGAIDescription
else the screen name won’t be set for the tracker hit.
You can view the 2 screen view tracking implementations in the sample project’s View Controller & Detail Controller files. This is as simple as it gets for screen view analytics. If you’ve gotten this far, you will be glad to know that event tracking is as simple as the line of code below.
[[GAI sharedInstance].defaultTracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Button Events" action:@"Button Click" label:@"Hello!" value:nil] build]];
That’s all it takes to record the click of a button titled ‘Hello!’.
Download or clone the sample project from the link at the end of this post to see everything in this tutorial in action. Switch between screens to initiate screen view tracking & hit the Hello! button to do some event tracking. View the ‘Reporting’ page which provides extensive detail about the analytics & also provides Real Time analytics viewing! Don’t worry if you aren’t able to see analytics reporting immediately, it does take a while for the Manhattan plot to come up š
As a bonus, you can also have analytics for exceptions by setting trackUncaughtExceptions
to YES & see GA debug information by setting the log level to kGAILogLevelVerbose
from the available options on the trackers GAILogger
(See sample project).
GA debug info sample:
2014-11-07 14:10:06.177 Heiosenberg[4965:105816] VERBOSE: GoogleAnalytics 3.10 -[GAIBatchingDispatcher persist:] (GAIBatchingDispatcher.m:497): Saved hit: {
parameters = {
"&_crc" = 0;
"&_u" = ".o";
"&_v" = "mi3.1.0";
"&a" = 1779447972;
"&aid" = "com.heiosenberg.wordpress.Heiosenberg";
"&an" = Heiosenberg;
"&av" = "1.0";
"&cd" = "Main Screen";
"&cid" = "70eda709-b530-48f3-b6de-18c79a662507";
"&ds" = app;
"&sr" = 375x667;
"&t" = appview;
"&tid" = "UA-XXXXXXXX-Y";
"&ul" = en;
"&v" = 1;
"&z" = 16805254527687481534;
gaiVersion = "3.10";
};
timestamp = "2014-11-07 08:40:06 +0000";
}
2014-11-07 14:10:06.511 Heiosenberg[4965:105816] VERBOSE: GoogleAnalytics 3.10 -[GAIRequestBuilder requestPostUrl:payload:compression:] (GAIRequestBuilder.m:167): building URLRequest for https://ssl.google-analytics.com/batch
2014-11-07 14:10:06.512 Heiosenberg[4965:105816] VERBOSE: GoogleAnalytics 3.10 -[GAIBatchingDispatcher dispatchWithCompletionHandler:] (GAIBatchingDispatcher.m:612): Sending hit(s) POST: https://ssl.google-analytics.com/batch
2014-11-07 14:10:06.573 Heiosenberg[4965:105762] INFO: GoogleAnalytics 3.10 -[GAIBatchingDispatcher didSendHits:response:data:error:] (GAIBatchingDispatcher.m:208): Hit(s) dispatched: HTTP status 200
2014-11-07 14:10:06.573 Heiosenberg[4965:105816] INFO: GoogleAnalytics 3.10 -[GAIBatchingDispatcher deleteHits:] (GAIBatchingDispatcher.m:509): hit(s) Successfully deleted
2014-11-07 14:10:06.574 Heiosenberg[4965:105816] INFO: GoogleAnalytics 3.10 -[GAIBatchingDispatcher didSendHits:] (GAIBatchingDispatcher.m:219): 2 hit(s) sent
Make sure to use your own Tracking ID in the application delegate to setup tracking & set a suitable dispatch interval. Setting this to 0 or 1~2 I think is bad practice since all the hits are saved & sent which means you don’t need to aggressively send tracking hits. Although this post covers just the basics of GA it is up to you to explore & use the information to your advantage. Find something interesting? Add a comment & let me know!
[Download Source]