Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Firebase Analytics with SwiftUI

I recently installed Firebase Analytics in my SwiftUI app. Everything works great except the automatic screen views. Am I obligated to call AnalyticsEventScreenView for every view I would like to track?

like image 909
Dom Avatar asked May 07 '26 14:05

Dom


1 Answers

Firebase 9.0.0 has introduced new APIs that are specific to SwiftUI and have exited beta. There is now a View extension that implements a ViewModifier that provides you some functionality:

func analyticsScreen(name: String, 
                     class: String = "View", 
                     extraParameters: [String: Any] = [:])

Analytics+SwiftUI.swift

For example:

struct MyView: View {
  var body: some View {
    Text("Hey")
       .analyticsScreen(
           name: "Screen Name", 
           extraParameters: [
              AnalyticsParameterScreenName: "\(type(of: self))",
              AnalyticsParameterScreenClass: "\(type(of: self))"
           ]
       )  
   }
}
like image 131
cohen72 Avatar answered May 09 '26 14:05

cohen72



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!