Skip to content

Commit d28a2c9

Browse files
bsneedBrandon Sneed
andauthored
Example of auto notification tracking (#63)
* Example of auto notification tracking * Fixed incorrect method sigs for ios lifecycle plugin * Added comment explaining UIApplication optionals in method sigs Co-authored-by: Brandon Sneed <[email protected]>
1 parent a208e98 commit d28a2c9

File tree

9 files changed

+134
-4
lines changed

9 files changed

+134
-4
lines changed

Examples/apps/MacExample/MacExample.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
4663C73C267A926C00ADDD1A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4663C73B267A926C00ADDD1A /* Assets.xcassets */; };
1313
4663C73F267A926C00ADDD1A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4663C73D267A926C00ADDD1A /* Main.storyboard */; };
1414
4663C749267A92E100ADDD1A /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 4663C748267A92E100ADDD1A /* Segment */; };
15+
46E73DA926F53C570021042C /* NotificationTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E73DA826F53C570021042C /* NotificationTracking.swift */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXFileReference section */
@@ -22,6 +23,7 @@
2223
4663C73E267A926C00ADDD1A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2324
4663C740267A926C00ADDD1A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2425
4663C741267A926C00ADDD1A /* MacExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MacExample.entitlements; sourceTree = "<group>"; };
26+
46E73DA826F53C570021042C /* NotificationTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NotificationTracking.swift; path = ../../other_plugins/NotificationTracking.swift; sourceTree = "<group>"; };
2527
/* End PBXFileReference section */
2628

2729
/* Begin PBXFrameworksBuildPhase section */
@@ -39,6 +41,7 @@
3941
4663C72B267A926B00ADDD1A = {
4042
isa = PBXGroup;
4143
children = (
44+
46E73DA726F53C3C0021042C /* Plugins */,
4245
4663C736267A926B00ADDD1A /* MacExample */,
4346
4663C735267A926B00ADDD1A /* Products */,
4447
4663C747267A92E100ADDD1A /* Frameworks */,
@@ -73,6 +76,14 @@
7376
name = Frameworks;
7477
sourceTree = "<group>";
7578
};
79+
46E73DA726F53C3C0021042C /* Plugins */ = {
80+
isa = PBXGroup;
81+
children = (
82+
46E73DA826F53C570021042C /* NotificationTracking.swift */,
83+
);
84+
name = Plugins;
85+
sourceTree = "<group>";
86+
};
7687
/* End PBXGroup section */
7788

7889
/* Begin PBXNativeTarget section */
@@ -147,6 +158,7 @@
147158
files = (
148159
4663C73A267A926B00ADDD1A /* ViewController.swift in Sources */,
149160
4663C738267A926B00ADDD1A /* AppDelegate.swift in Sources */,
161+
46E73DA926F53C570021042C /* NotificationTracking.swift in Sources */,
150162
);
151163
runOnlyForDeploymentPostprocessing = 0;
152164
};

Examples/apps/MacExample/MacExample/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2222
.flushInterval(10)
2323

2424
analytics = Analytics(configuration: configuration)
25-
25+
//analytics?.add(plugin: NotificationTracking())
2626
}
2727

2828
func applicationWillTerminate(_ aNotification: Notification) {

Examples/apps/SegmentUIKitExample/SegmentUIKitExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
46E3835326582DA400BA2502 /* CustomScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835126582DA400BA2502 /* CustomScreenTracking.swift */; };
2121
46E3835426582DA400BA2502 /* MultiInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835226582DA400BA2502 /* MultiInstance.swift */; };
2222
46E383572658307800BA2502 /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 46E383562658307800BA2502 /* Segment */; };
23+
46E73DA326F531320021042C /* NotificationTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E73DA226F531320021042C /* NotificationTracking.swift */; };
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXFileReference section */
@@ -39,6 +40,7 @@
3940
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
4041
46E3835126582DA400BA2502 /* CustomScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomScreenTracking.swift; sourceTree = "<group>"; };
4142
46E3835226582DA400BA2502 /* MultiInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultiInstance.swift; sourceTree = "<group>"; };
43+
46E73DA226F531320021042C /* NotificationTracking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTracking.swift; sourceTree = "<group>"; };
4244
/* End PBXFileReference section */
4345

