Kristian Andersen
May 22, 2017 · 2 min read

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) {

or a simple event

func didCompleteSignUp() {
.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.

Swift Digest

Articles on digesting Swift and development for the Apple Eco System

Kristian Andersen

Written by

iOS & Frontend Developer at @HeyNorthplay (formerly @Robocat). Organizer & Founder of @cphfront.

Swift Digest

Articles on digesting Swift and development for the Apple Eco System

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade