Last active
April 4, 2017 21:31
-
-
Save leoneparise/3ebef3292fc29db5e0093bf083f16edc to your computer and use it in GitHub Desktop.
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
//: Playground - noun: a place where people can play | |
import Foundation | |
protocol EventType { | |
var name: String { get } | |
var category: String? { get } | |
var info: [ String : Any ] { get } | |
} | |
enum SBTEvents:EventType { | |
case action1, action2, action3, timedAction | |
case actionWithOptions(option1:String, option2:Int) | |
var name: String { | |
switch(self) { | |
case .action1: return "Action 1" | |
case .action2: return "Action 2" | |
case .action3: return "Action 3" | |
case .timedAction: return "Timed Action" | |
case .actionWithOptions: return "Action With Options" | |
} | |
} | |
var info: [String : Any] { | |
switch self { | |
case let .actionWithOptions(option1, option2): | |
return [ | |
"option1": option1, | |
"option2": option2 | |
] | |
default: return [:] | |
} | |
} | |
var category: String? { | |
switch self { | |
case .timedAction: return "Some Category" | |
default: return nil | |
} | |
} | |
} | |
class Analytics { | |
private static var shareInstance:Analytics! | |
static var shared:Analytics { | |
if shareInstance == nil { | |
shareInstance = Analytics() | |
} | |
return shareInstance | |
} | |
private init() { | |
/* Initialize some analytics implementations */ | |
} | |
func log(event:EventType) { | |
print("\(event.name) \(event.info)") | |
} | |
func log(screen:String) { | |
print(screen) | |
} | |
func log(event:EventType, withTime time:TimeInterval) { | |
print("\(event.name) \(event.category!) \(time)") | |
} | |
} | |
Analytics.shared.log(event: SBTEvents.action1) | |
Analytics.shared.log(event: SBTEvents.actionWithOptions(option1: "test", option2: 25)) | |
Analytics.shared.log(event: SBTEvents.timedAction, withTime: 2.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment