@app-swiss-knife/react-native-sdk 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/CHANGELOG.md +385 -2
  2. package/LICENSE +21 -0
  3. package/README.md +229 -42
  4. package/SwissKnifePurchases.podspec +1 -1
  5. package/android/build.gradle +24 -2
  6. package/android/consumer-rules.pro +12 -0
  7. package/android/src/main/AndroidManifest.xml +15 -1
  8. package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +5 -4
  9. package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +7 -2
  10. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +168 -6
  11. package/android/src/play/java/com/swissknife/purchases/StoreIntentHandler.kt +7 -0
  12. package/android/src/rustore/java/com/swissknife/purchases/StoreIntentHandler.kt +9 -0
  13. package/android/vendor/java/com/appswissknife/SwissKnife.kt +107 -38
  14. package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +180 -115
  15. package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +16 -18
  16. package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +13 -13
  17. package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +56 -25
  18. package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
  19. package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +365 -206
  20. package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +195 -26
  21. package/android/vendor/java/com/appswissknife/identity/Identity.kt +31 -6
  22. package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +5 -0
  23. package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +56 -31
  24. package/android/vendor/java/com/appswissknife/net/IngestClient.kt +673 -87
  25. package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +214 -30
  26. package/android/vendor/java/com/appswissknife/notifications/NotificationRenderer.kt +195 -0
  27. package/android/vendor/java/com/appswissknife/notifications/PermissionRequestActivity.kt +60 -0
  28. package/android/vendor/java/com/appswissknife/notifications/PermissionStatus.kt +41 -0
  29. package/android/vendor/java/com/appswissknife/notifications/PushEventQueue.kt +143 -0
  30. package/android/vendor/java/com/appswissknife/notifications/PushPayload.kt +72 -0
  31. package/android/vendor/java/com/appswissknife/notifications/PushRegistration.kt +89 -0
  32. package/android/vendor/java/com/appswissknife/notifications/SwissKnifeMessagingService.kt +57 -0
  33. package/android/vendor/java/com/appswissknife/notifications/SwissKnifeNotifications.kt +438 -0
  34. package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +266 -64
  35. package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +1 -0
  36. package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +545 -88
  37. package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +55 -24
  38. package/android/vendor/java/com/appswissknife/purchases/ProductKindHint.kt +21 -0
  39. package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +14 -0
  40. package/android/vendor-rustore/AndroidManifest.xml +59 -0
  41. package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreGateway.kt +428 -0
  42. package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreInitProvider.kt +43 -0
  43. package/android/vendor-rustore/java/com/appswissknife/rustore/RuStorePriceEnrichmentProvider.kt +85 -0
  44. package/app.plugin.js +287 -0
  45. package/ios/SwissKnifePurchases.m +20 -0
  46. package/ios/SwissKnifePurchases.swift +97 -3
  47. package/ios/SwissKnifePushBootstrap.m +34 -0
  48. package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +138 -89
  49. package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
  50. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +345 -44
  51. package/ios/vendor/SwissKnifeiOS/Net/BackendBackoffGate.swift +43 -2
  52. package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +495 -16
  53. package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +231 -23
  54. package/ios/vendor/SwissKnifeiOS/Notifications/PushPayload.swift +87 -0
  55. package/ios/vendor/SwissKnifeiOS/Notifications/PushRegistrar.swift +322 -0
  56. package/ios/vendor/SwissKnifeiOS/Notifications/SwissKnife+Notifications.swift +403 -0
  57. package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +1 -0
  58. package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +11 -7
  59. package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +38 -7
  60. package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +34 -6
  61. package/lib/commonjs/attribution.js +4 -3
  62. package/lib/commonjs/attribution.js.map +1 -1
  63. package/lib/commonjs/identity.js +12 -11
  64. package/lib/commonjs/identity.js.map +1 -1
  65. package/lib/commonjs/index.js +16 -5
  66. package/lib/commonjs/index.js.map +1 -1
  67. package/lib/commonjs/internal/config.js +105 -8
  68. package/lib/commonjs/internal/config.js.map +1 -1
  69. package/lib/commonjs/internal/telemetry-context.js +70 -0
  70. package/lib/commonjs/internal/telemetry-context.js.map +1 -0
  71. package/lib/commonjs/modules/analytics.js +18 -25
  72. package/lib/commonjs/modules/analytics.js.map +1 -1
  73. package/lib/commonjs/modules/crash-reporting.js +27 -6
  74. package/lib/commonjs/modules/crash-reporting.js.map +1 -1
  75. package/lib/commonjs/modules/log.js +1 -1
  76. package/lib/commonjs/modules/notifications.js +500 -0
  77. package/lib/commonjs/modules/notifications.js.map +1 -0
  78. package/lib/commonjs/modules/performance.js +57 -8
  79. package/lib/commonjs/modules/performance.js.map +1 -1
  80. package/lib/commonjs/native/module.js +6 -0
  81. package/lib/commonjs/native/module.js.map +1 -1
  82. package/lib/commonjs/native/spec.js +12 -5
  83. package/lib/commonjs/native/spec.js.map +1 -1
  84. package/lib/commonjs/purchases.js +4 -4
  85. package/lib/commonjs/types.js +12 -15
  86. package/lib/commonjs/types.js.map +1 -1
  87. package/lib/module/attribution.js +4 -3
  88. package/lib/module/attribution.js.map +1 -1
  89. package/lib/module/identity.js +12 -11
  90. package/lib/module/identity.js.map +1 -1
  91. package/lib/module/index.js +8 -4
  92. package/lib/module/index.js.map +1 -1
  93. package/lib/module/internal/config.js +103 -8
  94. package/lib/module/internal/config.js.map +1 -1
  95. package/lib/module/internal/telemetry-context.js +63 -0
  96. package/lib/module/internal/telemetry-context.js.map +1 -0
  97. package/lib/module/modules/analytics.js +18 -25
  98. package/lib/module/modules/analytics.js.map +1 -1
  99. package/lib/module/modules/crash-reporting.js +27 -6
  100. package/lib/module/modules/crash-reporting.js.map +1 -1
  101. package/lib/module/modules/log.js +1 -1
  102. package/lib/module/modules/notifications.js +491 -0
  103. package/lib/module/modules/notifications.js.map +1 -0
  104. package/lib/module/modules/performance.js +57 -8
  105. package/lib/module/modules/performance.js.map +1 -1
  106. package/lib/module/native/module.js +6 -1
  107. package/lib/module/native/module.js.map +1 -1
  108. package/lib/module/native/spec.js +11 -4
  109. package/lib/module/native/spec.js.map +1 -1
  110. package/lib/module/purchases.js +4 -4
  111. package/lib/module/types.js +12 -15
  112. package/lib/module/types.js.map +1 -1
  113. package/lib/typescript/commonjs/attribution.d.ts +3 -2
  114. package/lib/typescript/commonjs/attribution.d.ts.map +1 -1
  115. package/lib/typescript/commonjs/identity.d.ts.map +1 -1
  116. package/lib/typescript/commonjs/index.d.ts +2 -0
  117. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  118. package/lib/typescript/commonjs/internal/config.d.ts +16 -2
  119. package/lib/typescript/commonjs/internal/config.d.ts.map +1 -1
  120. package/lib/typescript/commonjs/internal/telemetry-context.d.ts +22 -0
  121. package/lib/typescript/commonjs/internal/telemetry-context.d.ts.map +1 -0
  122. package/lib/typescript/commonjs/modules/analytics.d.ts +8 -11
  123. package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -1
  124. package/lib/typescript/commonjs/modules/crash-reporting.d.ts +18 -5
  125. package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
  126. package/lib/typescript/commonjs/modules/notifications.d.ts +110 -0
  127. package/lib/typescript/commonjs/modules/notifications.d.ts.map +1 -0
  128. package/lib/typescript/commonjs/modules/performance.d.ts +13 -0
  129. package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -1
  130. package/lib/typescript/commonjs/native/module.d.ts +2 -0
  131. package/lib/typescript/commonjs/native/module.d.ts.map +1 -1
  132. package/lib/typescript/commonjs/native/spec.d.ts +33 -4
  133. package/lib/typescript/commonjs/native/spec.d.ts.map +1 -1
  134. package/lib/typescript/commonjs/purchases.d.ts +4 -4
  135. package/lib/typescript/commonjs/types.d.ts +4 -5
  136. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  137. package/lib/typescript/module/attribution.d.ts +3 -2
  138. package/lib/typescript/module/attribution.d.ts.map +1 -1
  139. package/lib/typescript/module/identity.d.ts.map +1 -1
  140. package/lib/typescript/module/index.d.ts +2 -0
  141. package/lib/typescript/module/index.d.ts.map +1 -1
  142. package/lib/typescript/module/internal/config.d.ts +16 -2
  143. package/lib/typescript/module/internal/config.d.ts.map +1 -1
  144. package/lib/typescript/module/internal/telemetry-context.d.ts +22 -0
  145. package/lib/typescript/module/internal/telemetry-context.d.ts.map +1 -0
  146. package/lib/typescript/module/modules/analytics.d.ts +8 -11
  147. package/lib/typescript/module/modules/analytics.d.ts.map +1 -1
  148. package/lib/typescript/module/modules/crash-reporting.d.ts +18 -5
  149. package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
  150. package/lib/typescript/module/modules/notifications.d.ts +110 -0
  151. package/lib/typescript/module/modules/notifications.d.ts.map +1 -0
  152. package/lib/typescript/module/modules/performance.d.ts +13 -0
  153. package/lib/typescript/module/modules/performance.d.ts.map +1 -1
  154. package/lib/typescript/module/native/module.d.ts +2 -0
  155. package/lib/typescript/module/native/module.d.ts.map +1 -1
  156. package/lib/typescript/module/native/spec.d.ts +33 -4
  157. package/lib/typescript/module/native/spec.d.ts.map +1 -1
  158. package/lib/typescript/module/purchases.d.ts +4 -4
  159. package/lib/typescript/module/types.d.ts +4 -5
  160. package/lib/typescript/module/types.d.ts.map +1 -1
  161. package/package.json +12 -4
  162. package/src/attribution.ts +4 -3
  163. package/src/identity.ts +12 -11
  164. package/src/index.ts +16 -4
  165. package/src/internal/config.ts +113 -8
  166. package/src/internal/telemetry-context.ts +76 -0
  167. package/src/modules/analytics.ts +24 -33
  168. package/src/modules/crash-reporting.ts +50 -9
  169. package/src/modules/log.ts +1 -1
  170. package/src/modules/notifications.ts +557 -0
  171. package/src/modules/performance.ts +94 -14
  172. package/src/native/module.ts +6 -0
  173. package/src/native/spec.ts +45 -10
  174. package/src/purchases.ts +4 -4
  175. package/src/types.ts +12 -15
  176. package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +0 -326
  177. package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +0 -296
