@app-swiss-knife/react-native-sdk 0.4.0 → 0.5.1
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.
- package/CHANGELOG.md +473 -2
- package/LICENSE +21 -0
- package/README.md +279 -47
- package/SwissKnifePurchases.podspec +1 -1
- package/android/build.gradle +24 -2
- package/android/consumer-rules.pro +12 -0
- package/android/src/main/AndroidManifest.xml +15 -1
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +5 -4
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +20 -2
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +168 -6
- package/android/src/play/java/com/swissknife/purchases/StoreIntentHandler.kt +7 -0
- package/android/src/rustore/java/com/swissknife/purchases/StoreIntentHandler.kt +9 -0
- package/android/vendor/GENERATED.md +1 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +111 -38
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +180 -115
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +16 -18
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +13 -13
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +56 -25
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +365 -206
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +195 -26
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +31 -6
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +5 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +56 -31
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +673 -87
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +214 -30
- package/android/vendor/java/com/appswissknife/notifications/NotificationRenderer.kt +195 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionRequestActivity.kt +60 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionStatus.kt +41 -0
- package/android/vendor/java/com/appswissknife/notifications/PushEventQueue.kt +143 -0
- package/android/vendor/java/com/appswissknife/notifications/PushPayload.kt +72 -0
- package/android/vendor/java/com/appswissknife/notifications/PushRegistration.kt +89 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeMessagingService.kt +57 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeNotifications.kt +438 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +312 -65
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +1 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +705 -96
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +55 -24
- package/android/vendor/java/com/appswissknife/purchases/ProductKindHint.kt +29 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +24 -0
- package/android/vendor-rustore/AndroidManifest.xml +59 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreGateway.kt +428 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreInitProvider.kt +43 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStorePriceEnrichmentProvider.kt +85 -0
- package/app.plugin.js +343 -0
- package/ios/SwissKnifeJSON.swift +24 -2
- package/ios/SwissKnifePurchases.m +20 -0
- package/ios/SwissKnifePurchases.swift +97 -3
- package/ios/SwissKnifePushBootstrap.m +34 -0
- package/ios/vendor/GENERATED.md +1 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +138 -89
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +345 -44
- package/ios/vendor/SwissKnifeiOS/Net/BackendBackoffGate.swift +43 -2
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +495 -16
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +231 -23
- package/ios/vendor/SwissKnifeiOS/Notifications/PushPayload.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/PushRegistrar.swift +322 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/SwissKnife+Notifications.swift +403 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +1 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +11 -7
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +37 -5
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +38 -7
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +34 -6
- package/ios/vendor/SwissKnifeiOS/Types.swift +31 -1
- package/ios-notification-service/SwissKnifeNotificationService.podspec +37 -0
- package/ios-notification-service/vendor/GENERATED.md +11 -0
- package/ios-notification-service/vendor/SwissKnifeNotificationService/SwissKnifeNotificationServiceExtension.swift +177 -0
- package/lib/commonjs/attribution.js +4 -3
- package/lib/commonjs/attribution.js.map +1 -1
- package/lib/commonjs/identity.js +12 -11
- package/lib/commonjs/identity.js.map +1 -1
- package/lib/commonjs/index.js +16 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal/config.js +105 -8
- package/lib/commonjs/internal/config.js.map +1 -1
- package/lib/commonjs/internal/telemetry-context.js +70 -0
- package/lib/commonjs/internal/telemetry-context.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +18 -25
- package/lib/commonjs/modules/analytics.js.map +1 -1
- package/lib/commonjs/modules/crash-reporting.js +27 -6
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/commonjs/modules/log.js +1 -1
- package/lib/commonjs/modules/notifications.js +500 -0
- package/lib/commonjs/modules/notifications.js.map +1 -0
- package/lib/commonjs/modules/performance.js +57 -8
- package/lib/commonjs/modules/performance.js.map +1 -1
- package/lib/commonjs/native/module.js +6 -0
- package/lib/commonjs/native/module.js.map +1 -1
- package/lib/commonjs/native/spec.js +12 -5
- package/lib/commonjs/native/spec.js.map +1 -1
- package/lib/commonjs/purchases.js +4 -4
- package/lib/commonjs/types.js +12 -15
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/attribution.js +4 -3
- package/lib/module/attribution.js.map +1 -1
- package/lib/module/identity.js +12 -11
- package/lib/module/identity.js.map +1 -1
- package/lib/module/index.js +8 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal/config.js +103 -8
- package/lib/module/internal/config.js.map +1 -1
- package/lib/module/internal/telemetry-context.js +63 -0
- package/lib/module/internal/telemetry-context.js.map +1 -0
- package/lib/module/modules/analytics.js +18 -25
- package/lib/module/modules/analytics.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +27 -6
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/module/modules/log.js +1 -1
- package/lib/module/modules/notifications.js +491 -0
- package/lib/module/modules/notifications.js.map +1 -0
- package/lib/module/modules/performance.js +57 -8
- package/lib/module/modules/performance.js.map +1 -1
- package/lib/module/native/module.js +6 -1
- package/lib/module/native/module.js.map +1 -1
- package/lib/module/native/spec.js +11 -4
- package/lib/module/native/spec.js.map +1 -1
- package/lib/module/purchases.js +4 -4
- package/lib/module/types.js +12 -15
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/commonjs/attribution.d.ts +3 -2
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -1
- package/lib/typescript/commonjs/identity.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +2 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/config.d.ts +16 -2
- package/lib/typescript/commonjs/internal/config.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +8 -11
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +18 -5
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/notifications.d.ts +110 -0
- package/lib/typescript/commonjs/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +13 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/module.d.ts +2 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/spec.d.ts +33 -4
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -1
- package/lib/typescript/commonjs/purchases.d.ts +4 -4
- package/lib/typescript/commonjs/types.d.ts +4 -5
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/module/attribution.d.ts +3 -2
- package/lib/typescript/module/attribution.d.ts.map +1 -1
- package/lib/typescript/module/identity.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +2 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/internal/config.d.ts +16 -2
- package/lib/typescript/module/internal/config.d.ts.map +1 -1
- package/lib/typescript/module/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/module/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +8 -11
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts +18 -5
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/modules/notifications.d.ts +110 -0
- package/lib/typescript/module/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +13 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -1
- package/lib/typescript/module/native/module.d.ts +2 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -1
- package/lib/typescript/module/native/spec.d.ts +33 -4
- package/lib/typescript/module/native/spec.d.ts.map +1 -1
- package/lib/typescript/module/purchases.d.ts +4 -4
- package/lib/typescript/module/types.d.ts +4 -5
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/package.json +13 -4
- package/src/attribution.ts +4 -3
- package/src/identity.ts +12 -11
- package/src/index.ts +16 -4
- package/src/internal/config.ts +113 -8
- package/src/internal/telemetry-context.ts +76 -0
- package/src/modules/analytics.ts +24 -33
- package/src/modules/crash-reporting.ts +50 -9
- package/src/modules/log.ts +1 -1
- package/src/modules/notifications.ts +557 -0
- package/src/modules/performance.ts +94 -14
- package/src/native/module.ts +6 -0
- package/src/native/spec.ts +45 -10
- package/src/purchases.ts +4 -4
- package/src/types.ts +12 -15
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +0 -326
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +0 -296
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// packages/react-native-sdk/ios/SwissKnifePushBootstrap.m
|
|
2
|
+
//
|
|
3
|
+
// Cold-start notification taps need the UNUserNotificationCenter delegate to
|
|
4
|
+
// exist by the time the app finishes launching; otherwise iOS answers the
|
|
5
|
+
// pending response with nothing and the tap is lost. React Native instantiates
|
|
6
|
+
// the `SwissKnifePurchases` module lazily (when JS first touches it), which is
|
|
7
|
+
// AFTER the launch response has already been dropped, so resuming push
|
|
8
|
+
// auto-integration from the module's init is too late. Hook the launch
|
|
9
|
+
// notification from +load instead (no AppDelegate edit needed) and re-install
|
|
10
|
+
// the delegate proxy when the app opted in on an earlier launch.
|
|
11
|
+
#import <Foundation/Foundation.h>
|
|
12
|
+
#import <UIKit/UIKit.h>
|
|
13
|
+
|
|
14
|
+
@interface SwissKnifePushBootstrap : NSObject
|
|
15
|
+
@end
|
|
16
|
+
|
|
17
|
+
@implementation SwissKnifePushBootstrap
|
|
18
|
+
|
|
19
|
+
+ (void)load {
|
|
20
|
+
[[NSNotificationCenter defaultCenter]
|
|
21
|
+
addObserverForName:UIApplicationDidFinishLaunchingNotification
|
|
22
|
+
object:nil
|
|
23
|
+
queue:nil
|
|
24
|
+
usingBlock:^(NSNotification *note) {
|
|
25
|
+
Class moduleClass = NSClassFromString(@"SwissKnifePurchases");
|
|
26
|
+
SEL resume = NSSelectorFromString(@"resumePushEarly");
|
|
27
|
+
if (moduleClass && [moduleClass respondsToSelector:resume]) {
|
|
28
|
+
void (*call)(id, SEL) = (void (*)(id, SEL))[moduleClass methodForSelector:resume];
|
|
29
|
+
call(moduleClass, resume);
|
|
30
|
+
}
|
|
31
|
+
}];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@end
|
package/ios/vendor/GENERATED.md
CHANGED
|
@@ -4,6 +4,7 @@ Copied verbatim by packages/react-native-sdk/scripts/vendor-native.mjs from the
|
|
|
4
4
|
standalone native SDK sources in the monorepo. Edit those instead:
|
|
5
5
|
|
|
6
6
|
packages/swiss-knife-ios/Sources/SwissKnifeiOS/**
|
|
7
|
+
packages/swiss-knife-ios/Sources/SwissKnifeNotificationService/**
|
|
7
8
|
packages/swiss-knife-android/src/main/{java,res}/**
|
|
8
9
|
|
|
9
10
|
This directory is gitignored and regenerated on every `build` / `prepack`.
|
|
@@ -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
|
-
|
|
193
|
-
shared.
|
|
194
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
]
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
-
|
|
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
|
-
|
|
326
|
-
superProperties[k]
|
|
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
|
-
|
|
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
|
-
|
|
350
|
-
if
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
426
|
-
|
|
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
|
|
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.
|
|
102
|
+
static let sdkVersionConstant: String = "0.5.1"
|
|
103
103
|
|
|
104
104
|
private static let cacheLock = DispatchQueue(label: "com.swissknife.context.device")
|
|
105
105
|
private static var cached: DeviceContext?
|