Skip to content

Commit 0741945

Browse files
authored
Add macOS Lifecycle Support (#141)
* Added mac lifecycle support
1 parent d63f39a commit 0741945

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

Segment.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */; };
6060
9692724E25A4E5B7009B5298 /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692724D25A4E5B7009B5298 /* Startup.swift */; };
6161
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692726725A583A6009B5298 /* SegmentDestination.swift */; };
62+
96A9624E2810C6B80011DE54 /* macOSLifecycleEvents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A9624D2810C6B80011DE54 /* macOSLifecycleEvents.swift */; };
6263
96A9668927BC137F00078F8B /* iOSLifecycle_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A9668827BC137F00078F8B /* iOSLifecycle_Tests.swift */; };
6364
96C33A9C25880A5E00F3D538 /* SegmentLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* SegmentLog.swift */; };
6465
96C33AB1258961F500F3D538 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AB0258961F500F3D538 /* Settings.swift */; };
@@ -161,6 +162,7 @@
161162
967C40ED259A7311008EB0B6 /* HTTPClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClient.swift; sourceTree = "<group>"; };
162163
9692724D25A4E5B7009B5298 /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = "<group>"; };
163164
9692726725A583A6009B5298 /* SegmentDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentDestination.swift; sourceTree = "<group>"; };
165+
96A9624D2810C6B80011DE54 /* macOSLifecycleEvents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = macOSLifecycleEvents.swift; sourceTree = "<group>"; };
164166
96A9668827BC137F00078F8B /* iOSLifecycle_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSLifecycle_Tests.swift; sourceTree = "<group>"; };
165167
96C33A9B25880A5E00F3D538 /* SegmentLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLog.swift; sourceTree = "<group>"; };
166168
96C33AB0258961F500F3D538 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
@@ -337,6 +339,7 @@
337339
isa = PBXGroup;
338340
children = (
339341
46FE4C9625A3F35E003A7362 /* macOSLifecycleMonitor.swift */,
342+
96A9624D2810C6B80011DE54 /* macOSLifecycleEvents.swift */,
340343
);
341344
path = Mac;
342345
sourceTree = "<group>";
@@ -544,6 +547,7 @@
544547
46FE4CE025A53FAD003A7362 /* Storage.swift in Sources */,
545548
465879BA2686560C00180335 /* watchOSDelegation.swift in Sources */,
546549
46022764261E64A800A9E913 /* iOSLifecycleEvents.swift in Sources */,
550+
96A9624E2810C6B80011DE54 /* macOSLifecycleEvents.swift in Sources */,
547551
4621080C2605332D00EBC4A8 /* KeyPath.swift in Sources */,
548552
A31A16262576B6F200C9CDDF /* Timeline.swift in Sources */,
549553
96C33AB1258961F500F3D538 /* Settings.swift in Sources */,
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Sources/Segment/Startup.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ extension Analytics: Subscriber {
5353
plugins.append(watchOSLifecycleEvents())
5454
#endif
5555
#if os(macOS)
56-
// placeholder - need to build this
57-
//plugins.append(macOSLifecycleEvents())
56+
plugins.append(macOSLifecycleEvents())
5857
#endif
5958
#if os(Linux)
6059
// placeholder - not sure what this is yet

0 commit comments

Comments
 (0)