/
Amplitude.swift
290 lines (254 loc) · 8.35 KB
/
Amplitude.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import Foundation
public class Amplitude {
var configuration: Configuration
var instanceName: String
var _inForeground = false
internal var _sessionId: Int64 = -1
var state: State = State()
var contextPlugin: ContextPlugin
/**
Sets a block to be called when location (latitude, longitude) information can be passed into an event.
let locationInfo = LocationInfo(lat: 37.7, lng: 122.4)
Amplitude.testInstance.locationInfoBlock = {
return locationInfo
}
*/
public var locationInfoBlock: LocationInfoBlock?
lazy var storage: any Storage = {
return self.configuration.storageProvider
}()
lazy var timeline: Timeline = {
let timeline = Timeline()
timeline.amplitude = self
return timeline
}()
public lazy var logger: (any Logger)? = {
return self.configuration.loggerProvider
}()
public init(
configuration: Configuration,
instanceName: String = Constants.Configuration.DEFAULT_INSTANCE
) {
self.configuration = configuration
self.instanceName = instanceName
let contextPlugin = ContextPlugin()
self.contextPlugin = contextPlugin
// required plugin for specific platform, only has lifecyclePlugin now
if let requiredPlugin = VendorSystem.current.requiredPlugin {
_ = add(plugin: requiredPlugin)
}
_ = add(plugin: contextPlugin)
_ = add(plugin: AmplitudeDestinationPlugin())
timeline.start()
}
convenience init(apiKey: String, configuration: Configuration) {
configuration.apiKey = apiKey
self.init(configuration: configuration)
}
@discardableResult
public func track(event: BaseEvent, options: EventOptions? = nil, callback: EventCallback? = nil) -> Amplitude {
if options != nil {
event.mergeEventOptions(eventOptions: options!)
}
if callback != nil {
event.callback = callback
}
process(event: event)
return self
}
@discardableResult
public func track(eventType: String, eventProperties: [String: Any]? = nil, options: EventOptions? = nil)
-> Amplitude
{
let event = BaseEvent(eventType: eventType)
event.eventProperties = eventProperties
if let eventOptions = options {
event.mergeEventOptions(eventOptions: eventOptions)
}
process(event: event)
return self
}
@discardableResult
@available(*, deprecated, message: "use 'track' instead")
public func logEvent(event: BaseEvent) -> Amplitude {
return track(event: event)
}
@discardableResult
public func identify(userProperties: [String: Any]?, options: EventOptions? = nil) -> Amplitude {
return identify(identify: convertPropertiesToIdentify(userProperties: userProperties), options: options)
}
@discardableResult
public func identify(identify: Identify, options: EventOptions? = nil) -> Amplitude {
let event = IdentifyEvent()
event.userProperties = identify.properties as [String: Any]
if let eventOptions = options {
event.mergeEventOptions(eventOptions: eventOptions)
if eventOptions.userId != nil {
_ = setUserId(userId: eventOptions.userId)
}
if eventOptions.deviceId != nil {
_ = setUserId(userId: eventOptions.deviceId)
}
}
process(event: event)
return self
}
private func convertPropertiesToIdentify(userProperties: [String: Any]?) -> Identify {
let identify = Identify()
userProperties?.forEach { key, value in
_ = identify.set(property: key, value: value)
}
return identify
}
@discardableResult
public func groupIdentify(
groupType: String,
groupName: String,
groupProperties: [String: Any]?,
options: EventOptions? = nil
) -> Amplitude {
return groupIdentify(
groupType: groupType,
groupName: groupName,
identify: convertPropertiesToIdentify(userProperties: groupProperties),
options: options
)
}
@discardableResult
public func groupIdentify(
groupType: String,
groupName: String,
identify: Identify,
options: EventOptions? = nil
) -> Amplitude {
let event = GroupIdentifyEvent()
var groups = [String: Any]()
groups[groupType] = groupName
event.groups = groups
event.groupProperties = identify.properties
if let eventOptions = options {
event.mergeEventOptions(eventOptions: eventOptions)
}
process(event: event)
return self
}
@discardableResult
public func setGroup(
groupType: String,
groupName: String,
options: EventOptions? = nil
) -> Amplitude {
let event = IdentifyEvent()
event.groups = [groupType: groupName]
track(event: event, options: options)
return self
}
@discardableResult
public func setGroup(
groupType: String,
groupName: [String],
options: EventOptions? = nil
) -> Amplitude {
let event = IdentifyEvent()
event.groups = [groupType: groupName]
track(event: event, options: options)
return self
}
@discardableResult
@available(*, deprecated, message: "use 'revenue' instead")
public func logRevenue() -> Amplitude {
return self
}
@discardableResult
public func revenue(
revenue: Revenue,
options: EventOptions? = nil
) -> Amplitude {
guard revenue.isValid() else {
logger?.warn(message: "Invalid revenue object, missing required fields")
return self
}
let event = revenue.toRevenueEvent()
if let eventOptions = options {
event.mergeEventOptions(eventOptions: eventOptions)
}
_ = self.revenue(event: event)
return self
}
@discardableResult
public func revenue(event: RevenueEvent) -> Amplitude {
process(event: event)
return self
}
@discardableResult
public func add(plugin: Plugin) -> Amplitude {
plugin.setup(amplitude: self)
timeline.add(plugin: plugin)
return self
}
@discardableResult
public func remove(plugin: Plugin) -> Amplitude {
timeline.remove(plugin: plugin)
return self
}
@discardableResult
public func flush() -> Amplitude {
timeline.apply { plugin in
if let _plugin = plugin as? EventPlugin {
_plugin.flush()
}
}
return self
}
@discardableResult
public func setUserId(userId: String?) -> Amplitude {
try? storage.write(key: .USER_ID, value: userId)
state.userId = userId
return self
}
@discardableResult
public func setDeviceId(deviceId: String?) -> Amplitude {
try? storage.write(key: .DEVICE_ID, value: deviceId)
state.deviceId = deviceId
return self
}
@discardableResult
public func setSessionId(sessionId: Int64) -> Amplitude {
_sessionId = sessionId
_ = try? self.storage.write(key: .PREVIOUS_SESSION_ID, value: sessionId)
return self
}
@discardableResult
func reset() -> Amplitude {
_ = setUserId(userId: nil)
_ = setDeviceId(deviceId: nil)
contextPlugin.initializeDeviceId()
return self
}
public func apply(closure: (Plugin) -> Void) {
timeline.apply(closure)
}
private func process(event: BaseEvent) {
if configuration.optOut {
logger?.log(message: "Skip event based on opt out configuration")
return
}
event.timestamp = event.timestamp ?? Int64(NSDate().timeIntervalSince1970 * 1000)
timeline.process(event: event)
}
func onEnterForeground(timestamp: Int64) {
_inForeground = true
let dummySessionStartEvent = BaseEvent(
timestamp: timestamp,
sessionId: -1,
eventType: Constants.AMP_SESSION_START_EVENT
)
timeline.process(event: dummySessionStartEvent)
}
func onExitForeground() {
_inForeground = false
if configuration.flushEventsOnClose == true {
_ = self.flush()
}
}
}