-
Notifications
You must be signed in to change notification settings - Fork 52
/
AnalyticsKit.swift
234 lines (195 loc) · 8.59 KB
/
AnalyticsKit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import Foundation
@objc
public protocol AnalyticsKitProvider {
func applicationWillEnterForeground()
func applicationDidEnterBackground()
func applicationWillTerminate()
func uncaughtException(_ exception: NSException)
func logScreen(_ screenName: String)
func logScreen(_ screenName: String, withProperties properties: [String: Any])
func logEvent(_ event: String)
func logEvent(_ event: String, withProperty key: String, andValue value: String)
func logEvent(_ event: String, withProperties properties: [String: Any])
func logEvent(_ event: String, timed: Bool)
func logEvent(_ event: String, withProperties properties: [String: Any], timed: Bool)
func endTimedEvent(_ event: String, withProperties properties: [String: Any])
func logError(_ name: String, message: String?, properties: [String: Any]?, exception: NSException?)
func logError(_ name: String, message: String?, properties: [String: Any]?, error: Error?)
}
@objcMembers
public class AnalyticsKit: NSObject {
fileprivate static let DefaultChannel = "default"
static var channels = [String: AnalyticsKitChannel]()
public class func initializeProviders(_ providers: [AnalyticsKitProvider]) {
channel(DefaultChannel).initializeProviders(providers)
}
public class func providers() -> [AnalyticsKitProvider] {
return channel(DefaultChannel).providers
}
public class func channel(_ channelName: String) -> AnalyticsKitChannel {
guard let channel = channels[channelName] else {
AKLog("Created \(channelName) channel")
let newChannel = AnalyticsKitChannel(channelName: channelName, providers: [AnalyticsKitProvider]())
channels[channelName] = newChannel
return newChannel
}
return channel
}
public class func defaultChannel() -> AnalyticsKitChannel {
return channel(DefaultChannel)
}
public class func applicationWillEnterForeground() {
for (_, channel) in channels {
channel.applicationWillEnterForeground()
}
}
public class func applicationDidEnterBackground() {
for (_, channel) in channels {
channel.applicationDidEnterBackground()
}
}
public class func applicationWillTerminate() {
for (_, channel) in channels {
channel.applicationWillTerminate()
}
}
public class func uncaughtException(_ exception: NSException) {
for (_, channel) in channels {
channel.uncaughtException(exception)
}
}
public class func logScreen(_ screenName: String) {
channel(DefaultChannel).logScreen(screenName)
}
public class func logScreen(_ screenName: String, withProperties properties: [String: Any]) {
channel(DefaultChannel).logScreen(screenName, withProperties: properties)
}
public class func logEvent(_ event: String) {
channel(DefaultChannel).logEvent(event)
}
public class func logEvent(_ event: String, withProperty property: String, andValue value: String) {
channel(DefaultChannel).logEvent(event, withProperty: property, andValue: value)
}
public class func logEvent(_ event: String, withProperties properties: [String: Any]) {
channel(DefaultChannel).logEvent(event, withProperties: properties)
}
public class func logEvent(_ event: String, timed: Bool) {
channel(DefaultChannel).logEvent(event, timed: timed)
}
public class func logEvent(_ event: String, withProperties properties: [String: Any], timed: Bool) {
channel(DefaultChannel).logEvent(event, withProperties: properties, timed: timed)
}
public class func endTimedEvent(_ event: String, withProperties properties: [String: Any]) {
channel(DefaultChannel).endTimedEvent(event, withProperties: properties)
}
public class func logError(_ name: String, message: String?, properties: [String: Any]? = nil, exception: NSException?) {
channel(DefaultChannel).logError(name, message: message, properties: properties, exception: exception)
}
public class func logError(_ name: String, message: String?, properties: [String: Any]? = nil, error: Error?) {
channel(DefaultChannel).logError(name, message: message, properties: properties, error: error)
}
}
public class AnalyticsKitChannel: NSObject, AnalyticsKitProvider {
let channelName: String
var providers: [AnalyticsKitProvider]
public init(channelName: String, providers: [AnalyticsKitProvider]) {
self.channelName = channelName
self.providers = providers
}
public func initializeProviders(_ providers: [AnalyticsKitProvider]) {
self.providers = providers
}
public func applicationWillEnterForeground() {
AKLog("\(channelName)")
for provider in providers {
provider.applicationWillEnterForeground()
}
}
public func applicationDidEnterBackground() {
AKLog("\(channelName)")
for provider in providers {
provider.applicationDidEnterBackground()
}
}
public func applicationWillTerminate() {
AKLog("\(channelName)")
for provider in providers {
provider.applicationWillTerminate()
}
}
public func uncaughtException(_ exception: NSException) {
AKLog("\(channelName) \(exception.description)")
for provider in providers {
provider.uncaughtException(exception)
}
}
public func logScreen(_ screenName: String) {
AKLog("\(channelName) \(screenName)")
for provider in providers {
provider.logScreen(screenName)
}
}
public func logScreen(_ screenName: String, withProperties properties: [String: Any]) {
AKLog("\(channelName) \(screenName) withProperties: \(properties.description)")
for provider in providers {
provider.logScreen(screenName, withProperties: properties)
}
}
public func logEvent(_ event: String) {
AKLog("\(channelName) \(event)")
for provider in providers {
provider.logEvent(event)
}
}
public func logEvent(_ event: String, withProperty property: String, andValue value: String) {
AKLog("\(channelName) \(event) withProperty: \(property) andValue: \(value)")
for provider in providers {
provider.logEvent(event, withProperty: property, andValue: value)
}
}
public func logEvent(_ event: String, withProperties properties: [String: Any]) {
AKLog("\(channelName) \(event) withProperties: \(properties.description)")
for provider in providers {
provider.logEvent(event, withProperties: properties)
}
}
public func logEvent(_ event: String, timed: Bool) {
AKLog("\(channelName) \(event) timed: \(timed)")
for provider in providers {
provider.logEvent(event, timed: timed)
}
}
public func logEvent(_ event: String, withProperties properties: [String: Any], timed: Bool) {
AKLog("\(channelName) \(event) withProperties: \(properties) timed: \(timed)")
for provider in providers {
provider.logEvent(event, withProperties: properties, timed: timed)
}
}
public func endTimedEvent(_ event: String, withProperties properties: [String: Any]) {
AKLog("\(channelName) \(event) withProperties: \(properties)")
for provider in providers {
provider.endTimedEvent(event, withProperties: properties)
}
}
public func logError(_ name: String, message: String?, properties: [String: Any]?, exception: NSException?) {
AKLog("\(channelName) \(name) message: \(message ?? "nil") exception: \(exception?.description ?? "nil")")
for provider in providers {
provider.logError(name, message: message, properties: properties, exception: exception)
}
}
public func logError(_ name: String, message: String?, properties: [String: Any]?, error: Error?) {
AKLog("\(channelName) \(name) message: \(message ?? "nil") error: \(error?.localizedDescription ?? "nil")")
for provider in providers {
provider.logError(name, message: message, properties: properties, error: error)
}
}
}
private func AKLog(_ message: String, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
#if DEBUG
print("\(URL(string: file)?.lastPathComponent ?? "") \(function)[\(line)]: \(message)")
#else
if message == "" {
// Workaround for swift compiler optimizer crash
}
#endif
}