Firebase Analytics - logging screen_view not working in SwiftUI

Viewed 876

I have recently started looking into Firebase/Google Analytics. I am trying to use the following piece of code to log a screen_view event, but nothing happens in my console. I am using SwiftUI and the following code is run in an .onAppear clause.

Analytics.logEvent(AnalyticsEventScreenView,
                   parameters: [AnalyticsParameterScreenName: "Initial Load",
                                AnalyticsParameterScreenClass: InitialLoadView.self])

Is there something that I have misunderstood or is there a bug with SwiftUI and Firebase Analytics at the moment?

2 Answers

You can log a manual screen_view just like you would any other event in Google Analytics. You can also include the two optional parameters (i) screen_name and (ii) screen_class, as well as any custom event parameters you want to include. The two optional parameters take the place of the firebase_screen and firebase_screen_class event parameters that get passed into automatically collected screen_view events.

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated)

// After enough time has passed to make this screen view significant.
Analytics.logEvent(AnalyticsEventScreenView, parameters: [
    AnalyticsParameterScreenName: screenName!,
    AnalyticsParameterScreenClass: screenClass!,
    MyAppAnalyticsParameterFitnessCategory: category!
])
}

Please follow the link below for details: Manually tracking screen views in Google Analytics

import SwiftUI
import Firebase

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

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
Related