Using Google Analytics in your app without any SDKs

If you’re anything like me it weighs heavily on you every time your “forced” to add a 3rd party blob of binary disaster code to your app. I most often come across these type of libraries when a client wants to add “analytics”.

The one I’ve encountered the most is Google Analytics. It’s a great tool that is relatively simple to use. It has existed pretty much forever and is therefore widespread and well known. Unfortunately, it is like most other analytics solutions for iOS, closed source binary code 😱.

This rubs me the wrong way for a couple of reasons.

I can’t review the code. I really dislike not having the option of knowing the code that goes into the apps that I build. Even more so when the code is designed to track, measure and report user behaviour. These things are usually done in what I consider “creepy” ways. We all remember how twitter used canOpenURL to figure out what apps users install on the iPhone 😒. I am pretty convinced every one else in the business of selling ads (including Google) does similar or “creepier” tactics. Alas that is what you have to deal with when using analytic solutions from ad-selling companies.

Now for the good part

Google Analytics uses something called Google Measurement Protocol behind the scenes. This is a completely open and documented API that anyone can send requests to. It allows us to build our own reporting mechanisms to feed data into Google Analytics 👍.

I’ve built myself a nice little wrapper around that API that makes is easy to ship some basic google analytics into all of the apps that I build for clients. You can use that if you wish, or you can simply built your own 🤓 . It only took ~150 lines of Swift code.

Using my implementation of GoogleReporter it’s easy to track events, screen views and exceptions. Here’s how you would track a screen view:

class BeerViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
GoogleReporter.shared.screenView("Beer")
}
}

or a simple event

func didCompleteSignUp() {
GoogleReporter.shared
.event("Authentication", action: "Signed Up")
}

All you need to do before you get started, is to configure the tracker with the property ID that Google Analytics spits out when adding a new app.

GoogleReporter.shared.configure(withTrackerId: "UA-XXXXX-XX")

If you’re thinking about adding Google Analytics support to your app, you should go and check out GoogleReporter on GitHub.