Created
May 24, 2021 11:07
-
-
Save achernoprudov/8536e2c1bfe51e6501d4fda6db774159 to your computer and use it in GitHub Desktop.
Analytics
This file contains hidden or 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
| extension AnalyticsEvent { | |
| static func searchForUser(with query: String) -> AnalyticsEvent { | |
| return AnalyticsEvent(name: "Search for user", attributes: [.query: query]) | |
| } | |
| } |
This file contains hidden or 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
| public struct AnalyticsEvent { | |
| typealias Attributes = [Key: Any] | |
| let name: String | |
| let attributes: Attributes | |
| } | |
| public extension AnalyticsEvent { | |
| enum Key: String, CaseIterable { | |
| case query | |
| case result | |
| case duration | |
| } | |
| } |
This file contains hidden or 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
| public class AnalyticsFacade { | |
| // MARK: - Static | |
| public static let `default` = AnalyticsFacade() | |
| // MARK: - Instance variables | |
| private var services: [AnalyticsService] | |
| // MARK: - Public | |
| public func log(_ event: AnalyticsEvent) { | |
| // Can be on the separate queue | |
| for service in services { | |
| service.log(event) | |
| } | |
| func register(_ service: AnalyticsService) { | |
| services.append(service) | |
| } | |
| } | |
This file contains hidden or 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
| public protocol AnalyticsService { | |
| func log(_ event: AnalyticsEvent) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment