Last active
February 5, 2021 21:16
-
-
Save fredlacis/b228430b77ab9ce19166149ca79122e5 to your computer and use it in GitHub Desktop.
A simple way of organizing your app analytics events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Firebase // This can be changed by any other analytics library. | |
class AnalyticsManager{ | |
/// Unique instance of EventManager (SINGLETON) | |
static var shared: AnalyticsManager = AnalyticsManager() | |
// Stored properties | |
// This is the place where you can add variables that are changed in multiple views to use in your events | |
fileprivate init(){} | |
/// Logs an app event in the Analyitics Dashboard | |
/// - Parameters: | |
/// - event: The event declared in the AnalyticsEvent enum with all its parameters. | |
static func logEvent(_ event: AnalyticsEvent) { | |
// #if !DEBUG /* Uncomment this if statement if you want that only the production app send events. */ | |
// This is the function that sends the events to the chosen library, change it as needed. | |
Analytics.logEvent(event.name, parameters: event.parameters) | |
// #endif | |
} | |
} | |
/// All events and its parameters | |
enum AnalyticsEvent { | |
// Examples | |
// case ButtonTapped | |
// case FormSent(number: Int, color: String) | |
} | |
extension AnalyticsEvent{ | |
/// The name sent to the Analyitics Dashboard | |
var name: String { | |
switch self { | |
// Examples | |
// case .ButtonTapped: | |
// return "button_tapped" | |
// | |
// case .FormSent: | |
// return "form_sent" | |
} | |
} | |
} | |
extension AnalyticsEvent{ | |
/// The parameters dictionary sent to the Analyitics Dashboard | |
var parameters: [String : Any]? { | |
switch self { | |
// Examples | |
// case .FormSent(let number, let color): | |
// return ["Number" : number, "Color" : color] | |
default: | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment