Ahoy iOS — First-party analytics on iOS
Hi all — happy Halloween! 🎃👻
Today I’d like to introduce Ahoy iOS. It’s a lightweight library for Apple platforms which enables you to quickly and easily set up first-party analytics on your client for sending to your Rails backend. At its core, Ahoy iOS is really just a simple wrapper around the Ahoy API, though it also provides a number of powerful built-in features.
The backend framework this library was built around, Ahoy, was written by Andrew Kane and is a fully-featured analytics tool you can quickly plug into your existing Rails application. It is easy to setup, easy to use, and is first party, so all of your (and your users’) data will remain on your servers and under your control.
Installation
Ahoy iOS, despite its name, also supports macOS, tvOS, and watchOS. It’s set up for integration via Swift Package Manager and its inclusion in your project is as simple as adding the following to your Package.swift
file (or following Apple’s instructions for Xcode).
let package = Package(
name: "YourProject",
… // package definition cont.
dependencies: [
.package(url: "https://github.com/namolnad/ahoy-ios.git", from: "0.2.0"),
… // package definition cont.
)
Usage
Ahoy iOS tracks both visits and events. All events are associated with the current visit, and as such, a visit must first be tracked prior to tracking any subsequent events. To track a visit, create a new Ahoy client (maintain a reference) and use the trackVisit()
method.
ahoy.trackVisit()
.sink(receiveCompletion: { _ in }, receiveOutput: { visit in print(visit) })
.store(in: &cancellables)
After a visit has successfully been tracked, you are free to track events at will. The simplest method of tracking an event is to use the fire-and-forget track(_:properties:)
method.
ahoy.track("Viewed book", properties: ["book_id": 4])
If you’d prefer to minimize the number of calls to your backend, a bulk track(events:)
method is also available.
And that’s really all there is to it — easy as 🥧, right?!?
Given the ever-growing concerns over data privacy, there has never been a better time to ensure you’re owning your tracking events end-to-end and not sharing this data with third parties. Ahoy iOS, Ahoy and Ahoy Android are great tools to make this as easy as possible.
Thanks for reading! Feel free to share your feedback with me and/or contribute to help improve Ahoy iOS.