Skip to content

Commit

Permalink
chore: href screen name for recording (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Mar 25, 2024
1 parent 30d1eb7 commit 69c832b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 60 deletions.
26 changes: 17 additions & 9 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// swiftlint:disable cyclomatic_complexity

//
// PostHogReplayIntegration.swift
// PostHog
Expand Down Expand Up @@ -43,7 +45,7 @@
timer = nil
}

private func generateSnapshot(_ view: UIView) {
private func generateSnapshot(_ view: UIView, _ screenName: String? = nil) {
var hasChanges = false

let timestamp = Date().toMillis()
Expand All @@ -58,8 +60,12 @@
let width = Int(size.width)
let height = Int(size.height)

// TODO: set href
let data: [String: Any] = ["width": width, "height": height]
var data: [String: Any] = ["width": width, "height": height]

if screenName != nil {
data["href"] = screenName
}

let snapshotData: [String: Any] = ["type": 4, "data": data, "timestamp": timestamp]
PostHogSDK.shared.capture("$snapshot", properties: ["$snapshot_source": "mobile", "$snapshot_data": snapshotData])
snapshotStatus.sentMetaEvent = true
Expand Down Expand Up @@ -148,11 +154,9 @@
}
}

#if os(iOS)
if view is WKWebView {
wireframe.type = "web_view"
}
#endif
if view is WKWebView {
wireframe.type = "web_view"
}

if let progressView = view as? UIProgressView {
wireframe.type = "input"
Expand Down Expand Up @@ -214,15 +218,17 @@
return
}

var screenName: String?
if let controller = window.rootViewController {
if controller is AnyObjectUIHostingViewController {
hedgeLog("SwiftUI snapshot not supported.")
return
}
screenName = UIViewController.getViewControllerName(controller)
}

// TODO: offload conversion to off main thread
generateSnapshot(window)
generateSnapshot(window, screenName)
}
}

Expand All @@ -231,3 +237,5 @@
extension UIHostingController: AnyObjectUIHostingViewController {}

#endif

// swiftlint:enable cyclomatic_complexity
17 changes: 12 additions & 5 deletions PostHog/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ import Foundation
return nil
}

static func getViewControllerName(_ viewController: UIViewController) -> String {
var title = "Unknown"
title = String(describing: viewController.classForCoder).replacingOccurrences(of: "ViewController", with: "")

if title.count == 0 {
title = viewController.title ?? "Unknown"
}

return title
}

private func captureScreenView(_ window: UIWindow?) {
var rootController = window?.rootViewController
if rootController == nil {
rootController = activeController()
}
guard let top = findVisibleViewController(activeController()) else { return }

var name = String(describing: top.classForCoder).replacingOccurrences(of: "ViewController", with: "")

if name.count == 0 {
name = top.title ?? "Unknown"
}
let name = UIViewController.getViewControllerName(top)

if name != "Unknown" {
PostHogSDK.shared.screen(name)
Expand Down
2 changes: 1 addition & 1 deletion PostHogExampleStoryboard/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
apiKey: "phc_pQ70jJhZKHRvDIL5ruOErnPy6xiAiWCqlL4ayELj4X8"
)
// the ScreenViews for SwiftUI does not work, the names are not useful
config.captureScreenViews = false
config.captureScreenViews = true
config.captureApplicationLifecycleEvents = false
config.flushAt = 1
config.flushIntervalSeconds = 30
Expand Down
46 changes: 1 addition & 45 deletions PostHogExampleStoryboard/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,4 @@
import UIKit
import WebKit

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// let view = UITextView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
// view.text = "test"
// view.accessibilityIdentifier = "ph-no-capture"
// view.textContentType = .password

// let frame = CGRect(x: 50, y: 50, width: 200, height: 400)
// let view = WKWebView(frame: frame)
// let url = URL(string: "https://posthog.com")!
// view.load(URLRequest(url: url))

// let frame = CGRect(x: 50, y: 50, width: 200, height: 400)
// let view = UIProgressView(frame: frame)
// view.setProgress(0.5, animated: false)
// view.progressViewStyle = .default

// let view = UIActivityIndicatorView(style: .medium)
// view.color = .red
// view.startAnimating()

// let view = UILabel(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
// view.text = "test"
// view.accessibilityIdentifier = "ph-no-capture"

// let view = UITextField(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
// view.text = "test"
// view.accessibilityIdentifier = "ph-no-capture"

let view = UIImageView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
let url = URL(string: "https://1.bp.blogspot.com/-hkNkoCjc5UA/T4JTlCjhhfI/AAAAAAAAB98/XxQwZ-QPkI8/s1600/Free+Google+Wallpapers+3.jpg")!
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data: data) {
DispatchQueue.main.async {
view.image = image
}
}
}

self.view.addSubview(view)
}
}
class ViewController: UIViewController {}

0 comments on commit 69c832b

Please sign in to comment.