Background
Problem
How to add analytics to iOS apps
Firebase and My Day To-Do
Analytics to iOS apps: Analytics Constants
My goal for this solution was to have a file that houses (stores) all the different events that we are logging making it easier to track the events being logged. To accomplish that goal, I created a class called AnalyticsConstants where I defined a struct called ANALYTICS_CONSTANT with three properties id, name and type. The AnalyticsConstants class has several static properties of type ANALYTICS_CONSTANT that correspond to the events we are logging with Firebase. Confusing? yeah I am confused by that statement too, so how about we just look at some code to get an idea
struct ANALYTICS_CONSTANT {
var id = ""
var type = ""
var name = ""
init(id:String, name: String, type: String) {
self.id = id
self.name = name
self.type = type
}
}
static let A_TODO_VISIT = ANALYTICS_CONSTANT(id: "a_tab", name: "TodoTabVisit", type: "PageView")
btw that’s Swift code from the My Day To-Do iOS app
this is what the logEventForFirebase function look like,
func logEventForFirebase(analyticsConstant: AnalyticsConstants.ANALYTICS_CONSTANT)
{
Analytics.logEvent(AnalyticsEventSelectContent, parameters: [
AnalyticsParameterItemName: analyticsConstant.name as NSObject,
AnalyticsParameterItemID: "id-(analyticsConstant.id)" as NSObject,
AnalyticsParameterContentType: analyticsConstant.type as NSObject
])
}
logEventForFirebase(AnalyticsConstants.A_TODO_VISIT)
- to solve a problem
- so an app is a solution to a problem
- we design the UI
- code the logical app flow and
- add a means of storing data etc etc
A central location in iOS app
After thinking about it that way, it would make sense to add analytics to our app via a single class that stores all the methods to log analytics data which the rest of the app code can call. This leads to the thought of having the AnalyticsHelper, which looks like this,
static func logEvent(analyticsConstant: AnalyticsConstants.ANALYTICS_CONSTANT) { Analytics.logEvent(AnalyticsEventSelectContent, parameters: [ AnalyticsParameterItemName: analyticsConstant.name as NSObject, AnalyticsParameterItemID: "id-(analyticsConstant.id)" as NSObject, AnalyticsParameterContentType: analyticsConstant.type as NSObject ]) }
How does it all fit together?
In the interest of clarity I think it would make sense to look how all this code fits together in an iOS app, let’s look at our viewController
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func todoVisit(_ sender: Any) { AnalyticsHelper.logEvent(AnalyticsConstants.A_TODO_VIST) } }
It’s fairly self-explanatory, it’s has a an event that’s triggered when the button is pressed and we are logging that event using our AnalyticsHelper. Next let’s look at our AnalyticsConstants class
import Firebase class AnalyticsHelper { static func logEvent(analyticsConstant: AnalyticsConstants.ANALYTICS_CONSTANT) { Analytics.logEvent(AnalyticsEventSelectContent, parameters: [ AnalyticsParameterItemName: analyticsConstant.name as NSObject, AnalyticsParameterItemID: "id-(analyticsConstant.id)" as NSObject, AnalyticsParameterContentType: analyticsConstant.type as NSObject ]) } }
and finally the AnalyticsConstants class
import Foundation
/*
Acronyms
A = access i.e. access a section
P = push i.e. button push
*/
class AnalyticsConstants {
struct ANALYTICS_CONSTANT {
var id = ""
var type = ""
var name = ""
init(id:String, name: String, type: String) {
self.id = id
self.name = name
self.type = type
}
}
static let A_TODO_VISIT = ANALYTICS_CONSTANT(id: "a_tab", name: "TodoTabVisit", type: "PageView")
static let P_LOGIN = ANALYTICS_CONSTANT(id: "p_login", name: "Login", type: "Button")
}
The only additional property is if we were tracking user login, also I have just named the variables with the starting letter for the type of event it’s associated to i.e. “button push” and “access” certain sections of the app.
p.s. if you need to know more about that entire where it’s extensively discussed.
0 Comments