4446
/* Begin PBXFrameworksBuildPhase section */
@@ -92,6 +94,7 @@
9294
isa = PBXGroup;
9395
children = (
9496
46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */,
97+
46E73DA226F531320021042C /* NotificationTracking.swift */,
9598
46E3834926582D9E00BA2502 /* ConsentTracking.swift */,
9699
46E3834A26582D9E00BA2502 /* IDFACollection.swift */,
97100
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */,
@@ -200,6 +203,7 @@
200203
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */,
201204
46E3835426582DA400BA2502 /* MultiInstance.swift in Sources */,
202205
46E3834E26582D9E00BA2502 /* IDFACollection.swift in Sources */,
206+
46E73DA326F531320021042C /* NotificationTracking.swift in Sources */,
203207
46022787261F860100A9E913 /* SceneDelegate.swift in Sources */,
204208
46E3834D26582D9E00BA2502 /* ConsentTracking.swift in Sources */,
205209
);

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717
Analytics.main.add(plugin: ConsentTracking())
1818
Analytics.main.add(plugin: IDFACollection())
1919
Analytics.main.add(plugin: UIKitScreenTracking())
20+
Analytics.main.add(plugin: NotificationTracking())
2021

2122
Analytics.support.add(plugin: ConsoleLogger(name: "support"))
2223
Analytics.support.add(plugin: ConsentTracking())

Examples/apps/watchOSExample/watchOSExample WatchKit Extension/ExtensionDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate {
1919

2020
analytics = Analytics(configuration: configuration)
2121
analytics?.add(plugin: ConsoleLogger(name: "consoleLogger"))
22+
analytics?.add(plugin: NotificationTracking())
2223
}
2324

2425
func applicationDidBecomeActive() {

Examples/apps/watchOSExample/watchOSExample.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
469ECD672684F9090028BE9A /* ComplicationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 469ECD662684F9090028BE9A /* ComplicationController.swift */; };
2020
469ECD692684F90A0028BE9A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 469ECD682684F90A0028BE9A /* Assets.xcassets */; };
2121
469ECD7B2684FD710028BE9A /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 469ECD7A2684FD710028BE9A /* Segment */; };
22+
46E73DA626F5389E0021042C /* NotificationTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E73DA526F5389E0021042C /* NotificationTracking.swift */; };
2223
/* End PBXBuildFile section */
2324

2425
/* Begin PBXContainerItemProxy section */
@@ -79,6 +80,7 @@
7980
469ECD682684F90A0028BE9A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
8081
469ECD6A2684F90A0028BE9A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8182
469ECD6B2684F90A0028BE9A /* PushNotificationPayload.apns */ = {isa = PBXFileReference; lastKnownFileType = text; path = PushNotificationPayload.apns; sourceTree = "<group>"; };
83+
46E73DA526F5389E0021042C /* NotificationTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NotificationTracking.swift; path = ../../other_plugins/NotificationTracking.swift; sourceTree = "<group>"; };
8284
/* End PBXFileReference section */
8385

