Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AnalyticsManager class to easily log and manage events in iOS apps
import FirebaseAnalytics
import Crashlytics
enum AnalyticsEvent {
case materialDetails(matName: String?, matNumber: String?)
case addMaterialInTab(tabName: String)
case tabPressed(tabName: String)
case sendOrder(agentCode: String?, customerNumber: String?, totalAmount: NSNumber?, userCurrency: String?, itemsQty: Int?)
case loggedUserInfo(customerNumber: String?, contactEmail: String?, userMobile: String?)
}
extension AnalyticsEvent{
var params: [String : Any] {
switch self {
case .materialDetails(let matName, let matNumber):
return [
"material_name" : matName ?? "" as NSString,
"material_number" : matNumber ?? "" as NSString
]
case .sendOrder(let agentCode, let customerNumber, let totalAmount, let userCurrency, let itemsQty):
return [
"Customer_number" : customerNumber ?? "" as NSString,
"Total_amount" : totalAmount ?? 0 as NSNumber,
"User_currency" : userCurrency ?? "" as NSString,
"Items_quantity" : itemsQty ?? 0 as NSNumber
]
case .addMaterialTab(let tabName),
.tabPressed(let tabName):
return ["tab_name" : tabName as NSString]
case .loggedUserInfo(let customerSAPNumber, let contactEmail, let userMobile):
return [
"customer_number" : customerNumber ?? "" as NSString,
"contact_email" : contactEmail ?? "" as NSString,
"user_mobile" : userMobile ?? "" as NSString,
]
default:
return [:]
}
}
var name: String {
switch self {
case .materialDetails:
return "display_material_details"
case .sendOrder:
return "send_order"
case .tabPressed:
return "tab_pressed"
case .addMaterialTab:
return "add_material_tab"
case .loggedUserInfo:
return "user_logged_in"
}
}
}
class AnalyticsManager{
static let shared = AnalyticsManager()
func log(event: AnalyticsEvent) {
// log event to Firebase
Analytics.logEvent(event.name, parameters: event.params)
// log event to Fabric
Answers.logCustomEvent(withName: event.name, customAttributes: event.params)
}
}
class UseCase {
AnalyticsManager.shared.log(event: .addMaterialTab(tabName: currentTab))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment