Skip to content

Commit

Permalink
fix: remove start mode from session replay config
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj committed Dec 24, 2024
1 parent ce0bb4a commit 241282d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 36 deletions.
10 changes: 5 additions & 5 deletions PostHog/PostHogFeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 14 additions & 10 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions PostHog/PostHogSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -177,7 +177,7 @@ import Foundation
self.sessionActivityTimestamp = newTimestamp
}

onSessionIDChanged()
onSessionIdChanged()

if let sessionId {
hedgeLog("New session id created \(sessionId) (\(reason))")
Expand Down
6 changes: 3 additions & 3 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -128,7 +128,7 @@
func stop() {
stopTimer()
resetViews()
PostHogSessionManager.shared.onSessionIDChanged = {}
PostHogSessionManager.shared.onSessionIdChanged = {}

ViewLayoutTracker.unSwizzleLayoutSubviews()
UIApplicationTracker.unswizzleSendEvent()
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 0 additions & 12 deletions PostHog/Replay/PostHogSessionReplayConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion PostHog/Replay/UIApplicationTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion PostHog/Replay/URLSessionExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion PostHog/Replay/URLSessionInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion PostHogObjCExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit 241282d

Please sign in to comment.