@@ -151,10 +151,20 @@ public final class SKRevenue {
151
151
 
152
152
  // MARK: - Main SDK
153
153
 
154
+ /// THREAD SAFETY: hosts call this singleton from any thread, so every read
155
+ /// and write of its state (configuration, identity, super properties, timed
156
+ /// events, user properties, groups, the ingest seam) happens on `queue`, a
157
+ /// serial queue. `track` and the mutators hop onto it asynchronously, so the
158
+ /// caller never waits on the enqueue's disk write; the getters read through
159
+ /// `queue.sync`, which runs after every earlier call from the same thread,
160
+ /// so a getter called right after a burst of `track` calls waits for their
161
+ /// (fsync'd) enqueues. These used to be plain dictionaries mutated on the caller's thread, and
162
+ /// concurrent `track` / `registerSuperProperties` calls could crash the app.
154
163
  public final class SKAnalytics {
155
164
 
156
165
  public static let shared = SKAnalytics()
157
166
 
167
+ // All state below is guarded by `queue`.
158
168
  private var configuration: SKAnalyticsConfiguration?
159
169
  private var userId: String?
160
170
  /// Persistent per-device identifier. Resolved lazily on first access and
@@ -189,9 +199,12 @@ public final class SKAnalytics {
189
199
  // MARK: Initialize
190
200
 
191
201
  public static func initialize(configuration: SKAnalyticsConfiguration) {
192
- shared.configuration = configuration
193
- shared.sessionId = Int64(Date().timeIntervalSince1970 * 1000)
194
- shared.startFlushTimer()
202
+ let sessionId = Int64(Date().timeIntervalSince1970 * 1000)
203
+ shared.queue.sync {
204
+ shared.configuration = configuration
205
+ shared.sessionId = sessionId
206
+ }
207
+ shared.startFlushTimer(interval: configuration.flushIntervalSeconds)
195
208
 
196
209
  if configuration.autocaptureOptions.contains(.appLifecycles) {
197
210
  shared.trackAppLifecycle()
@@ -203,37 +216,45 @@ public final class SKAnalytics {
203
216
 
204
217
  // MARK: Track events (Amplitude-compatible)
205
218
 
219
+ /// Asynchronous: the event is built and enqueued on `queue`, in call
220
+ /// order with every other SKAnalytics call, and the caller never waits
221
+ /// on the enqueue's disk write.
206
222
  public func track(eventType: String, eventProperties: [String: Any]? = nil) {
207
- guard configuration?.optOut != true else { return }
208
-
209
- // Merge super properties under event properties (event props override)
210
- var mergedProps = superProperties
211
- if let ep = eventProperties {
212
- for (k, v) in ep { mergedProps[k] = v }
213
- }
223
+ // The duration of a timed event ends when `track` is CALLED, not
224
+ // when the queue gets to it.
225
+ let calledAt = Date().timeIntervalSince1970
226
+ queue.async { [self] in
227
+ guard configuration?.optOut != true else { return }
228
+
229
+ // Merge super properties under event properties (event props override)
230
+ var mergedProps = superProperties
231
+ if let ep = eventProperties {
232
+ for (k, v) in ep { mergedProps[k] = v }
233
+ }
214
234
 
215
- // If this event was timed, compute duration
216
- if let startTime = timedEvents.removeValue(forKey: eventType) {
217
- mergedProps["$duration"] = Date().timeIntervalSince1970 - startTime
218
- }
235
+ // If this event was timed, compute duration
236
+ if let startTime = timedEvents.removeValue(forKey: eventType) {
237
+ mergedProps["$duration"] = calledAt - startTime
238
+ }
219
239
 
220
- // Round-A migration: the event payload is now pure domain data —
221
- // event_id, user_id, device_id, session_id, timestamp all come from
222
- // IngestClient's ContextSnapshot stamping at flush time. We just
223
- // pass `{ event_type, properties }` and let the central pipeline
224
- // handle context.
225
- var payload: [String: Any] = [
226
- "event_type": eventType,
227
- "properties": mergedProps,
228
- ]
229
- // User-scoped props that are still domain-shape, not context.
230
- if !userProperties.isEmpty {
231
- payload["user_properties"] = userProperties
232
- }
233
- if !groups.isEmpty {
234
- payload["groups"] = groups
240
+ // Round-A migration: the event payload is now pure domain data —
241
+ // event_id, user_id, device_id, session_id, timestamp all come from
242
+ // IngestClient's ContextSnapshot stamping at flush time. We just
243
+ // pass `{ event_type, properties }` and let the central pipeline
244
+ // handle context.
245
+ var payload: [String: Any] = [
246
+ "event_type": eventType,
247
+ "properties": mergedProps,
248
+ ]
249
+ // User-scoped props that are still domain-shape, not context.
250
+ if !userProperties.isEmpty {
251
+ payload["user_properties"] = userProperties
252
+ }
253
+ if !groups.isEmpty {
254
+ payload["groups"] = groups
255
+ }
256
+ ingestEnqueueLocked(payload: payload)
235
257
  }
236
- ingestEnqueue(payload: payload)
237
258
  }
238
259
 
239
260
  /// Round-A IngestClient path. Replaces `persistAndMaybeFlush` for the
@@ -243,8 +264,8 @@ public final class SKAnalytics {
243
264
  ///
244
265
  /// Opt-out is still checked here even though `track(...)` also gates
245
266
  /// on it — defense in depth against future call sites bypassing the
246
- /// public entry.
247
- private func ingestEnqueue(payload: [String: Any]) {
267
+ /// public entry. Must run on `queue`.
268
+ private func ingestEnqueueLocked(payload: [String: Any]) {
248
269
  guard configuration?.optOut != true else { return }
249
270
  ingest.enqueue(type: .analytics, jsonObject: payload)
250
271
  }
@@ -267,12 +288,17 @@ public final class SKAnalytics {
267
288
  /// The event dict must already carry an `event_id` (idempotency key) and
268
289
  /// `device_id`. We accept anything JSONSerializable.
269
290
  public func trackPreBuiltEvent(_ event: [String: Any]) {
270
- guard configuration?.optOut != true else { return }
291
+ // Synchronous on the caller's thread up to the disk write, as
292
+ // `SwissKnife.trackEvent` documents. Only the state read goes through
293
+ // `queue` (so it sees every earlier call); the enqueue runs here.
294
+ let client: IngestClient? = queue.sync {
295
+ configuration?.optOut == true ? nil : ingest
296
+ }
271
297
  // Pre-built events from the RN bridge already carry their own
272
298
  // event_id, device_id, super-property merges, etc. We pass them
273
299
  // through unchanged — IngestClient's ContextSnapshot stamping is
274
300
  // additive (it tops up missing fields rather than overwriting).
275
- ingestEnqueue(payload: event)
301
+ client?.enqueue(type: .analytics, jsonObject: event)
276
302
  }
277
303
 
278
304
  // MARK: Screen tracking (Mixpanel-compatible)
@@ -286,31 +312,36 @@ public final class SKAnalytics {
286
312
  // MARK: User identity
287
313
 
288
314
  public func setUserId(_ userId: String?) {
289
- self.userId = userId
315
+ queue.async { [self] in self.userId = userId }
290
316
  }
291
317
 
292
- public func getUserId() -> String? { userId }
318
+ public func getUserId() -> String? { queue.sync { userId } }
293
319
 
294
320
  public func setDeviceId(_ deviceId: String) {
295
- self.deviceId = deviceId
321
+ queue.async { [self] in self.deviceId = deviceId }
296
322
  }
297
323
 
298
- public func getDeviceId() -> String { deviceId }
324
+ public func getDeviceId() -> String { queue.sync { deviceId } }
299
325
 
300
- public func getSessionId() -> Int64 { sessionId }
326
+ public func getSessionId() -> Int64 { queue.sync { sessionId } }
301
327
 
302
328
  public func identify(identify: SKIdentify) {
303
- let payload: [String: Any] = [
304
- "event_type": "$identify",
305
- "user_properties": ["$set": identify.toDict()],
306
- ]
307
- ingestEnqueue(payload: payload)
308
-
309
- // Also apply locally
310
- for op in identify.operations {
311
- if let key = op["key"] as? String, let value = op["value"],
312
- (op["op"] as? String) == "$set" {
313
- userProperties[key] = value
329
+ // Snapshot the builder on the caller's thread: it is the caller's
330
+ // object and may be reused after this returns.
331
+ let operations = identify.operations
332
+ queue.async { [self] in
333
+ let payload: [String: Any] = [
334
+ "event_type": "$identify",
335
+ "user_properties": ["$set": operations],
336
+ ]
337
+ ingestEnqueueLocked(payload: payload)
338
+
339
+ // Also apply locally
340
+ for op in operations {
341
+ if let key = op["key"] as? String, let value = op["value"],
342
+ (op["op"] as? String) == "$set" {
343
+ userProperties[key] = value
344
+ }
314
345
  }
315
346
  }
316
347
  }
@@ -318,75 +349,89 @@ public final class SKAnalytics {
318
349
  // MARK: Super properties (Mixpanel-compatible)
319
350
 
320
351
  public func registerSuperProperties(_ properties: [String: Any]) {
321
- for (k, v) in properties { superProperties[k] = v }
352
+ queue.async { [self] in
353
+ for (k, v) in properties { superProperties[k] = v }
354
+ }
322
355
  }
323
356
 
324
357
  public func registerSuperPropertiesOnce(_ properties: [String: Any]) {
325
- for (k, v) in properties where superProperties[k] == nil {
326
- superProperties[k] = v
358
+ queue.async { [self] in
359
+ for (k, v) in properties where superProperties[k] == nil {
360
+ superProperties[k] = v
361
+ }
327
362
  }
328
363
  }
329
364
 
330
- public func getSuperProperties() -> [String: Any] { superProperties }
365
+ public func getSuperProperties() -> [String: Any] { queue.sync { superProperties } }
331
366
 
332
367
  public func unregisterSuperProperty(_ property: String) {
333
- superProperties.removeValue(forKey: property)
368
+ queue.async { [self] in _ = superProperties.removeValue(forKey: property) }
334
369
  }
335
370
 
336
371
  public func clearSuperProperties() {
337
- superProperties = [:]
372
+ queue.async { [self] in superProperties = [:] }
338
373
  }
339
374
 
340
375
  // MARK: Time events (Mixpanel-compatible)
341
376
 
342
377
  public func timeEvent(_ eventName: String) {
343
- timedEvents[eventName] = Date().timeIntervalSince1970
378
+ let startedAt = Date().timeIntervalSince1970
379
+ queue.async { [self] in timedEvents[eventName] = startedAt }
344
380
  }
345
381
 
346
382
  // MARK: Group helpers (Mixpanel-compatible)
347
383
 
348
384
  public func addGroup(groupType: String, groupName: String) {
349
- if var existing = groups[groupType] as? [String] {
350
- if !existing.contains(groupName) { existing.append(groupName) }
351
- groups[groupType] = existing
352
- } else {
353
- groups[groupType] = [groupName]
385
+ queue.async { [self] in
386
+ if var existing = groups[groupType] as? [String] {
387
+ if !existing.contains(groupName) { existing.append(groupName) }
388
+ groups[groupType] = existing
389
+ } else {
390
+ groups[groupType] = [groupName]
391
+ }
354
392
  }
355
393
  }
356
394
 
357
395
  public func reset() {
358
- userId = nil
359
- // `reset()` is the explicit "fresh user" path — we rotate the
360
- // in-memory deviceId but leave the persisted Keychain ID intact so
361
- // the device continues to be recognisable across launches. If a
362
- // consumer ever needs to fully wipe the persisted ID, expose a
363
- // dedicated API; doing it implicitly from reset() would defeat the
364
- // whole point of Keychain-backed persistence.
365
- deviceId = IDs.eventId()
366
- userProperties = [:]
367
- groups = [:]
368
- superProperties = [:]
369
- timedEvents = [:]
370
- sessionId = Int64(Date().timeIntervalSince1970 * 1000)
396
+ let newDeviceId = IDs.eventId()
397
+ let newSessionId = Int64(Date().timeIntervalSince1970 * 1000)
398
+ queue.async { [self] in
399
+ userId = nil
400
+ // `reset()` is the explicit "fresh user" path — we rotate the
401
+ // in-memory deviceId but leave the persisted Keychain ID intact so
402
+ // the device continues to be recognisable across launches. If a
403
+ // consumer ever needs to fully wipe the persisted ID, expose a
404
+ // dedicated API; doing it implicitly from reset() would defeat the
405
+ // whole point of Keychain-backed persistence.
406
+ deviceId = newDeviceId
407
+ userProperties = [:]
408
+ groups = [:]
409
+ superProperties = [:]
410
+ timedEvents = [:]
411
+ sessionId = newSessionId
412
+ }
371
413
  }
372
414
 
373
415
  // MARK: Groups (Amplitude-compatible)
374
416
 
375
417
  public func setGroup(groupType: String, groupName: String) {
376
- groups[groupType] = groupName
418
+ queue.async { [self] in groups[groupType] = groupName }
377
419
  }
378
420
 
379
421
  public func setGroup(groupType: String, groupNames: [String]) {
380
- groups[groupType] = groupNames
422
+ queue.async { [self] in groups[groupType] = groupNames }
381
423
  }
382
424
 
383
425
  public func groupIdentify(groupType: String, groupName: String, identify: SKIdentify) {
384
- let payload: [String: Any] = [
385
- "event_type": "$groupidentify",
386
- "groups": [groupType: groupName],
387
- "group_properties": ["$set": identify.toDict()],
388
- ]
389
- ingestEnqueue(payload: payload)
426
+ let operations = identify.operations
427
+ queue.async { [self] in
428
+ let payload: [String: Any] = [
429
+ "event_type": "$groupidentify",
430
+ "groups": [groupType: groupName],
431
+ "group_properties": ["$set": operations],
432
+ ]
433
+ ingestEnqueueLocked(payload: payload)
434
+ }
390
435
  }
391
436
 
392
437
  // MARK: Revenue tracking (Amplitude + Mixpanel compatible)
@@ -421,22 +466,26 @@ public final class SKAnalytics {
421
466
  /// two of the six triggers that used to race each other, and every
422
467
  /// racing pair re-uploaded whatever was in the queue. Do not
423
468
  /// "optimise" this into a direct `ingest.flush()`.
469
+ ///
470
+ /// The flush is kicked from `queue`, so it runs behind every `track`
471
+ /// called before it and carries those events.
424
472
  public func flush() {
425
- Task { [ingest] in
426
- await ingest.flushBestEffort()
473
+ queue.async { [self] in
474
+ let client = ingest
475
+ Task { await client.flushBestEffort() }
427
476
  }
428
477
  }
429
478
 
430
479
  // MARK: Opt out
431
480
 
432
481
  public func setOptOut(_ optOut: Bool) {
433
- configuration?.optOut = optOut
482
+ queue.async { [self] in configuration?.optOut = optOut }
434
483
  }
435
484
 
436
485
  // MARK: Private
437
486
 
438
- private func startFlushTimer() {
439
- guard let interval = configuration?.flushIntervalSeconds, interval > 0 else { return }
487
+ private func startFlushTimer(interval: TimeInterval) {
488
+ guard interval > 0 else { return }
440
489
  DispatchQueue.main.async {
441
490
  self.flushTimer?.invalidate()
442
491
  self.flushTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
@@ -451,7 +500,7 @@ public final class SKAnalytics {
451
500
  /// `DeviceID._resetCache_forTesting()` to simulate a fresh process where
452
501
  /// the SKAnalytics singleton is rebuilt from Keychain state.
453
502
  internal func _resetDeviceIdForTesting() {
454
- deviceId = DeviceID.get()
503
+ queue.sync { deviceId = DeviceID.get() }
455
504
  }
456
505
 
457
506
  /// Swap in a test-controlled IngestClient — usually one wired to a
@@ -99,7 +99,7 @@ internal enum DeviceContextProvider {
99
99
  /// Round A baseline. The build pipeline (`swiss-build`) will eventually
100
100
  /// inject this via `-D` flag, but for now keep it in source so we don't
101
101
  /// have to thread build settings through the SPM Package.swift.
102
- static let sdkVersionConstant: String = "0.4.0"
102
+ static let sdkVersionConstant: String = "0.5.0"
103
103
 
104
104
  private static let cacheLock = DispatchQueue(label: "com.swissknife.context.device")
105
105
  private static var cached: DeviceContext?