How to add Google Analytics to iOS App? When I first time came to this question I thought that it shouldn’t be tricky. But I was mistaken. And the reason is that Google Analytics iOS SDK didn’t support bitcode. At least until version 3.14. So, let’s talk a little about this topic.
Ok, how to add Google Analytics to iOS App?
It’s easy if you use Cocoapods.
Go to https://cocoapods.org/pods/GoogleAnalytics and add the next line to your podfile
Go to terminal, set your project folder and run
After that you need to get a configuration file on developer.google.com and drag the
GoogleService-Info.plist file you just downloaded into the root of your Xcode project and add it to all targets.
After that add
to AppDelegate and then override the
didFinishLaunchingWithOptions method to configure
// Configure tracker from GoogleService-Info.plist.
[[GGLContext sharedInstance] configureWithError:&configureError];
NSAssert(!configureError, @"Error configuring Google services: %@", configureError);
// Optional: configure GAI options.
GAI *gai = [GAI sharedInstance];
gai.trackUncaughtExceptions = YES; // report uncaught exceptions
gai.logger.logLevel = kGAILogLevelVerbose; // remove before app release
Then add the next code to every viewVillAppear method to every view you want to add to the analytics.
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"YourSceneName"];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
It seems to be easy. But when I started to archive my project I found that Google Analytics iOS SDK doesn’t support bitcode (!). I set Enable Bitcode to NO in Build Settings of my target, but after that online analytics stopped to work properly. I’ve found that since version 3.14 Google Analytics iOS SDK supports bitcode. I’ve read tons of internet pages, looking the solution, but, unfortunately, I could not get it to work with both enabled and with disabled bitcode. May be I was doing something wrong, but I believe, that there is something wrong and I don’t want to waste my time.
So I decided to create my own GAnalytics class based on Measurement Protocol
The Google Analytics Measurement Protocol allows developers to make HTTP requests to send raw user interaction data directly to Google Analytics servers. This allows developers to measure how users interact with their business from almost any environment. Developers can then use the Measurement Protocol to:
- Measure user activity in new environments.
- Tie online to offline behavior.
- Send data from both the client and server.
To use the GAnalytics class in an app, just drag the GAnalytics class files into your project.
Import it with
SetUp trackingID in application:didFinishLaunchingWithOptions:
[GAnalytics sharedAnalytics].trackingID = @"XX-00000000-0";
Using for screenview hitType
Use the next code in viewWillAppear: method if you want to track screenView
[[GAnalytics sharedAnalytics] sendMessage:@"SceneName" forHitType:GAHitTypeScreenView];
Using for event hitType
Use the next code in onButtonPressed: method if you want to track actions
[[GAnalytics sharedAnalytics] sendMessage:@"ButtonPressed" forHitType:GAHitTypeEvent];
This is it. Good luck.