Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better macos support #96

Merged
merged 4 commits into from
Jan 16, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- better macOS support with more event properties [#96](https://github.com/PostHog/posthog-ios/pull/96)

## 3.0.0-beta.3 - 2024-01-11

- Do not report dupe `Application Opened` event during the first time [#95](https://github.com/PostHog/posthog-ios/pull/95)
Expand Down
22 changes: 20 additions & 2 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation

#if os(iOS) || os(tvOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif

class PostHogContext {
Expand All @@ -35,13 +37,13 @@ class PostHogContext {
if Bundle.main.bundleIdentifier != nil {
properties["$app_namespace"] = Bundle.main.bundleIdentifier
}
properties["$device_manufacturer"] = "Apple"
properties["$device_model"] = platform()

#if os(iOS) || os(tvOS)
let device = UIDevice.current
// use https://github.com/devicekit/DeviceKit
properties["$device_model"] = platform()
properties["$device_name"] = device.model
properties["$device_manufacturer"] = "Apple"
properties["$os_name"] = device.systemName
properties["$os_version"] = device.systemVersion

Expand All @@ -63,6 +65,16 @@ class PostHogContext {
if deviceType != nil {
properties["$device_type"] = deviceType
}
#elseif os(macOS)
let deviceName = Host.current().localizedName
if (deviceName?.isEmpty) != nil {
properties["$device_name"] = deviceName
}
let processInfo = ProcessInfo.processInfo
properties["$os_name"] = "macOS \(processInfo.operatingSystemVersionString)" // eg Version 14.2.1 (Build 23C71)
let osVersion = processInfo.operatingSystemVersion
properties["$os_version"] = "\(osVersion.majorVersion).\(osVersion.minorVersion).\(osVersion.patchVersion)"
properties["$device_type"] = "Desktop"
#endif

return properties
Expand Down Expand Up @@ -94,6 +106,12 @@ class PostHogContext {
#if os(iOS) || os(tvOS)
properties["$screen_width"] = Float(UIScreen.main.bounds.width)
properties["$screen_height"] = Float(UIScreen.main.bounds.height)
#elseif os(macOS)
if let mainScreen = NSScreen.main {
let screenFrame = mainScreen.visibleFrame
properties["$screen_width"] = Float(screenFrame.size.width)
properties["$screen_height"] = Float(screenFrame.size.height)
}
#endif

properties["$lib"] = postHogSdkName
Expand Down
16 changes: 16 additions & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation

#if os(iOS) || os(tvOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif

let retryDelay = 5.0
Expand Down Expand Up @@ -652,6 +654,20 @@ let maxRetryDelay = 30.0
selector: #selector(captureAppOpened),
name: UIApplication.didBecomeActiveNotification,
object: nil)
#elseif os(macOS)
defaultCenter.addObserver(self,
selector: #selector(captureAppInstallLifecycle),
name: NSApplication.didFinishLaunchingNotification,
object: nil)
// macOS does not have didEnterBackgroundNotification, so we use didResignActiveNotification
defaultCenter.addObserver(self,
selector: #selector(captureAppBackgrounded),
name: NSApplication.didResignActiveNotification,
object: nil)
defaultCenter.addObserver(self,
selector: #selector(captureAppOpened),
name: NSApplication.didBecomeActiveNotification,
object: nil)
#endif
}

Expand Down