Skip to main content

Screen Tracking

Track screen views to understand which screens users visit in your app.

UIKit - Automatic Tracking

Enable automatic screen view tracking by starting the AutoTracker:

// In AppDelegate, after SDK initialization
AutoTracker.shared.start()

Once enabled, the SDK automatically logs a screen_view event every time a UIViewController's viewDidAppear(_:) is called.

Event properties:

  • Screen name (derived from view controller class name)
// When ProductDetailViewController appears
// → screen_view event logged with screen_name = "ProductDetailViewController"
class ProductDetailViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// SDK automatically logs screen_view event
}
}

SwiftUI - Manual Tracking

For SwiftUI views, use the .trackScreen() view modifier:

import SwiftUI
import B2MetricAnalyticsSDK

struct ContentView: View {
var body: some View {
Text("Welcome")
.padding()
.trackScreen("HomeScreen")
}
}

You can also provide a custom name dynamically:

struct ProductDetailView: View {
let productId: String

var body: some View {
VStack {
Text("Product Details")
}
.trackScreen("ProductDetail_\(productId)")
}
}