@@ -20,22 +20,23 @@ public class Analytics {
20
20
}
21
21
internal var store : Store
22
22
internal var storage : Storage
23
-
23
+
24
24
/// Enabled/disables debug logging to trace your data going through the SDK.
25
25
public static var debugLogsEnabled = false
26
-
26
+
27
27
public var timeline : Timeline
28
-
28
+
29
29
static internal let deadInstance = " DEADINSTANCE "
30
30
static internal weak var firstInstance : Analytics ? = nil
31
+
31
32
@Atomic static internal var activeWriteKeys = [ String] ( )
32
-
33
+
33
34
/**
34
35
This method isn't a traditional singleton implementation. It's provided here
35
36
to ease migration from analytics-ios to analytics-swift. Rather than return a
36
37
singleton, it returns the first instance of Analytics created, OR an instance
37
38
who's writekey is "DEADINSTANCE".
38
-
39
+
39
40
In the case of a dead instance, an assert will be thrown when in DEBUG builds to
40
41
assist developers in knowning that `shared()` is being called too soon.
41
42
*/
@@ -45,16 +46,16 @@ public class Analytics {
45
46
return a
46
47
}
47
48
}
48
-
49
+
49
50
#if DEBUG
50
51
if isUnitTesting == false {
51
52
assert ( true == false , " An instance of Analytice does not exist! " )
52
53
}
53
54
#endif
54
-
55
+
55
56
return Analytics ( configuration: Configuration ( writeKey: deadInstance) )
56
57
}
57
-
58
+
58
59
/// Initialize this instance of Analytics with a given configuration setup.
59
60
/// - Parameters:
60
61
/// - configuration: The configuration to use
@@ -75,40 +76,40 @@ public class Analytics {
75
76
operatingMode: configuration. values. operatingMode
76
77
)
77
78
timeline = Timeline ( )
78
-
79
+
79
80
// provide our default state
80
81
store. provide ( state: System . defaultState ( configuration: configuration, from: storage) )
81
82
store. provide ( state: UserInfo . defaultState ( from: storage, anonIdGenerator: configuration. values. anonymousIdGenerator) )
82
83
83
84
storage. analytics = self
84
-
85
+
85
86
checkSharedInstance ( )
86
-
87
+
87
88
// Get everything running
88
89
platformStartup ( )
89
90
}
90
91
91
92
deinit {
92
93
Self . removeActiveWriteKey ( configuration. values. writeKey)
93
94
}
94
-
95
+
95
96
internal func process< E: RawEvent > ( incomingEvent: E ) {
96
97
guard enabled == true else { return }
97
98
let event = incomingEvent. applyRawEventData ( store: store)
98
-
99
+
99
100
_ = timeline. process ( incomingEvent: event)
100
-
101
+
101
102
let flushPolicies = configuration. values. flushPolicies
102
103
for policy in flushPolicies {
103
104
policy. updateState ( event: event)
104
-
105
+
105
106
if ( policy. shouldFlush ( ) == true ) {
106
107
flush ( )
107
108
policy. reset ( )
108
109
}
109
110
}
110
111
}
111
-
112
+
112
113
/// Process a raw event through the system. Useful when one needs to queue and replay events at a later time.
113
114
/// - Parameters:
114
115
/// - event: An event conforming to RawEvent that will be processed.
@@ -160,20 +161,20 @@ extension Analytics {
160
161
}
161
162
return " "
162
163
}
163
-
164
+
164
165
/// Returns the userId that was specified in the last identify call.
165
166
public var userId : String ? {
166
167
if let userInfo: UserInfo = store. currentState ( ) {
167
168
return userInfo. userId
168
169
}
169
170
return nil
170
171
}
171
-
172
+
172
173
/// Returns the current operating mode this instance was given.
173
174
public var operatingMode : OperatingMode {
174
175
return configuration. values. operatingMode
175
176
}
176
-
177
+
177
178
/// Adjusts the flush interval post configuration.
178
179
public var flushInterval : TimeInterval {
179
180
get {
@@ -186,7 +187,7 @@ extension Analytics {
186
187
}
187
188
}
188
189
}
189
-
190
+
190
191
/// Adjusts the flush-at count post configuration.
191
192
public var flushAt : Int {
192
193
get {
@@ -199,30 +200,30 @@ extension Analytics {
199
200
}
200
201
}
201
202
}
202
-
203
+
203
204
/// Returns a list of currently active flush policies.
204
205
public var flushPolicies : [ FlushPolicy ] {
205
206
get {
206
207
configuration. values. flushPolicies
207
208
}
208
209
}
209
-
210
+
210
211
/// Returns the traits that were specified in the last identify call.
211
212
public func traits< T: Codable > ( ) -> T ? {
212
213
if let userInfo: UserInfo = store. currentState ( ) {
213
214
return userInfo. traits? . codableValue ( )
214
215
}
215
216
return nil
216
217
}
217
-
218
+
218
219
/// Returns the traits that were specified in the last identify call, as a dictionary.
219
220
public func traits( ) -> [ String : Any ] ? {
220
221
if let userInfo: UserInfo = store. currentState ( ) {
221
222
return userInfo. traits? . dictionaryValue
222
223
}
223
224
return nil
224
225
}
225
-
226
+
226
227
/// Tells this instance of Analytics to flush any queued events up to Segment.com. This command will also
227
228
/// be sent to each plugin present in the system. A completion handler can be optionally given and will be
228
229
/// called when flush has completed.
@@ -247,7 +248,7 @@ extension Analytics {
247
248
completion ? ( )
248
249
}
249
250
}
250
-
251
+
251
252
/// Resets this instance of Analytics to a clean slate. Traits, UserID's, anonymousId, etc are all cleared or reset. This
252
253
/// command will also be sent to each plugin present in the system.
253
254
public func reset( ) {
@@ -258,13 +259,13 @@ extension Analytics {
258
259
}
259
260
}
260
261
}
261
-
262
+
262
263
/// Retrieve the version of this library in use.
263
264
/// - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
264
265
public func version( ) -> String {
265
266
return Analytics . version ( )
266
267
}
267
-
268
+
268
269
/// Retrieve the version of this library in use.
269
270
/// - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
270
271
public static func version( ) -> String {
@@ -282,7 +283,7 @@ extension Analytics {
282
283
}
283
284
return settings
284
285
}
285
-
286
+
286
287
/// Manually enable a destination plugin. This is useful when a given DestinationPlugin doesn't have any Segment tie-ins at all.
287
288
/// This will allow the destination to be processed in the same way within this library.
288
289
/// - Parameters:
@@ -306,25 +307,25 @@ extension Analytics {
306
307
307
308
return storage. dataStore. hasData
308
309
}
309
-
310
+
310
311
/// Provides a list of finished, but unsent events.
311
312
public var pendingUploads : [ URL ] ? {
312
313
return storage. read ( Storage . Constants. events) ? . dataFiles
313
314
}
314
-
315
+
315
316
/// Purge all pending event upload files.
316
317
public func purgeStorage( ) {
317
318
storage. dataStore. reset ( )
318
319
}
319
-
320
+
320
321
/// Purge a single event upload file.
321
322
public func purgeStorage( fileURL: URL ) {
322
323
guard let dataFiles = storage. read ( Storage . Constants. events) ? . dataFiles else { return }
323
324
if dataFiles. contains ( fileURL) {
324
325
try ? FileManager . default. removeItem ( at: fileURL)
325
326
}
326
327
}
327
-
328
+
328
329
/// Wait until the Analytics object has completed startup.
329
330
/// This method is primarily useful for command line utilities where
330
331
/// it's desirable to wait until the system is up and running
@@ -344,7 +345,7 @@ extension Analytics {
344
345
Call openURL as needed or when instructed to by either UIApplicationDelegate or UISceneDelegate.
345
346
This is necessary to track URL referrers across events. This method will also iterate
346
347
any plugins that are watching for openURL events.
347
-
348
+
348
349
Example:
349
350
```
350
351
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
@@ -359,12 +360,12 @@ extension Analytics {
359
360
guard let dict = jsonProperties. dictionaryValue else { return }
360
361
openURL ( url, options: dict)
361
362
}
362
-
363
+
363
364
/**
364
365
Call openURL as needed or when instructed to by either UIApplicationDelegate or UISceneDelegate.
365
366
This is necessary to track URL referrers across events. This method will also iterate
366
367
any plugins that are watching for openURL events.
367
-
368
+
368
369
Example:
369
370
```
370
371
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
@@ -375,14 +376,14 @@ extension Analytics {
375
376
*/
376
377
public func openURL( _ url: URL , options: [ String : Any ] = [ : ] ) {
377
378
store. dispatch ( action: UserInfo . SetReferrerAction ( url: url) )
378
-
379
+
379
380
// let any conforming plugins know
380
381
apply { plugin in
381
382
if let p = plugin as? OpeningURLs {
382
383
p. openURL ( url, options: options)
383
384
}
384
385
}
385
-
386
+
386
387
var jsonProperties : JSON ? = nil
387
388
if let json = try ? JSON ( options) {
388
389
jsonProperties = json
@@ -411,7 +412,7 @@ extension Analytics {
411
412
Self . firstInstance = self
412
413
}
413
414
}
414
-
415
+
415
416
/// Determines if an instance is dead.
416
417
internal var isDead : Bool {
417
418
return configuration. values. writeKey == Self . deadInstance
@@ -455,4 +456,3 @@ extension OperatingMode {
455
456
}
456
457
}
457
458
}
458
-
0 commit comments