将 Firebase Analytics 与 SwiftUI 结合使用

Using Firebase Analytics with SwiftUI

我最近在我的 SwiftUI 应用程序中安装了 Firebase Analytics。除了自动屏幕视图外,一切都很好。我是否有义务为我想跟踪的每个视图调用 AnalyticsEventScreenView?

是 - 由于 SwiftUI 视图的结构方式,Analytics 只能看到 NotifyingMulticolumnSplitViewController

您必须手动发送屏幕浏览量(如 stated in the docs),如下所示:

import SwiftUI
import Firebase

struct ContentView: View {
  var body: some View {
    NavigationView {
      List {
        NavigationLink(destination: OneView()) {
          Text("One")
        }
        NavigationLink(destination: TwoView()) {
          Text("Two")
        }
      }
      .navigationTitle("Choose a screen")
      .onAppear() {
        Analytics.logEvent(AnalyticsEventScreenView,
                           parameters: [AnalyticsParameterScreenName: "\(ContentView.self)",
                                        AnalyticsParameterScreenClass: "\(ContentView.self)"])
      }
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
import SwiftUI
import Firebase

struct OneView: View {
  var body: some View {
    Text("1")
      .onAppear() {
        Analytics.logEvent(AnalyticsEventScreenView,
                           parameters: [AnalyticsParameterScreenName: "\(OneView.self)",
                                        AnalyticsParameterScreenClass: "\(OneView.self)"])
      }
  }
}

struct OneView_Previews: PreviewProvider {
  static var previews: some View {
    OneView()
  }
}