Skip to content

Commit

Permalink
feat: add support for uitextview autocapture (#195)
Browse files Browse the repository at this point in the history
* Fix indentation issue

* feat: add user interactions capture

* fix: fix typo

* fix: remove support for UITextField action message capture

* refactor: add dispatch once for method swizzling

* fix: remove support for UISlider to reduce noise

* feat: add support for UITextField and UISlider event capture

* style: remove return keyword for one line functions

* fix: fix typo

* feat: add support for text field gained/lost focus

* fix: fix lint

* fix: remove tag as textfield title

* refactor: refactor title and shouldTrack

* refactor: add a protocol to track control elements

* refactor: guard on action name

* feat: add support for uitextview capture

* fix: disable action method tracking support for UITextView

* fix: fix lint issue

* style: remove amp_ prefix from objc methods
  • Loading branch information
PouriaAmini authored Jul 16, 2024
1 parent c0522a7 commit 3810e2c
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions Sources/Amplitude/Plugins/iOS/UIKitUserInteractions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@ class UIKitUserInteractions {

private static let lock = NSLock()

private static let initializeSwizzle: () = {
swizzleSendAction()
private static let addNotificationObservers: () = {
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(didBeginEditing), name: UITextField.textDidBeginEditingNotification, object: nil)
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(didEndEditing), name: UITextField.textDidEndEditingNotification, object: nil)
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(didBeginEditing), name: UITextView.textDidBeginEditingNotification, object: nil)
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(didEndEditing), name: UITextView.textDidEndEditingNotification, object: nil)
}()

private static let initializeNotificationListeners: () = {
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(UIKitUserInteractions.amp_textFieldDidBeginEditing), name: UITextField.textDidBeginEditingNotification, object: nil)
NotificationCenter.default.addObserver(UIKitUserInteractions.self, selector: #selector(UIKitUserInteractions.amp_textFieldDidEndEditing), name: UITextField.textDidEndEditingNotification, object: nil)
}()

static func register(_ amplitude: Amplitude) {
lock.withLock {
amplitudeInstances.add(amplitude)
}
initializeSwizzle
initializeNotificationListeners
}

private static func swizzleSendAction() {
private static let swizzleSendAction: () = {
let applicationClass = UIApplication.self

let originalSelector = #selector(UIApplication.sendAction)
Expand All @@ -45,19 +35,27 @@ class UIKitUserInteractions {
originalSelector,
swizzledImp,
method_getTypeEncoding(swizzledMethod))
}()

static func register(_ amplitude: Amplitude) {
lock.withLock {
amplitudeInstances.add(amplitude)
}
swizzleSendAction
addNotificationObservers
}

@objc static func amp_textFieldDidBeginEditing(_ notification: NSNotification) {
guard let textField = notification.object as? UITextField else { return }
let userInteractionEvent = textField.eventFromData(with: "didBeginEditing")
@objc static func didBeginEditing(_ notification: NSNotification) {
guard let view = notification.object as? UIView else { return }
let userInteractionEvent = view.eventFromData(with: "didBeginEditing")
amplitudeInstances.allObjects.forEach {
$0.track(event: userInteractionEvent)
}
}

@objc static func amp_textFieldDidEndEditing(_ notification: NSNotification) {
guard let textField = notification.object as? UITextField else { return }
let userInteractionEvent = textField.eventFromData(with: "didEndEditing")
@objc static func didEndEditing(_ notification: NSNotification) {
guard let view = notification.object as? UIView else { return }
let userInteractionEvent = view.eventFromData(with: "didEndEditing")
amplitudeInstances.allObjects.forEach {
$0.track(event: userInteractionEvent)
}
Expand All @@ -68,13 +66,15 @@ extension UIApplication {
@objc func amp_sendAction(_ action: Selector, to target: Any?, from sender: Any?, for event: UIEvent?) -> Bool {
let sendActionResult = amp_sendAction(action, to: target, from: sender, for: event)

guard
sendActionResult,
guard sendActionResult,
let view = sender as? UIView,
view.amp_shouldTrack(action, for: event)
view.amp_shouldTrack(action, for: event),
let actionName = NSStringFromSelector(action)
.components(separatedBy: ":")
.first
else { return sendActionResult }

let userInteractionEvent = view.eventFromData(with: NSStringFromSelector(action).components(separatedBy: ":").first ?? "")
let userInteractionEvent = view.eventFromData(with: actionName)

UIKitUserInteractions.amplitudeInstances.allObjects.forEach {
$0.track(event: userInteractionEvent)
Expand Down Expand Up @@ -156,6 +156,10 @@ extension UITextField {
override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { false }
}

extension UITextView {
override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { false }
}

#if !os(tvOS)
extension UISlider {
override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool {
Expand Down

0 comments on commit 3810e2c

Please sign in to comment.