8486
/* Begin PBXFrameworksBuildPhase section */
@@ -96,6 +98,7 @@
9698
469ECD412684F9080028BE9A = {
9799
isa = PBXGroup;
98100
children = (
101+
46E73DA426F538940021042C /* Plugins */,
99102
469ECD502684F9080028BE9A /* watchOSExample WatchKit App */,
100103
469ECD5F2684F9090028BE9A /* watchOSExample WatchKit Extension */,
101104
469ECD492684F9080028BE9A /* Products */,
@@ -146,6 +149,14 @@
146149
name = Frameworks;
147150
sourceTree = "<group>";
148151
};
152+
46E73DA426F538940021042C /* Plugins */ = {
153+
isa = PBXGroup;
154+
children = (
155+
46E73DA526F5389E0021042C /* NotificationTracking.swift */,
156+
);
157+
name = Plugins;
158+
sourceTree = "<group>";
159+
};
149160
/* End PBXGroup section */
150161

151162
/* Begin PBXNativeTarget section */
@@ -279,6 +290,7 @@
279290
465879B4268641B900180335 /* SomeScreenController.swift in Sources */,
280291
469ECD672684F9090028BE9A /* ComplicationController.swift in Sources */,
281292
469ECD632684F9090028BE9A /* ExtensionDelegate.swift in Sources */,
293+
46E73DA626F5389E0021042C /* NotificationTracking.swift in Sources */,
282294
465879B22685058800180335 /* ConsoleLogger.swift in Sources */,
283295
469ECD612684F9090028BE9A /* InterfaceController.swift in Sources */,
284296
);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// NotificationTracking.swift
3+
//
4+
// Created by Brandon Sneed on 9/17/21.
5+
//
6+
7+
// NOTE: You can see this plugin in use in the SwiftUIKitExample application.
8+
//
9+
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
10+
// and for your convenience should you find it useful.
11+
12+
// MIT License
13+
//
14+
// Copyright (c) 2021 Segment
15+
//
16+
// Permission is hereby granted, free of charge, to any person obtaining a copy
17+
// of this software and associated documentation files (the "Software"), to deal
18+
// in the Software without restriction, including without limitation the rights
19+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
// copies of the Software, and to permit persons to whom the Software is
21+
// furnished to do so, subject to the following conditions:
22+
//
23+
// The above copyright notice and this permission notice shall be included in all
24+
// copies or substantial portions of the Software.
25+
//
26+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
// SOFTWARE.
33+
34+
// MARK: Common
35+
36+
#if !os(Linux) && !os(macOS)
37+
38+
import Foundation
39+
import Segment
40+
41+
class NotificationTracking: Plugin {
42+
var type: PluginType = .utility
43+
var analytics: Analytics?
44+
45+
func trackNotification(_ properties: [String: Any], fromLaunch launch: Bool) {
46+
if launch {
47+
analytics?.track(name: "Push Notification Tapped", properties: properties)
48+
} else {
49+
analytics?.track(name: "Push Notification Received", properties: properties)
50+
}
51+
}
52+
}
53+
54+
// NOTE: watchOS doesn't have the concept of launch options for making a
55+
// determination if a push notification caused the app to open.
56+
extension NotificationTracking: RemoteNotifications {
57+
func receivedRemoteNotification(userInfo: [AnyHashable: Any]) {
58+
if let notification = userInfo as? [String: Any] {
59+
trackNotification(notification, fromLaunch: false)
60+
}
61+
}
62+
}
63+
64+
#endif
65+
66+
// MARK: macOS -- TODO: Full lifecycle/delegation options in library.
67+
/*
68+
#if os(macOS)
69+
70+
import Cocoa
71+
72+
extension NotificationTracking: macOSLifecycle {
73+
func application(didFinishLaunchingWithOptions launchOptions: [String: Any]?) {
74+
if let notification = launchOptions?[NSApplication.launchUserNotificationUserInfoKey] as? [String: Any] {
75+
trackNotification(notification, fromLaunch: true)
76+
}
77+
}
78+
}
79+
80+
#endif
81+
*/
82+
83+
// MARK: iOS/tvOS/Catalyst
84+
85+
#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst)
86+
87+
import UIKit
88+
89+
extension NotificationTracking: iOSLifecycle {
90+
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
91+
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any] {
92+
trackNotification(notification, fromLaunch: true)
93+
}
94+
}
95+
}
96+
97+
#endif
98+

Sources/Segment/Plugins/Platforms/iOS/iOSLifecycleEvents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
1818
let type = PluginType.before
1919
var analytics: Analytics?
2020

21-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
21+
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
2222
if analytics?.configuration.values.trackApplicationLifecycleEvents == false {
2323
return
2424
}
@@ -55,7 +55,7 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
5555
UserDefaults.standard.setValue(currentBuild, forKey: Self.buildKey)
5656
}
5757

58-
func applicationWillEnterForeground(application: UIApplication) {
58+
func applicationWillEnterForeground(application: UIApplication?) {
5959
if analytics?.configuration.values.trackApplicationLifecycleEvents == false {
6060
return
6161
}
@@ -70,7 +70,7 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
7070
])
7171
}
7272

73-
func applicationDidEnterBackground(application: UIApplication) {
73+
func applicationDidEnterBackground(application: UIApplication?) {
7474
if analytics?.configuration.values.trackApplicationLifecycleEvents == false {
7575
return
7676
}

Sources/Segment/Plugins/Platforms/iOS/iOSLifecycleMonitor.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import Foundation
1111
import UIKit
1212

13+
// NOTE: These method signatures are marked optional as application extensions may not have
14+
// a UIApplication object available. See `safeShared` below.
1315
public protocol iOSLifecycle {
1416
func applicationDidEnterBackground(application: UIApplication?)
1517
func applicationWillEnterForeground(application: UIApplication?)

0 commit comments

Comments
 (0)