If you would like to implement the steps outlined in this blog as you go, you can use the starter app found here. Alternatively, you can follow along with the finished app found here.
Overview
We will start with a functional E-commerce app built with SwiftUI that does not have tracking implemented. From there, we will walk through a complete tracking implementation with Analytics-Swift. The architecture implemented in Analytics-Swift will make it possible for us to add IDFA consent and collection without incorporating another third-party dependency. Finally, with the help of the Firebase Destination Plugin, we will send the events tracked to Firebase.
Segment’s Mobile Architecture
A few years ago, we completely rebuilt our mobile libraries from the ground up with an entirely new architecture. There were a number of reasons for this but the biggest one was customizability. Every customer has their own bespoke needs and we strongly believe that our analytics libraries should reflect and support this reality.
With this in mind, we decided to take a flywheel approach to the new libraries. The idea is that by keeping the core library small and lightweight, customers can “plugin” to it when and where they need. To accomplish this we decided to break down the lifecycle of an analytics event into simple components. You can think of this as a timeline of when an analytics event is created until the time it leaves a device. We came up with the following:
- Before: What happens before the event is created. For instance, has consent been given by the user?
- Enrichment: Data that needs to be added to the event after it has been invoked. An example of this would be adding contextual data if certain criteria have been met.
- Destination: The destination of the event, either Segment or a third party SDK (Device Mode integrations).
- After: What happens after the event lifecycle. This includes clean up operations and things of that nature.
Approaching an analytics event this way makes it much easier to conceptualize your entire implementation and gives you more control over what and when you track. This has become increasingly important as Apple and Google continue to restrict tracking anything useful without a user’s consent. This architecture has been stable for almost two years and is used by customers in every industry from healthcare to media, and of course, retail.
Getting Started
- create a new Apple source in your Segment workspace
- clone the sample app repository<link>
- open starter app in XCode
- add Analytics-Swift via SPM
https://github.com/segmentio/analytics-swift
Once you’ve added Analytics-Swift to the starter app you can start implementing the SDK.
1. Add an Analytics Extension in SegmentShopApp.Swift with your write-key