From 510b6f068bb78c4bc5137c2dddc5b9546b9755f1 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Tue, 16 Jan 2024 12:40:56 +0100 Subject: [PATCH] chore: better macos support (#96) --- CHANGELOG.md | 2 ++ PostHog/PostHogContext.swift | 22 ++++++++++++++++++++-- PostHog/PostHogSDK.swift | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df1561ac..83c428196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/PostHog/PostHogContext.swift b/PostHog/PostHogContext.swift index 239b6c0c2..d9a2a8ed1 100644 --- a/PostHog/PostHogContext.swift +++ b/PostHog/PostHogContext.swift @@ -9,6 +9,8 @@ import Foundation #if os(iOS) || os(tvOS) import UIKit +#elseif os(macOS) + import AppKit #endif class PostHogContext { @@ -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 @@ -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 @@ -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 diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index 7654bf019..25273dd91 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -9,6 +9,8 @@ import Foundation #if os(iOS) || os(tvOS) import UIKit +#elseif os(macOS) + import AppKit #endif let retryDelay = 5.0 @@ -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 }