Skip to content

Commit

Permalink
chore: better macos support (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Jan 16, 2024
1 parent a7b1084 commit 510b6f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
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

0 comments on commit 510b6f0

Please sign in to comment.