forked from segmentio/analytics-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIKitScreenTracking.swift
169 lines (150 loc) · 6.97 KB
/
UIKitScreenTracking.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// UIKitScreenTracking.swift
// SegmentUIKitExample
//
// Created by Brandon Sneed on 4/13/21.
//
// NOTE: You can see this plugin in use in the SwiftUIKitExample application.
//
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
// and for your convenience should you find it useful.
// MIT License
//
// Copyright (c) 2021 Segment
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
import Segment
import UIKit
/**
Example plugin to replicate automatic screen tracking in iOS.
*/
// Conform to this protocol if self-tracking screens are desired
@objc protocol UIKitScreenTrackable: NSObjectProtocol {
@objc func seg__trackScreen(name: String?)
}
class UIKitScreenTracking: UtilityPlugin {
static let notificationName = Notification.Name(rawValue: "UIKitScreenTrackNotification")
static let screenNameKey = "name"
static let controllerKey = "controller"
let type = PluginType.utility
var analytics: Analytics? = nil
init() {
setupUIKitHooks()
}
internal func setupUIKitHooks() {
swizzle(forClass: UIViewController.self,
original: #selector(UIViewController.viewDidAppear(_:)),
new: #selector(UIViewController.seg__viewDidAppear)
)
swizzle(forClass: UIViewController.self,
original: #selector(UIViewController.viewDidDisappear(_:)),
new: #selector(UIViewController.seg__viewDidDisappear)
)
NotificationCenter.default.addObserver(forName: Self.notificationName, object: nil, queue: OperationQueue.main) { notification in
let name = notification.userInfo?[Self.screenNameKey] as? String
if let controller = notification.userInfo?[Self.controllerKey] as? UIKitScreenTrackable {
// if the controller conforms to UIKitScreenTrackable,
// call the trackScreen method with the name we have (possibly even nil)
// and the implementor will decide what to do.
controller.seg__trackScreen(name: name)
} else if let name = name {
// if we have a name, call screen
self.analytics?.screen(title: name)
}
}
}
}
extension UIKitScreenTracking {
private func swizzle(forClass: AnyClass, original: Selector, new: Selector) {
guard let originalMethod = class_getInstanceMethod(forClass, original) else { return }
guard let swizzledMethod = class_getInstanceMethod(forClass, new) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
extension UIViewController {
internal func activeController() -> UIViewController? {
// if a view is being dismissed, this will return nil
if let root = viewIfLoaded?.window?.rootViewController {
return root
} else if #available(iOS 13.0, *) {
// preferred way to get active controller in ios 13+
for scene in UIApplication.shared.connectedScenes {
if scene.activationState == .foregroundActive {
let windowScene = scene as? UIWindowScene
let sceneDelegate = windowScene?.delegate as? UIWindowSceneDelegate
if let target = sceneDelegate, let window = target.window {
return window?.rootViewController
}
}
}
} else {
// this was deprecated in ios 13.0
return UIApplication.shared.keyWindow?.rootViewController
}
return nil
}
internal func captureScreen() {
var rootController = viewIfLoaded?.window?.rootViewController
if rootController == nil {
rootController = activeController()
}
guard let top = Self.seg__visibleViewController(activeController()) else { return }
var name = String(describing: top.self.classForCoder).replacingOccurrences(of: "ViewController", with: "")
print("Auto-tracking Screen: \(name)")
// name could've been just "ViewController"...
if name.count == 0 {
name = top.title ?? "Unknown"
}
// post a notification that our plugin can capture.
// if you were to do a custom implementation of how the name should
// appear, you probably want to inspect the viewcontroller itself, `top` in this
// case to generate your string for name and/or category.
NotificationCenter.default.post(name: UIKitScreenTracking.notificationName,
object: self,
userInfo: [UIKitScreenTracking.screenNameKey: name,
UIKitScreenTracking.controllerKey: top])
}
@objc internal func seg__viewDidAppear(animated: Bool) {
captureScreen()
// it looks like we're calling ourselves, but we're actually
// calling the original implementation of viewDidAppear since it's been swizzled.
seg__viewDidAppear(animated: animated)
}
@objc internal func seg__viewDidDisappear(animated: Bool) {
// call the original method first
seg__viewDidDisappear(animated: animated)
// the VC should be gone from the stack now, so capture where we're at now.
captureScreen()
}
static func seg__visibleViewController(_ controller: UIViewController?) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return seg__visibleViewController(navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return seg__visibleViewController(selected)
}
}
if let presented = controller?.presentedViewController {
return seg__visibleViewController(presented)
}
return controller
}
}