Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions PostHog/AppLifeCycle/PostHogAppLifeCycleIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import Foundation
final class PostHogAppLifeCycleIntegration: PostHogIntegration {
var requiresSwizzling: Bool { false }

private static var integrationInstalledLock = NSLock()
private static var integrationInstalled = false
private static let integrationInstallState = PostHogIntegrationInstallState()
private static var didCaptureAppInstallOrUpdate = false

private weak var postHog: PostHogSDK?
Expand All @@ -35,33 +34,19 @@ final class PostHogAppLifeCycleIntegration: PostHogIntegration {
private var didFinishLaunchingToken: RegistrationToken?

func install(_ postHog: PostHogSDK) -> PostHogIntegrationInstallResult {
let didInstall = PostHogAppLifeCycleIntegration.integrationInstalledLock.withLock {
if PostHogAppLifeCycleIntegration.integrationInstalled {
return false
}
PostHogAppLifeCycleIntegration.integrationInstalled = true
return true
}
installIfNeeded(using: Self.integrationInstallState) {
self.postHog = postHog

guard didInstall else {
return .skipped(.alreadyInstalled)
start()
captureAppInstallOrUpdated()
}

self.postHog = postHog

start()
captureAppInstallOrUpdated()
return .installed
}

func uninstall(_ postHog: PostHogSDK) {
// uninstall only for integration instance
if self.postHog === postHog || self.postHog == nil {
uninstallIfNeeded(from: postHog, installedPostHog: self.postHog, state: Self.integrationInstallState) {
// uninstall only for integration instance
stop()
self.postHog = nil
PostHogAppLifeCycleIntegration.integrationInstalledLock.withLock {
PostHogAppLifeCycleIntegration.integrationInstalled = false
}
}
}

Expand Down Expand Up @@ -214,9 +199,7 @@ final class PostHogAppLifeCycleIntegration: PostHogIntegration {
extension PostHogAppLifeCycleIntegration {
static func clearInstalls() {
PostHogAppLifeCycleIntegration.didCaptureAppInstallOrUpdate = false
integrationInstalledLock.withLock {
integrationInstalled = false
}
integrationInstallState.clear()
}
}
#endif
31 changes: 7 additions & 24 deletions PostHog/Autocapture/PostHogAutocaptureIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,24 @@
class PostHogAutocaptureIntegration: AutocaptureEventProcessing, PostHogIntegration {
var requiresSwizzling: Bool { true }

private static var integrationInstalledLock = NSLock()
private static var integrationInstalled = false
private static let integrationInstallState = PostHogIntegrationInstallState()

private weak var postHog: PostHogSDK?
private var debounceTimers: [Int: Timer] = [:]

func install(_ postHog: PostHogSDK) -> PostHogIntegrationInstallResult {
let didInstall = PostHogAutocaptureIntegration.integrationInstalledLock.withLock {
if PostHogAutocaptureIntegration.integrationInstalled {
return false
}
PostHogAutocaptureIntegration.integrationInstalled = true
return true
}
installIfNeeded(using: Self.integrationInstallState) {
self.postHog = postHog

guard didInstall else {
return .skipped(.alreadyInstalled)
start()
}

self.postHog = postHog

start()
return .installed
}

func uninstall(_ postHog: PostHogSDK) {
// uninstall only for integration instance
if self.postHog === postHog || self.postHog == nil {
uninstallIfNeeded(from: postHog, installedPostHog: self.postHog, state: Self.integrationInstallState) {
// uninstall only for integration instance
stop()
self.postHog = nil
PostHogAutocaptureIntegration.integrationInstalledLock.withLock {
PostHogAutocaptureIntegration.integrationInstalled = false
}
}
}

Expand Down Expand Up @@ -140,9 +125,7 @@
#if TESTING
extension PostHogAutocaptureIntegration {
static func clearInstalls() {
integrationInstalledLock.withLock {
integrationInstalled = false
}
integrationInstallState.clear()
}
}
#endif
Expand Down
33 changes: 8 additions & 25 deletions PostHog/Autocapture/PostHogRageClickIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,26 @@
final class PostHogRageClickIntegration: PostHogIntegration {
var requiresSwizzling: Bool { true }

private static var integrationInstalledLock = NSLock()
private static var integrationInstalled = false
private static let integrationInstallState = PostHogIntegrationInstallState()

private weak var postHog: PostHogSDK?
private var rageClickDetector: RageClickDetector?
private var applicationEventToken: RegistrationToken?

func install(_ postHog: PostHogSDK) -> PostHogIntegrationInstallResult {
let didInstall = PostHogRageClickIntegration.integrationInstalledLock.withLock {
if PostHogRageClickIntegration.integrationInstalled {
return false
}
PostHogRageClickIntegration.integrationInstalled = true
return true
}
installIfNeeded(using: Self.integrationInstallState) {
self.postHog = postHog
rageClickDetector = RageClickDetector(config: postHog.config.rageClickConfig)

guard didInstall else {
return .skipped(.alreadyInstalled)
start()
}

self.postHog = postHog
rageClickDetector = RageClickDetector(config: postHog.config.rageClickConfig)

start()
return .installed
}

func uninstall(_ postHog: PostHogSDK) {
// uninstall only for integration instance
if self.postHog === postHog || self.postHog == nil {
uninstallIfNeeded(from: postHog, installedPostHog: self.postHog, state: Self.integrationInstallState) {
// uninstall only for integration instance
stop()
self.postHog = nil
PostHogRageClickIntegration.integrationInstalledLock.withLock {
PostHogRageClickIntegration.integrationInstalled = false
}
}
}

Expand Down Expand Up @@ -150,9 +135,7 @@
#if TESTING
extension PostHogRageClickIntegration {
static func clearInstalls() {
integrationInstalledLock.withLock {
integrationInstalled = false
}
integrationInstallState.clear()
}

func processTapForTesting(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import Foundation
@_implementationOnly import PHPLCrashReporter

class PostHogErrorTrackingAutoCaptureIntegration: PostHogIntegration {
private static let integrationInstalledLock = NSLock()
private static var integrationInstalled = false
private static let integrationInstallState = PostHogIntegrationInstallState()

var requiresSwizzling: Bool { false }

Expand All @@ -25,37 +24,22 @@ import Foundation
return .skipped(.disabledByRemoteConfig)
}

let installed = PostHogErrorTrackingAutoCaptureIntegration.integrationInstalledLock.withLock {
if PostHogErrorTrackingAutoCaptureIntegration.integrationInstalled {
return false
return installIfNeeded(using: Self.integrationInstallState) {
if let crashReporter = setupCrashReporter() {
self.crashReporter = crashReporter
self.postHog = postHog
// Note: Order here matters, we need to process any pending crash report before enabling the crash reporter
processPendingCrashReportIfNeeded(reporter: crashReporter)
enableCrashReporter(reporter: crashReporter)
}
PostHogErrorTrackingAutoCaptureIntegration.integrationInstalled = true
return true
}

guard installed else {
return .skipped(.alreadyInstalled)
}

if let crashReporter = setupCrashReporter() {
self.crashReporter = crashReporter
self.postHog = postHog
// Note: Order here matters, we need to process any pending crash report before enabling the crash reporter
processPendingCrashReportIfNeeded(reporter: crashReporter)
enableCrashReporter(reporter: crashReporter)
}

return .installed
}

func uninstall(_ postHog: PostHogSDK) {
if self.postHog === postHog || self.postHog == nil {
uninstallIfNeeded(from: postHog, installedPostHog: self.postHog, state: Self.integrationInstallState) {
stop()
crashReporter = nil
self.postHog = nil
PostHogErrorTrackingAutoCaptureIntegration.integrationInstalledLock.withLock {
PostHogErrorTrackingAutoCaptureIntegration.integrationInstalled = false
}
}
}

Expand Down
32 changes: 11 additions & 21 deletions PostHog/ErrorTracking/PostHogExceptionProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ enum PostHogExceptionProcessor {
mechanismType: String = "generic",
config: PostHogErrorTrackingConfig
) -> [String: Any] {
var properties: [String: Any] = [:]

properties["$exception_level"] = "error"

let exceptions = buildExceptionList(
from: error,
handled: handled,
mechanismType: mechanismType,
config: config
)

attachExceptionsAndDebugImages(exceptions, to: &properties)

return properties
return buildProperties(exceptions: exceptions)
}

/// Convert NSException to properties
Expand All @@ -63,19 +57,14 @@ enum PostHogExceptionProcessor {
mechanismType: String = "generic",
config: PostHogErrorTrackingConfig
) -> [String: Any] {
var properties: [String: Any] = [:]
properties["$exception_level"] = "error" // TODO: figure this out from error wrapped type
Comment thread
marandaneto marked this conversation as resolved.

let exceptions = buildExceptionList(
from: exception,
handled: handled,
mechanismType: mechanismType,
config: config
)

attachExceptionsAndDebugImages(exceptions, to: &properties)

return properties
return buildProperties(exceptions: exceptions)
}

/// Convert a message string to properties
Expand All @@ -90,10 +79,6 @@ enum PostHogExceptionProcessor {
mechanismType: String = "generic",
config: PostHogErrorTrackingConfig
) -> [String: Any] {
var properties: [String: Any] = [:]

properties["$exception_level"] = "error"

var exception: [String: Any] = [:]
exception["type"] = "Message"
exception["value"] = message
Expand All @@ -109,14 +94,19 @@ enum PostHogExceptionProcessor {
exception["stacktrace"] = stacktrace
}

let exceptions = [exception]
attachExceptionsAndDebugImages(exceptions, to: &properties)

return properties
return buildProperties(exceptions: [exception])
}

// MARK: - Internal Exception Building

private static func buildProperties(exceptions: [[String: Any]]) -> [String: Any] {
var properties: [String: Any] = [
"$exception_level": "error", // TODO: figure this out from error wrapped type
]
attachExceptionsAndDebugImages(exceptions, to: &properties)
return properties
}

/// Build list of exceptions from NSException chain
///
/// Walks the NSException chain via NSUnderlyingErrorKey to capture all related exceptions.
Expand Down
Loading
Loading