|
| 1 | +// |
| 2 | +// macOSLifecycleEvents.swift |
| 3 | +// Segment |
| 4 | +// |
| 5 | +// Created by Cody on 4/20/22. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +#if os(macOS) |
| 11 | + |
| 12 | +import Cocoa |
| 13 | + |
| 14 | +class macOSLifecycleEvents: PlatformPlugin, macOSLifecycle { |
| 15 | + static var versionKey = "SEGVersionKey" |
| 16 | + static var buildKey = "SEGBuildKeyV2" |
| 17 | + |
| 18 | + let type = PluginType.before |
| 19 | + var analytics: Analytics? |
| 20 | + |
| 21 | + /// Since application:didFinishLaunchingWithOptions is not automatically called with Scenes / SwiftUI, |
| 22 | + /// this gets around by using a flag in user defaults to check for big events like application updating, |
| 23 | + /// being installed or even opening. |
| 24 | + @Atomic |
| 25 | + private var didFinishLaunching = false |
| 26 | + |
| 27 | + func application(didFinishLaunchingWithOptions launchOptions: [String : Any]?) { |
| 28 | + // Make sure we aren't double calling application:didFinishLaunchingWithOptions |
| 29 | + // by resetting the check at the start |
| 30 | + didFinishLaunching = true |
| 31 | + |
| 32 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + let previousVersion = UserDefaults.standard.string(forKey: Self.versionKey) |
| 37 | + let previousBuild = UserDefaults.standard.string(forKey: Self.buildKey) |
| 38 | + |
| 39 | + let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String |
| 40 | + let currentBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String |
| 41 | + |
| 42 | + if previousBuild == nil { |
| 43 | + analytics?.track(name: "Application Installed", properties: [ |
| 44 | + "version": currentVersion ?? "", |
| 45 | + "build": currentBuild ?? "" |
| 46 | + ]) |
| 47 | + } else if currentBuild != previousBuild { |
| 48 | + analytics?.track(name: "Application Updated", properties: [ |
| 49 | + "previous_version": previousVersion ?? "", |
| 50 | + "previous_build": previousBuild ?? "", |
| 51 | + "version": currentVersion ?? "", |
| 52 | + "build": currentBuild ?? "" |
| 53 | + ]) |
| 54 | + } |
| 55 | + |
| 56 | + analytics?.track(name: "Application Opened", properties: [ |
| 57 | + "from_background": false, |
| 58 | + "version": currentVersion ?? "", |
| 59 | + "build": currentBuild ?? "" |
| 60 | + ]) |
| 61 | + |
| 62 | + UserDefaults.standard.setValue(currentVersion, forKey: Self.versionKey) |
| 63 | + UserDefaults.standard.setValue(currentBuild, forKey: Self.buildKey) |
| 64 | + } |
| 65 | + |
| 66 | + func applicationDidUnhide() { |
| 67 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String |
| 72 | + let currentBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String |
| 73 | + |
| 74 | + analytics?.track(name: "Application Unhidden", properties: [ |
| 75 | + "from_background": true, |
| 76 | + "version": currentVersion ?? "", |
| 77 | + "build": currentBuild ?? "" |
| 78 | + ]) |
| 79 | + } |
| 80 | + |
| 81 | + func applicationDidHide() { |
| 82 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + analytics?.track(name: "Application Hidden") |
| 87 | + } |
| 88 | + func applicationDidResignActive() { |
| 89 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + analytics?.track(name: "Application Backgrounded") |
| 94 | + } |
| 95 | + |
| 96 | + func applicationDidBecomeActive() { |
| 97 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Lets check if we skipped application:didFinishLaunchingWithOptions, |
| 102 | + // if so, lets call it. |
| 103 | + if didFinishLaunching == false { |
| 104 | + // Call application did finish launching |
| 105 | + self.application(didFinishLaunchingWithOptions: nil) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + func applicationWillTerminate() { |
| 110 | + if analytics?.configuration.values.trackApplicationLifecycleEvents == false { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + analytics?.track(name: "Application Terminated") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#endif |
0 commit comments