The web content provides a comprehensive guide to designing a flexible analytics manager for iOS Swift applications, detailing the implementation of an event tracking system that supports multiple analytics SDKs, enhances readability, and facilitates testing and maintenance.
Abstract
The article discusses the importance of analytics in mobile applications, emphasizing the need for a robust analytics engine to track and measure user interactions over time. It outlines the key features of a well-designed analytics system, including support for various analytics SDKs, flexibility in managing events, and the ability to group events for clarity. The author introduces the EventProtocol and Event struct to standardize event properties and simplify event creation, using property wrappers for better organization. The article also addresses the challenge of managing a large number of events by advocating for feature-wise grouping, which enhances code readability and maintainability. Furthermore, it provides examples of implementing loggers and an analytics manager protocol, demonstrating how to route events to different analytics SDKs through a centralized manager. The article concludes with practical code snippets for tracking events and encourages feedback, offering references for further reading.
Opinions
The author believes that information is a crucial asset in the digital age, akin to oil in the 21st century, and that analytics serves as the combustion engine to harness its potential.
The author suggests that a flexible analytics engine is essential for developers to consider various points, such as the ability to add, update, and remove analytics SDKs and events, to maintain best practices.
The use of enums and property wrappers is highly recommended for organizing events in a way that promotes readability and ease of understanding, especially in large applications with hundreds of events.
The author implies that a single-enum, struct, or class approach for managing events can become unwieldy and recommends grouping events feature-wise to improve code organization.
The article promotes the use of a singleton AnalyticsManager class for centralized event tracking, which simplifies the process of sending events across the application.
The author values the importance of a testable and mockable analytics module, although this point is not extensively elaborated upon in the provided content.
The author encourages readers to engage with the content by providing feedback and suggests exploring additional resources for a deeper understanding of the topic.
Design an Analytics or Event Tracking Manager in iOS Swift App
Implement analytics wrapper to support multiple SDK’s and track events across the app
“Information is the oil of the 21st century, and analytics is the combustion engine” — Peter Sondergaard
Why Do You Need Analytics?
To measure and track your work across time.
To understand your visitors, leads, prospects.
To understand, track, and improve the mechanisms used to convert your first visitor into valuable customers.
The main objective is to drive awareness, engagement, and loyalty.
Features
As a developer, you may have to consider many points while designing a flexible analytics engine. Some of these include:
Support for multiple analytics SDKs
Flexibility to add, update, and remove analytics SDKs
Flexible events to add, update, and remove and their properties
Grouping events for better readability
A testable and mockable module
Flexibility to use it anywhere in-app to track events.
I am sure there are many points that still need to be added, but these are some ideal things that need to considered by developers while designing an analytics layer without violating best practices.
Event
We will start with a very basic EventProtocol that will state ideally which properties we need to send in an Event:
protocol EventProtocol {
var name: String { getset }
var params: [String: Any] { getset }
}
Then we will create the Event struct that implements our EventProtocol. We will also make use of propertyWrapper for building events.
I have also added some helper functions in the Event extension for this example:
Mock Events
In this example, we will send two kinds of events: Screen Visit and Click Events.
For convenience’s sake, we have created twoenums in which we will add all ourevents and parameters:
Now, this is the interesting part. When we’re working on a huge app, we have to send events from different parts of our app. There are many approaches to docking all these events, like a single-enum, struct, or class approach where we add all events. But it loses readability when this singleenum, struct, or class grows to 500+ events.
Imagine your manager or lead asks you for event information for any feature. It would be so difficult to find it and listing it all in a single enum, struct, or class would also make your code look sloppy.
The solution is to group our events feature-wise with the help of propertyWrapper. You can see how easy it is for us to create events and our code is much more readable and easily understandable to anyone.
Let’s Implement Loggers
We will first create AnalyticsEventsLoggerProtocol,which will state the basic requirements for any analytics engine (SDK) like:
Then we will implement this protocol to the AnalyticsManagerclass.
This is a singleton class that will hold an array reference of all the analytics engines and it will be the one responsible to send events with trackEvent protocol method. You can check the implementation of this function where it is calling it’s logger trackEventmethod to send events for tracking user actions:
Wrap-Up
Now just call a single line to send events in any part of our app and send events:
// Without Params
AnalyticsManager.shared.trackEvent(FeatureOneEvents.screen)
// With Params
AnalyticsManager.shared.trackEvent(FeatureOneEvents.screen,
params:["source": "Tabs"])
Thank You
I hope this article helps you design your own analytics layer. Do share your feedbackin the comments section.