@@ -20,21 +20,21 @@ 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
32
/**
33
33
This method isn't a traditional singleton implementation. It's provided here
34
34
to ease migration from analytics-ios to analytics-swift. Rather than return a
35
35
singleton, it returns the first instance of Analytics created, OR an instance
36
36
who's writekey is "DEADINSTANCE".
37
-
37
+
38
38
In the case of a dead instance, an assert will be thrown when in DEBUG builds to
39
39
assist developers in knowning that `shared()` is being called too soon.
40
40
*/
@@ -44,53 +44,53 @@ public class Analytics {
44
44
return a
45
45
}
46
46
}
47
-
47
+
48
48
#if DEBUG
49
49
if isUnitTesting == false {
50
50
assert ( true == false , " An instance of Analytice does not exist! " )
51
51
}
52
52
#endif
53
-
53
+
54
54
return Analytics ( configuration: Configuration ( writeKey: deadInstance) )
55
55
}
56
-
56
+
57
57
/// Initialize this instance of Analytics with a given configuration setup.
58
58
/// - Parameters:
59
59
/// - configuration: The configuration to use
60
60
public init ( configuration: Configuration ) {
61
61
store = Store ( )
62
62
storage = Storage ( store: self . store, writeKey: configuration. values. writeKey)
63
63
timeline = Timeline ( )
64
-
64
+
65
65
// provide our default state
66
66
store. provide ( state: System . defaultState ( configuration: configuration, from: storage) )
67
67
store. provide ( state: UserInfo . defaultState ( from: storage) )
68
-
68
+
69
69
storage. analytics = self
70
-
70
+
71
71
checkSharedInstance ( )
72
-
72
+
73
73
// Get everything running
74
74
platformStartup ( )
75
75
}
76
-
76
+
77
77
internal func process< E: RawEvent > ( incomingEvent: E ) {
78
78
guard enabled == true else { return }
79
79
let event = incomingEvent. applyRawEventData ( store: store)
80
-
80
+
81
81
_ = timeline. process ( incomingEvent: event)
82
-
82
+
83
83
let flushPolicies = configuration. values. flushPolicies
84
84
for policy in flushPolicies {
85
85
policy. updateState ( event: event)
86
-
86
+
87
87
if ( policy. shouldFlush ( ) == true ) {
88
88
flush ( )
89
89
policy. reset ( )
90
90
}
91
91
}
92
92
}
93
-
93
+
94
94
/// Process a raw event through the system. Useful when one needs to queue and replay events at a later time.
95
95
/// - Parameters:
96
96
/// - event: An event conforming to RawEvent that will be processed.
@@ -129,28 +129,28 @@ extension Analytics {
129
129
store. dispatch ( action: System . ToggleEnabledAction ( enabled: value) )
130
130
}
131
131
}
132
-
132
+
133
133
/// Returns the anonymousId currently in use.
134
134
public var anonymousId : String {
135
135
if let userInfo: UserInfo = store. currentState ( ) {
136
136
return userInfo. anonymousId
137
137
}
138
138
return " "
139
139
}
140
-
140
+
141
141
/// Returns the userId that was specified in the last identify call.
142
142
public var userId : String ? {
143
143
if let userInfo: UserInfo = store. currentState ( ) {
144
144
return userInfo. userId
145
145
}
146
146
return nil
147
147
}
148
-
148
+
149
149
/// Returns the current operating mode this instance was given.
150
150
public var operatingMode : OperatingMode {
151
151
return configuration. values. operatingMode
152
152
}
153
-
153
+
154
154
/// Adjusts the flush interval post configuration.
155
155
public var flushInterval : TimeInterval {
156
156
get {
@@ -163,7 +163,7 @@ extension Analytics {
163
163
}
164
164
}
165
165
}
166
-
166
+
167
167
/// Adjusts the flush-at count post configuration.
168
168
public var flushAt : Int {
169
169
get {
@@ -176,41 +176,41 @@ extension Analytics {
176
176
}
177
177
}
178
178
}
179
-
179
+
180
180
/// Returns a list of currently active flush policies.
181
181
public var flushPolicies : [ FlushPolicy ] {
182
182
get {
183
183
configuration. values. flushPolicies
184
184
}
185
185
}
186
-
186
+
187
187
/// Returns the traits that were specified in the last identify call.
188
188
public func traits< T: Codable > ( ) -> T ? {
189
189
if let userInfo: UserInfo = store. currentState ( ) {
190
190
return userInfo. traits? . codableValue ( )
191
191
}
192
192
return nil
193
193
}
194
-
194
+
195
195
/// Returns the traits that were specified in the last identify call, as a dictionary.
196
196
public func traits( ) -> [ String : Any ] ? {
197
197
if let userInfo: UserInfo = store. currentState ( ) {
198
198
return userInfo. traits? . dictionaryValue
199
199
}
200
200
return nil
201
201
}
202
-
202
+
203
203
/// Tells this instance of Analytics to flush any queued events up to Segment.com. This command will also
204
204
/// be sent to each plugin present in the system. A completion handler can be optionally given and will be
205
205
/// called when flush has completed.
206
206
public func flush( completion: ( ( ) -> Void ) ? = nil ) {
207
207
// only flush if we're enabled.
208
208
guard enabled == true else { return }
209
-
209
+
210
210
let flushGroup = DispatchGroup ( )
211
211
// gotta call enter at least once before we ask to be notified.
212
212
flushGroup. enter ( )
213
-
213
+
214
214
apply { plugin in
215
215
// we want to enter as soon as possible. waiting to do it from
216
216
// another queue just takes too long.
@@ -228,9 +228,9 @@ extension Analytics {
228
228
}
229
229
}
230
230
}
231
-
231
+
232
232
flushGroup. leave ( ) // matches our initial enter().
233
-
233
+
234
234
// if we ARE in sync mode, we need to wait on the group.
235
235
// This effectively ends up being a `sync` operation.
236
236
if operatingMode == . synchronous {
@@ -257,7 +257,7 @@ extension Analytics {
257
257
}
258
258
}
259
259
}
260
-
260
+
261
261
/// Resets this instance of Analytics to a clean slate. Traits, UserID's, anonymousId, etc are all cleared or reset. This
262
262
/// command will also be sent to each plugin present in the system.
263
263
public func reset( ) {
@@ -268,13 +268,13 @@ extension Analytics {
268
268
}
269
269
}
270
270
}
271
-
271
+
272
272
/// Retrieve the version of this library in use.
273
273
/// - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
274
274
public func version( ) -> String {
275
275
return Analytics . version ( )
276
276
}
277
-
277
+
278
278
/// Retrieve the version of this library in use.
279
279
/// - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
280
280
public static func version( ) -> String {
@@ -292,7 +292,7 @@ extension Analytics {
292
292
}
293
293
return settings
294
294
}
295
-
295
+
296
296
/// Manually enable a destination plugin. This is useful when a given DestinationPlugin doesn't have any Segment tie-ins at all.
297
297
/// This will allow the destination to be processed in the same way within this library.
298
298
/// - Parameters:
@@ -319,15 +319,15 @@ extension Analytics {
319
319
return true
320
320
}
321
321
}
322
-
322
+
323
323
return false
324
324
}
325
-
325
+
326
326
/// Provides a list of finished, but unsent events.
327
327
public var pendingUploads : [ URL ] ? {
328
328
return storage. read ( Storage . Constants. events)
329
329
}
330
-
330
+
331
331
/// Purge all pending event upload files.
332
332
public func purgeStorage( ) {
333
333
if let files = pendingUploads {
@@ -336,12 +336,12 @@ extension Analytics {
336
336
}
337
337
}
338
338
}
339
-
339
+
340
340
/// Purge a single event upload file.
341
341
public func purgeStorage( fileURL: URL ) {
342
342
try ? FileManager . default. removeItem ( at: fileURL)
343
343
}
344
-
344
+
345
345
/// Wait until the Analytics object has completed startup.
346
346
/// This method is primarily useful for command line utilities where
347
347
/// it's desirable to wait until the system is up and running
@@ -361,7 +361,7 @@ extension Analytics {
361
361
Call openURL as needed or when instructed to by either UIApplicationDelegate or UISceneDelegate.
362
362
This is necessary to track URL referrers across events. This method will also iterate
363
363
any plugins that are watching for openURL events.
364
-
364
+
365
365
Example:
366
366
```
367
367
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
@@ -376,12 +376,12 @@ extension Analytics {
376
376
guard let dict = jsonProperties. dictionaryValue else { return }
377
377
openURL ( url, options: dict)
378
378
}
379
-
379
+
380
380
/**
381
381
Call openURL as needed or when instructed to by either UIApplicationDelegate or UISceneDelegate.
382
382
This is necessary to track URL referrers across events. This method will also iterate
383
383
any plugins that are watching for openURL events.
384
-
384
+
385
385
Example:
386
386
```
387
387
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
@@ -392,14 +392,14 @@ extension Analytics {
392
392
*/
393
393
public func openURL( _ url: URL , options: [ String : Any ] = [ : ] ) {
394
394
store. dispatch ( action: UserInfo . SetReferrerAction ( url: url) )
395
-
395
+
396
396
// let any conforming plugins know
397
397
apply { plugin in
398
398
if let p = plugin as? OpeningURLs {
399
399
p. openURL ( url, options: options)
400
400
}
401
401
}
402
-
402
+
403
403
var jsonProperties : JSON ? = nil
404
404
if let json = try ? JSON ( options) {
405
405
jsonProperties = json
@@ -428,7 +428,7 @@ extension Analytics {
428
428
Self . firstInstance = self
429
429
}
430
430
}
431
-
431
+
432
432
/// Determines if an instance is dead.
433
433
internal var isDead : Bool {
434
434
return configuration. values. writeKey == Self . deadInstance
@@ -453,4 +453,3 @@ extension OperatingMode {
453
453
}
454
454
}
455
455
}
456
-
0 commit comments