Created
August 21, 2020 05:12
-
-
Save achernoprudov/0035939730ac6906766d4fc6a76f3486 to your computer and use it in GitHub Desktop.
Analytics system contract
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 Analytics { | |
typealias Attributes = [String: Any] | |
func log( | |
event eventName: String, | |
with attributes: Attributes? | |
) | |
} | |
public extension Analytics { | |
func log(_ event: AnalyticsEvent) { | |
var parameters: [String: Any] = [:] | |
for (key, value) in event.parameters { | |
switch value { | |
case let bool as Bool: | |
parameters[key.rawValue] = bool.description | |
default: | |
parameters[key.rawValue] = value | |
} | |
} | |
log(event: event.name, with: parameters) | |
} | |
} | |
public enum AnalyticsEvent { | |
case seach(query: String) | |
case userLoggedIn | |
} | |
public extension AnalyticsEvent { | |
enum Key: String, CaseIterable { | |
case query | |
} | |
} | |
public extension AnalyticsEvent { | |
var name: String { | |
switch self { | |
case .seach: | |
return "search" | |
case .userLoggedIn: | |
return "user_logged_in" | |
} | |
} | |
var parameters: [Key: Any] { | |
var result: [Key: Any] = [:] | |
switch self { | |
case .search(let query): | |
result[.query] = query | |
case .userLoggedIn: | |
break | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment