Skip to content

Commit

Permalink
chore: observing view tree
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Mar 20, 2024
1 parent 765e6ea commit f9204e6
Show file tree
Hide file tree
Showing 20 changed files with 272 additions and 72 deletions.
36 changes: 28 additions & 8 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
6992AA8E2AFE51CE00087600 /* PostHog.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AC745B5296D6FE60025C109 /* PostHog.framework */; };
6992AA8F2AFE51CE00087600 /* PostHog.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3AC745B5296D6FE60025C109 /* PostHog.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
6992AA942AFE529E00087600 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6992AA932AFE529E00087600 /* AppDelegate.swift */; };
6993C40B2B9F35E300075A72 /* PostHogSessionReplayConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6993C40A2B9F35E300075A72 /* PostHogSessionReplayConfig.swift */; };
699991882AFE1B37000DCB78 /* PostHogExampleMacOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 699991872AFE1B37000DCB78 /* PostHogExampleMacOSApp.swift */; };
6999918A2AFE1B37000DCB78 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 699991892AFE1B37000DCB78 /* ContentView.swift */; };
6999918C2AFE1B39000DCB78 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6999918B2AFE1B39000DCB78 /* Assets.xcassets */; };
Expand All @@ -83,6 +82,9 @@
699991952AFE1B56000DCB78 /* PostHog.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3AC745B5296D6FE60025C109 /* PostHog.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
6999919A2AFE1BAB000DCB78 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 699991992AFE1BAB000DCB78 /* AppDelegate.swift */; };
69BA38D72B888E8500AA69D6 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 69BA38D62B888E8500AA69D6 /* PrivacyInfo.xcprivacy */; };
69EE82BA2BA9C50400EB9542 /* PostHogReplayIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69EE82B92BA9C50400EB9542 /* PostHogReplayIntegration.swift */; };
69EE82BC2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69EE82BB2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift */; };
69EE82BE2BA9C8AA00EB9542 /* ViewLayoutTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69EE82BD2BA9C8AA00EB9542 /* ViewLayoutTracker.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -275,7 +277,6 @@
6992AA862AFE51A100087600 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6992AA892AFE51A100087600 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
6992AA932AFE529E00087600 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6993C40A2B9F35E300075A72 /* PostHogSessionReplayConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogSessionReplayConfig.swift; sourceTree = "<group>"; };
699991562AFE0E9F000DCB78 /* PostHogExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PostHogExample.entitlements; sourceTree = "<group>"; };
699991852AFE1B37000DCB78 /* PostHogExampleMacOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PostHogExampleMacOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
699991872AFE1B37000DCB78 /* PostHogExampleMacOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogExampleMacOSApp.swift; sourceTree = "<group>"; };
Expand All @@ -285,6 +286,9 @@
699991902AFE1B39000DCB78 /* PostHogExampleMacOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PostHogExampleMacOS.entitlements; sourceTree = "<group>"; };
699991992AFE1BAB000DCB78 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
69BA38D62B888E8500AA69D6 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
69EE82B92BA9C50400EB9542 /* PostHogReplayIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogReplayIntegration.swift; sourceTree = "<group>"; };
69EE82BB2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogSessionReplayConfig.swift; sourceTree = "<group>"; };
69EE82BD2BA9C8AA00EB9542 /* ViewLayoutTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewLayoutTracker.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -432,6 +436,7 @@
3AC745B7296D6FE60025C109 /* PostHog */ = {
isa = PBXGroup;
children = (
69EE82B82BA9C4DA00EB9542 /* Replay */,
69BA38E62B893F2200AA69D6 /* Resources */,
69779BED2AE6B29E00D7A48E /* Models */,
3AC745B8296D6FE60025C109 /* PostHog.h */,
Expand All @@ -451,7 +456,6 @@
6926DA8D2ADD2876005760D2 /* PostHogContext.swift */,
69779BEB2AE68E6900D7A48E /* UIViewController.swift */,
690FF0C42AEFAE8200A0B06B /* PostHogLegacyQueue.swift */,
6993C40A2B9F35E300075A72 /* PostHogSessionReplayConfig.swift */,
);
path = PostHog;
sourceTree = "<group>";
Expand Down Expand Up @@ -580,6 +584,16 @@
path = Resources;
sourceTree = "<group>";
};
69EE82B82BA9C4DA00EB9542 /* Replay */ = {
isa = PBXGroup;
children = (
69EE82B92BA9C50400EB9542 /* PostHogReplayIntegration.swift */,
69EE82BB2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift */,
69EE82BD2BA9C8AA00EB9542 /* ViewLayoutTracker.swift */,
);
path = Replay;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -723,7 +737,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1500;
LastUpgradeCheck = 1500;
LastUpgradeCheck = 1530;
TargetAttributes = {
3AA34CF6296D951A003398F4 = {
CreatedOnToolsVersion = 14.2;
Expand Down Expand Up @@ -893,9 +907,9 @@
buildActionMask = 2147483647;
files = (
690FF05F2AE7E2D400A0B06B /* Data+Gzip.swift in Sources */,
69EE82BE2BA9C8AA00EB9542 /* ViewLayoutTracker.swift in Sources */,
69261D1F2AD9681300232EC7 /* PostHogConsumerPayload.swift in Sources */,
690FF0BF2AEFA97F00A0B06B /* FileUtils.swift in Sources */,
6993C40B2B9F35E300075A72 /* PostHogSessionReplayConfig.swift in Sources */,
69261D252AD9787A00232EC7 /* PostHogExtensions.swift in Sources */,
3AE3FB4E2993D1D600AFFC18 /* PostHogSessionManager.swift in Sources */,
3AE3FB49299391DF00AFFC18 /* PostHogStorage.swift in Sources */,
Expand All @@ -913,6 +927,8 @@
3AE3FB332991388500AFFC18 /* PostHogQueue.swift in Sources */,
690FF0B52AEBBD3C00A0B06B /* DictUtils.swift in Sources */,
69261D1B2AD9678C00232EC7 /* PostHogEvent.swift in Sources */,
69EE82BC2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift in Sources */,
69EE82BA2BA9C50400EB9542 /* PostHogReplayIntegration.swift in Sources */,
3AE3FB472992AB0000AFFC18 /* Hedgelog.swift in Sources */,
69261D132AD5685B00232EC7 /* PostHogFeatureFlags.swift in Sources */,
69261D1D2AD967CD00232EC7 /* PostHogFileBackedQueue.swift in Sources */,
Expand Down Expand Up @@ -1031,7 +1047,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = PostHogExample/PostHogExample.entitlements;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"PostHogExample/Preview Content\"";
Expand All @@ -1046,10 +1062,12 @@
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited)";
"LD_RUNPATH_SEARCH_PATHS[arch=*]" = "@executable_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHogExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
Expand All @@ -1066,7 +1084,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = PostHogExample/PostHogExample.entitlements;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"PostHogExample/Preview Content\"";
Expand All @@ -1085,6 +1103,7 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHogExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
Expand Down Expand Up @@ -1230,10 +1249,11 @@
buildSettings = {
BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = PNC2XCH2XP;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down
2 changes: 1 addition & 1 deletion PostHog.xcodeproj/xcshareddata/xcschemes/PostHog.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
11 changes: 9 additions & 2 deletions PostHog.xcodeproj/xcshareddata/xcschemes/PostHogExample.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -31,7 +31,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -50,6 +50,13 @@
ReferencedContainer = "container:PostHog.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
<AdditionalOption
key = "APP_DISTRIBUTOR_ID_OVERRIDE"
value = "com.apple.AppStore"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1530"
version = "2.2">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
19 changes: 11 additions & 8 deletions PostHog/PostHogConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ import Foundation
@objc public var captureScreenViews: Bool = true
@objc public var debug: Bool = false
@objc public var optOut: Bool = false
/// Enable Recording of Session Replays for iOS
/// Experimental support
/// Default: false
@objc public var sessionReplay: Bool = false
/// Session Replay configuration
/// Experimental support
/// Default: false
@objc public var sessionReplayConfig: PostHogSessionReplayConfig = .init()
/// Internal
var snapshotEndpoint: String = "/s/"

public static let defaultHost: String = "https://app.posthog.com"

#if os(iOS) || os(tvOS)
/// Enable Recording of Session Replays for iOS
/// Experimental support
/// Default: false
@objc public var sessionReplay: Bool = false
/// Session Replay configuration
/// Experimental support
/// Default: false
@objc public var sessionReplayConfig: PostHogSessionReplayConfig = .init()
#endif

// only internal
var disableReachabilityForTesting: Bool = false
var disableQueueTimerForTesting: Bool = false
Expand Down
12 changes: 12 additions & 0 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class PostHogContext {
properties["$device_manufacturer"] = "Apple"
properties["$device_model"] = platform()

// if there's a receipt, we assume its installed via store or testflight
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path)
{
properties["$is_sideloaded"] = false
// if the receipt ends with sandboxReceipt, we assume its a testing build (testflight)
properties["$is_testflight"] = appStoreReceiptURL.lastPathComponent == "sandboxReceipt"
} else {
properties["$is_sideloaded"] = true
properties["$is_testflight"] = false
}

#if os(iOS) || os(tvOS)
let device = UIDevice.current
// use https://github.com/devicekit/DeviceKit
Expand Down
32 changes: 30 additions & 2 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
private var sessionId: String?
private var sessionLastTimestamp: TimeInterval?
private var isInBackground = false
#if os(iOS) || os(tvOS)
private var replayIntegration: PostHogReplayIntegration?
#endif

@objc public static let shared: PostHogSDK = {
let instance = PostHogSDK(PostHogConfig(apiKey: ""))
Expand Down Expand Up @@ -93,6 +96,9 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
api = theApi
featureFlags = PostHogFeatureFlags(config, theStorage, theApi)
sessionManager = PostHogSessionManager(config)
#if os(iOS) || os(tvOS)
replayIntegration = PostHogReplayIntegration(config)
#endif
#if !os(watchOS)
do {
reachability = try Reachability()
Expand Down Expand Up @@ -129,6 +135,12 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30

rotateSession()

#if os(iOS) || os(tvOS)
if config.sessionReplay {
replayIntegration?.start()
}
#endif

DispatchQueue.main.async {
NotificationCenter.default.post(name: PostHogSDK.didStartNotification, object: nil)
}
Expand Down Expand Up @@ -755,7 +767,17 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
enabled = false
PostHogSDK.apiKeys.remove(config.apiKey)

if config.captureScreenViews {
#if os(iOS) || os(tvOS)
UIViewController.unswizzleScreenView()
#endif
}

queue?.stop()
#if os(iOS) || os(tvOS)
replayIntegration?.stop()
replayIntegration = nil
#endif
queue = nil
replayQueue = nil
sessionManager = nil
Expand All @@ -768,15 +790,13 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
reachability = nil
#endif
flagCallReported.removeAll()
featureFlags = nil
context = nil
resetSession()
unregisterNotifications()
capturedAppInstalled = false
appFromBackground = false
isInBackground = false
toggleHedgeLog(false)
// TODO: remove swizzlers
}
}

Expand Down Expand Up @@ -954,4 +974,12 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
}
capture("Application Backgrounded")
}

func isSessionActive() -> Bool {
var active = false
sessionLock.withLock {
active = sessionId != nil
}
return active
}
}
20 changes: 0 additions & 20 deletions PostHog/PostHogSessionReplayConfig.swift

This file was deleted.

Loading

0 comments on commit f9204e6

Please sign in to comment.