77
88import Amplify
99import Foundation
10+ import Network
1011
1112public extension AWSClickstreamPlugin {
1213 /// called when sdk init.
@@ -22,5 +23,68 @@ public extension AWSClickstreamPlugin {
2223 }
2324
2425 /// Configure AWSClickstreamPlugin programatically using AWSClickstreamConfiguration
25- func configure( using configuration: AWSClickstreamConfiguration ) throws { }
26+ func configure( using configuration: AWSClickstreamConfiguration ) throws {
27+ let contextConfiguration = ClickstreamContextConfiguration ( appId: configuration. appId,
28+ endpoint: configuration. endpoint,
29+ sendEventsInterval: configuration. sendEventsInterval,
30+ isTrackAppExceptionEvents: configuration. isTrackAppExceptionEvents,
31+ isCompressEvents: configuration. isCompressEvents)
32+ let clickstream = try ClickstreamContext ( with: contextConfiguration)
33+
34+ let sessionClient = SessionClient ( configuration: . init( uniqueDeviceId: clickstream. uniqueId,
35+ sessionBackgroundTimeout: TimeInterval ( 10 ) ) ,
36+ userDefaults: clickstream. storage. userDefaults)
37+ clickstream. sessionClient = sessionClient
38+ let sessionProvider : ( ) -> Session = { [ weak sessionClient] in
39+ guard let sessionClient else {
40+ fatalError ( " SessionClient was deallocated " )
41+ }
42+ return sessionClient. currentSession
43+ }
44+ let eventRecorder = try EventRecorder ( clickstream: clickstream)
45+ analyticsClient = try AnalyticsClient ( clickstream: clickstream,
46+ eventRecorder: eventRecorder,
47+ sessionProvider: sessionProvider)
48+ clickstream. analyticsClient = analyticsClient
49+ sessionClient. analyticsClient = analyticsClient
50+ let networkMonitor = NWPathMonitor ( )
51+ clickstream. networkMonitor = networkMonitor
52+
53+ var autoFlushEventsTimer : DispatchSourceTimer ?
54+ if configuration. sendEventsInterval != 0 {
55+ let timeInterval = TimeInterval ( configuration. sendEventsInterval / 1000 )
56+ autoFlushEventsTimer = RepeatingTimer . createRepeatingTimer (
57+ timeInterval: timeInterval,
58+ eventHandler: { [ weak self] in
59+ self ? . log. debug ( " AutoFlushTimer triggered, flushing events " )
60+ self ? . flushEvents ( )
61+ }
62+ )
63+ }
64+
65+ configure ( clickstream: clickstream,
66+ autoFlushEventsTimer: autoFlushEventsTimer,
67+ networkMonitor: networkMonitor)
68+ sessionClient. startSession ( )
69+ log. debug ( " init the sdk success " )
70+ }
71+
72+ // MARK: Internal
73+
74+ /// Internal configure method to set the properties of the plugin
75+ internal func configure( clickstream: ClickstreamContext ,
76+ autoFlushEventsTimer: DispatchSourceTimer ? ,
77+ networkMonitor: NetworkMonitor )
78+ {
79+ self . clickstream = clickstream
80+ isEnabled = true
81+ self . autoFlushEventsTimer = autoFlushEventsTimer
82+ self . autoFlushEventsTimer? . resume ( )
83+ self . networkMonitor = networkMonitor
84+ self . networkMonitor. startMonitoring (
85+ using: DispatchQueue (
86+ label: " com.amazonaws.solution.clickstream.AnalyticsPlugin.NetworkMonitor "
87+ )
88+ )
89+ }
2690}
0 commit comments