Guest User

Untitled

a guest
Dec 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import Foundation
  2.  
  3. // this is either a third party SDK interface or your own analytics client implementation
  4. // that actually sends JSON across the wire to deliver tracked analytics events
  5. protocol AnalyticsClient {
  6. func sendAnalyticsDataToTheBackend(_ eventJSON: [String: Any])
  7. }
  8.  
  9. final class ConcreteAnalyticsClient: AnalyticsClient {
  10. func sendAnalyticsDataToTheBackend(_ eventJSON: [String: Any]) {
  11. print("sending event json: \(eventJSON)")
  12. }
  13. }
  14.  
  15. // This is the analytics interface that the rest of your app relies on
  16. protocol AnalyticsInterface {
  17. init(_ analyticsClient: AnalyticsClient)
  18.  
  19. func track(_ event: AnalyticsEvent)
  20. }
  21.  
  22. // this is a concrete implementation of analytics that is used by your app
  23. final class Analytics: AnalyticsInterface {
  24.  
  25. private let analyticsClient: AnalyticsClient
  26.  
  27. required init(_ analyticsClient: AnalyticsClient) {
  28. self.analyticsClient = analyticsClient
  29. }
  30.  
  31. func track(_ event: AnalyticsEvent) {
  32. print("tracking event \(event.asDictionary())")
  33. // here it suppose to use the injected analyitcs client actually issue a request to send over the JSON of the tracked event
  34. analyticsClient.sendAnalyticsDataToTheBackend(event.asDictionary())
  35. }
  36. }
  37.  
  38. // this is a generic event that can be used for various kind of event tracking across your app
  39. class AnalyticsEvent {
  40. let eventName: String
  41. var uiElement: String?
  42. var screen: String?
  43.  
  44. init(_ eventName: String, uiElement: String? = nil, screen: String? = nil) {
  45. self.eventName = eventName
  46. self.uiElement = uiElement
  47. self.screen = screen
  48. }
  49.  
  50. func asDictionary() -> [String: String] {
  51. return [
  52. "event_name": eventName,
  53. "screen": screen != nil ? screen! : "",
  54. "element": uiElement != nil ? uiElement! : ""
  55. ]
  56. }
  57. }
  58.  
  59. // this is a case specific event that is crafted only for tracking login
  60. class LoginAnalyticsEvent: AnalyticsEvent {
  61. let loggedInUserId: Int
  62.  
  63. init(loggedInUserId: Int) {
  64. self.loggedInUserId = loggedInUserId
  65. super.init("User Logged In", uiElement: nil, screen: nil)
  66. }
  67.  
  68. override func asDictionary() -> [String : String] {
  69. let eventDictionary = super.asDictionary()
  70.  
  71. var fullDictionary = [
  72. "user_id": String(loggedInUserId)
  73. ]
  74. fullDictionary.merge(eventDictionary) { (newDictionaryValue, _) -> String in return newDictionaryValue }
  75.  
  76. return fullDictionary
  77. }
  78. }
  79.  
  80.  
  81. // This is how you'd use analytics and events across your codebase
  82.  
  83. let analyticsClient = ConcreteAnalyticsClient()
  84. let analytics = Analytics(analyticsClient)
  85.  
  86. var buttonTappedEvent = AnalyticsEvent("Tapped")
  87. buttonTappedEvent.screen = "My Awesome Screen"
  88. analytics.track(buttonTappedEvent)
  89.  
  90. let loginEvent = LoginAnalyticsEvent(loggedInUserId: 3)
  91. analytics.track(loginEvent)
Add Comment
Please, Sign In to add comment