Tracking Android app metrics

When I joined Monzo I was keen to keep a track of metrics that would help us keep a handle on the app’s size and method count. I set up a shell script on our CI to upload the numbers to StatHat each time we merged a change to our develop branch. This was great in theory, but not visible enough to be actually useful — in fact, one of the stats stopped uploading properly some months ago and we didn’t even notice.

We recently started using Danger and I thought it would be a great opportunity to surface these numbers, so we could actually see the impact of each change being merged into the app.

Step 1 — Upload stats when merging into develop

The StatHat docs really helpful here, there are many ways to upload a stat so pick your favourite. I went with Ruby.

Uploading the apk size is pretty simple once you’ve found the right path to it.

Upload apk size with Ruby

The method count is tricker — we’ve use this plugin on our build which outputs a CSV with the method counts and it’s been working perfectly. I just needed to whip up some command line magic to extract the right number ✨

Ignore the first line of the CSV and take the 1st column

How/where you run this script will depend on your setup — we use CircleCI.

Run upload_stats.rb on develop

Step 2 — Retrieve stats from develop

Next up, we need to be able to retrieve the stats we’ve uploaded to compare the latest develop against what we have locally. This was trickier, as you can only retrieve raw data points from StatHat, so we need to filter down all the points and get the most recently uploaded stat.

Step 3— Comment on pull request with changes

Finally, we need to put it all together. We retrieve the apk size from the local branch and compare it against the what it is on develop. Danger runs on CI for all branches (except develop, master and release-*) and will warn us by commenting on the pull request if the app size has changed by more than half a MB.

Originally I had it showing a warning if the apk size didn’t exactly match, but it felt a bit chatty. We felt that if it posts on every change, it will likely start to get ignored.

That’s it! It’s been running for the past week and is working really well so far. If you liked this post, follow me on twitter.