@@ -56,30 +56,41 @@ internal struct MiniTrackEvent: RawEvent {
5656}
5757
5858internal class MiniAnalytics {
59- let analytics : Analytics
6059 let session : URLSession
60+ let apiHost : String
6161 let storage : TransientDB
6262
6363 @Atomic var flushing : Bool = false
64+
65+ struct LocalUserInfo {
66+ var userId : String ? = nil
67+ var anonymousId : String ? = nil
68+ }
69+ @Atomic var userInfo = LocalUserInfo ( )
70+
6471 // used for testing only.
6572 internal static var observer : ( ( _ in: any RawSignal , _ out: MiniTrackEvent ) -> Void ) ? = nil
6673
67- init ( analytics : Analytics ) {
68- self . analytics = analytics
74+ init ( writeKey : String , apiHost : String ) {
75+ self . apiHost = apiHost
6976 self . session = Self . configuredSession ( )
7077
7178 let fileStore = DirectoryStore (
7279 configuration:
7380 DirectoryStore . Configuration (
74- writeKey: analytics . writeKey,
75- storageLocation: Self . signalStorageDirectory ( writeKey: analytics . writeKey) ,
81+ writeKey: writeKey,
82+ storageLocation: Self . signalStorageDirectory ( writeKey: writeKey) ,
7683 baseFilename: " segment-signals " ,
7784 maxFileSize: 475000 ,
7885 indexKey: " signalFileIndex " )
7986 )
8087 self . storage = TransientDB ( store: fileStore, asyncAppend: true )
8188 }
8289
90+ func stateSubscriber( userInfo: UserInfo ) {
91+ _userInfo. set ( LocalUserInfo ( userId: userInfo. userId, anonymousId: userInfo. anonymousId) )
92+ }
93+
8394 func track( signal: any RawSignal , obfuscate: Bool ) {
8495 let input = signal
8596 var signal = signal
@@ -90,9 +101,10 @@ internal class MiniAnalytics {
90101
91102 guard let props = try ? JSON ( with: signal) else { return }
92103
93- let anonId = analytics. anonymousId
104+ let userInfo = self . userInfo
105+ let anonId = userInfo. anonymousId
106+ let userId = userInfo. userId
94107 let messageId = UUID ( ) . uuidString
95- let userId = analytics. userId
96108 let timestamp = Date ( ) . iso8601 ( )
97109
98110 let track = MiniTrackEvent (
@@ -137,7 +149,7 @@ internal class MiniAnalytics {
137149 break
138150 }
139151
140- analytics . log ( message: " Processed: \( url. lastPathComponent) " )
152+ Analytics . segmentLog ( message: " Processed: \( url. lastPathComponent) " , kind : . debug )
141153 group. leave ( )
142154 }
143155 }
@@ -150,7 +162,7 @@ internal class MiniAnalytics {
150162
151163extension MiniAnalytics {
152164 //private static let defaultAPIHost = "signals.segment.io/v1"
153- private static let defaultAPIHost = " signals.segment.build/v1 "
165+ // private static let defaultAPIHost = "signals.segment.build/v1"
154166
155167 func segmentURL( for host: String , path: String ) -> URL ? {
156168 let s = " https:// \( host) \( path) "
@@ -160,8 +172,8 @@ extension MiniAnalytics {
160172
161173 @discardableResult
162174 func startBatchUpload( batch: URL , completion: @escaping ( _ result: Result < Bool , Error > ) -> Void ) -> URLSessionDataTask ? {
163- guard let uploadURL = segmentURL ( for: Self . defaultAPIHost , path: " /b " ) else {
164- analytics . reportInternalError ( HTTPClientErrors . failedToOpenBatch, fatal: false )
175+ guard let uploadURL = segmentURL ( for: self . apiHost , path: " /b " ) else {
176+ Analytics . reportInternalError ( HTTPClientErrors . failedToOpenBatch, fatal: false )
165177 completion ( . failure( HTTPClientErrors . failedToOpenBatch) )
166178 return nil
167179 }
@@ -181,7 +193,7 @@ extension MiniAnalytics {
181193 var request = URLRequest ( url: url, cachePolicy: . reloadIgnoringLocalCacheData, timeoutInterval: 60 )
182194 request. httpMethod = method
183195 request. addValue ( " application/json; charset=utf-8 " , forHTTPHeaderField: " Content-Type " )
184- request. addValue ( " analytics-ios/ \( analytics . version ( ) ) " , forHTTPHeaderField: " User-Agent " )
196+ request. addValue ( " analytics-ios/ \( Analytics . version ( ) ) " , forHTTPHeaderField: " User-Agent " )
185197 request. addValue ( " gzip " , forHTTPHeaderField: " Accept-Encoding " )
186198
187199 return request
@@ -196,22 +208,22 @@ extension MiniAnalytics {
196208
197209 private func handleResponse( data: Data ? , response: URLResponse ? , error: Error ? , completion: @escaping ( _ result: Result < Bool , Error > ) -> Void ) {
198210 if let error = error {
199- analytics . log ( message: " Error uploading request \( error. localizedDescription) . " )
200- analytics . reportInternalError ( AnalyticsError . networkUnknown ( error) , fatal: false )
211+ Analytics . segmentLog ( message: " Error uploading request \( error. localizedDescription) . " , kind : . error )
212+ Analytics . reportInternalError ( AnalyticsError . networkUnknown ( error) , fatal: false )
201213 completion ( . failure( HTTPClientErrors . unknown ( error: error) ) )
202214 } else if let httpResponse = response as? HTTPURLResponse {
203215 switch ( httpResponse. statusCode) {
204216 case 1 ..< 300 :
205217 completion ( . success( true ) )
206218 return
207219 case 300 ..< 400 :
208- analytics . reportInternalError ( AnalyticsError . networkUnexpectedHTTPCode ( httpResponse. statusCode) , fatal: false )
220+ Analytics . reportInternalError ( AnalyticsError . networkUnexpectedHTTPCode ( httpResponse. statusCode) , fatal: false )
209221 completion ( . failure( HTTPClientErrors . statusCode ( code: httpResponse. statusCode) ) )
210222 case 429 :
211- analytics . reportInternalError ( AnalyticsError . networkServerLimited ( httpResponse. statusCode) , fatal: false )
223+ Analytics . reportInternalError ( AnalyticsError . networkServerLimited ( httpResponse. statusCode) , fatal: false )
212224 completion ( . failure( HTTPClientErrors . statusCode ( code: httpResponse. statusCode) ) )
213225 default :
214- analytics . reportInternalError ( AnalyticsError . networkServerRejected ( httpResponse. statusCode) , fatal: false )
226+ Analytics . reportInternalError ( AnalyticsError . networkServerRejected ( httpResponse. statusCode) , fatal: false )
215227 completion ( . failure( HTTPClientErrors . statusCode ( code: httpResponse. statusCode) ) )
216228 }
217229 }
0 commit comments