diff --git a/PostHog/PostHogFeatureFlags.swift b/PostHog/PostHogFeatureFlags.swift index ccff706af..4477751b3 100644 --- a/PostHog/PostHogFeatureFlags.swift +++ b/PostHog/PostHogFeatureFlags.swift @@ -31,10 +31,10 @@ class PostHogFeatureFlags { self.storage = storage self.api = api - preloadSesssionReplayFlag() + preloadSessionReplayFlag() } - private func preloadSesssionReplayFlag() { + private func preloadSessionReplayFlag() { var sessionReplay: [String: Any]? var featureFlags: [String: Any]? featureFlagsLock.withLock { @@ -70,7 +70,7 @@ class PostHogFeatureFlags { // check for multi flag variant (any) // if let linkedFlag = sessionRecording["linkedFlag"] as? String, // featureFlags[linkedFlag] != nil - // is also a valid check bbut since we cannot check the value of the flag, + // is also a valid check but since we cannot check the value of the flag, // we consider session recording is active return recordingActive @@ -117,7 +117,7 @@ class PostHogFeatureFlags { } else if let sessionRecording = data?["sessionRecording"] as? [String: Any] { // keeps the value from config.sessionReplay since having sessionRecording // means its enabled on the project settings, but its only enabled - // when local config.sessionReplay is also enabled + // when local replay integration is enabled/active if let endpoint = sessionRecording["endpoint"] as? String { self.config.snapshotEndpoint = endpoint } @@ -242,7 +242,7 @@ class PostHogFeatureFlags { hedgeLog("Error parsing the object \(String(describing: value)): \(error)") } - // fallbak to original value if not possible to serialize + // fallback to original value if not possible to serialize return value } diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index 149e8a2a8..2797baefe 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -149,8 +149,8 @@ let maxRetryDelay = 30.0 PostHogSessionManager.shared.startSession() #if os(iOS) - if config.sessionReplay, config.sessionReplayConfig.startMode == .automatic { - startSessionRecording() + if config.sessionReplay { + replayIntegration?.start() } #endif @@ -334,7 +334,7 @@ let maxRetryDelay = 30.0 // only Session replay requires $window_id, so we set as the same as $session_id. // the backend might fallback to $session_id if $window_id is not present next. #if os(iOS) - if !appendSharedProps, config.sessionReplay { + if !appendSharedProps, isSessionReplayActive() { props["$window_id"] = sessionId } #endif @@ -1031,12 +1031,17 @@ let maxRetryDelay = 30.0 return } - guard let replayIntegration, !replayIntegration.isActive() else { + guard let replayIntegration else { return } - guard config.sessionReplay else { - return hedgeLog("Could not resume recording. Session replay is disabled in config.") + if resumeCurrent, replayIntegration.isActive() { + // nothing to resume, already active + return + } + + guard let featureFlags, featureFlags.isSessionReplayFlagActive() else { + return hedgeLog("Could not start recording. Session replay feature flag is disabled.") } let sessionId = resumeCurrent @@ -1264,14 +1269,13 @@ let maxRetryDelay = 30.0 return false } - guard let replayIntegration else { + guard let replayIntegration, let featureFlags else { return false } - return config.sessionReplay + return replayIntegration.isActive() && !PostHogSessionManager.shared.getSessionId(readOnly: true).isNilOrEmpty - && replayIntegration.isActive() - && (featureFlags?.isSessionReplayFlagActive() ?? false) + && featureFlags.isSessionReplayFlagActive() } #endif diff --git a/PostHog/PostHogSessionManager.swift b/PostHog/PostHogSessionManager.swift index 53afd35c8..11984f092 100644 --- a/PostHog/PostHogSessionManager.swift +++ b/PostHog/PostHogSessionManager.swift @@ -40,7 +40,7 @@ import Foundation // 24 hours in seconds private let sessionMaxLengthThreshold: TimeInterval = 24 * 60 * 60 // Called when session id is cleared or changes - var onSessionIDChanged: () -> Void = {} + var onSessionIdChanged: () -> Void = {} @objc public func setSessionId(_ sessionId: String) { setSessionIdInternal(sessionId, reason: .customSessionId) @@ -177,7 +177,7 @@ import Foundation self.sessionActivityTimestamp = newTimestamp } - onSessionIDChanged() + onSessionIdChanged() if let sessionId { hedgeLog("New session id created \(sessionId) (\(reason))") diff --git a/PostHog/Replay/PostHogReplayIntegration.swift b/PostHog/Replay/PostHogReplayIntegration.swift index 216208aca..6fe32ef2f 100644 --- a/PostHog/Replay/PostHogReplayIntegration.swift +++ b/PostHog/Replay/PostHogReplayIntegration.swift @@ -106,7 +106,7 @@ func start() { stopTimer() // reset views when session id changes (or is cleared) so we can re-send new metadata (or full snapshot in the future) - PostHogSessionManager.shared.onSessionIDChanged = resetViews + PostHogSessionManager.shared.onSessionIdChanged = resetViews // flutter captures snapshots, so we don't need to capture them here if isNotFlutter() { @@ -128,7 +128,7 @@ func stop() { stopTimer() resetViews() - PostHogSessionManager.shared.onSessionIDChanged = {} + PostHogSessionManager.shared.onSessionIdChanged = {} ViewLayoutTracker.unSwizzleLayoutSubviews() UIApplicationTracker.unswizzleSendEvent() @@ -157,7 +157,7 @@ let snapshotStatus = windowViews.object(forKey: window) ?? ViewTreeSnapshotStatus() // always make sure we have a fresh session id as early as possible - guard let sessionId: String = PostHogSessionManager.shared.getSessionId(at: timestampDate) else { + guard let sessionId = PostHogSessionManager.shared.getSessionId(at: timestampDate) else { return } diff --git a/PostHog/Replay/PostHogSessionReplayConfig.swift b/PostHog/Replay/PostHogSessionReplayConfig.swift index c38564d6c..ce1ac073f 100644 --- a/PostHog/Replay/PostHogSessionReplayConfig.swift +++ b/PostHog/Replay/PostHogSessionReplayConfig.swift @@ -50,18 +50,6 @@ /// Defaults to 1s @objc public var debouncerDelay: TimeInterval = 1.0 - /// Whether to start session replay automatically or manually during the SDK setup - /// Options are: - /// - .automatic: Start session replay automatically during the SDK setup - /// - .manual: Start/Stop session replay manually by calling `startSessionReplay()` and `stopSessionReplay()` - /// Default is .automatic - @objc public var startMode: PostHogSessionReplayStartMode = .automatic - // TODO: sessionRecording config such as networkPayloadCapture, captureConsoleLogs, sampleRate, etc } - - @objc(PostHogSessionReplayStartMode) public enum PostHogSessionReplayStartMode: Int32 { - case automatic - case manual - } #endif diff --git a/PostHog/Replay/UIApplicationTracker.swift b/PostHog/Replay/UIApplicationTracker.swift index adb17ade5..ded02cd9c 100644 --- a/PostHog/Replay/UIApplicationTracker.swift +++ b/PostHog/Replay/UIApplicationTracker.swift @@ -53,7 +53,7 @@ } // always make sure we have a fresh session id as early as possible - guard let sessionId: String = PostHogSessionManager.shared.getSessionId(at: date) else { + guard let sessionId = PostHogSessionManager.shared.getSessionId(at: date) else { return } diff --git a/PostHog/Replay/URLSessionExtension.swift b/PostHog/Replay/URLSessionExtension.swift index 59cf5b185..43374ed4d 100644 --- a/PostHog/Replay/URLSessionExtension.swift +++ b/PostHog/Replay/URLSessionExtension.swift @@ -115,7 +115,7 @@ let currentEnd = end ?? getMonotonicTimeInMilliseconds() // always make sure we have a fresh session id as early as possible - guard let sessionId: String = PostHogSessionManager.shared.getSessionId(at: timestamp) else { + guard let sessionId = PostHogSessionManager.shared.getSessionId(at: timestamp) else { return } diff --git a/PostHog/Replay/URLSessionInterceptor.swift b/PostHog/Replay/URLSessionInterceptor.swift index 995869e96..441bffe49 100644 --- a/PostHog/Replay/URLSessionInterceptor.swift +++ b/PostHog/Replay/URLSessionInterceptor.swift @@ -125,7 +125,7 @@ let timestamp = sample.timeOrigin // always make sure we have a fresh session id as early as possible - guard let sessionId: String = PostHogSessionManager.shared.getSessionId(at: timestamp) else { + guard let sessionId = PostHogSessionManager.shared.getSessionId(at: timestamp) else { return } diff --git a/PostHogObjCExample/AppDelegate.m b/PostHogObjCExample/AppDelegate.m index 3b44bddca..652d466f6 100644 --- a/PostHogObjCExample/AppDelegate.m +++ b/PostHogObjCExample/AppDelegate.m @@ -29,7 +29,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( PostHogConfig *config = [[PostHogConfig alloc] apiKey:@"_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI"]; config.preloadFeatureFlags = YES; - config.sessionReplayConfig.startMode = PostHogSessionReplayStartModeManual; [[PostHogSDK shared] debug:YES]; [[PostHogSDK shared] setup:config];