44

In version 6.29.0 of Firebase Analytics the method

class func setScreenName(_ screenName: String?, screenClass screenClassOverride: String?)

is deprecated. The hint given is to Use +[FIRAnalytics logEventWithName:kFIREventScreenView parameters:] instead.

My question is what is the parameter for screenName? How do I rewrite my helper method:

import FirebaseAnalytics
func setScreenName(_ screenName: String) {
    Analytics.setScreenName(screenName, screenClass: nil)
}
Rool Paap
  • 1,918
  • 4
  • 32
  • 39

5 Answers5

78

This is the code to rewrite the deprecated method:

Replace this line:

Analytics.setScreenName(screenName, screenClass: nil)

With this line:

Analytics.logEvent(AnalyticsEventScreenView, parameters: [AnalyticsParameterScreenName: screenName])
Laura Franco
  • 904
  • 7
  • 5
  • Ah, I missed the AnalyticsParameterScreenName part. Thanks! – Rool Paap Jul 30 '20 at 10:03
  • Hi, sorry to ask this question but since you manually log the event (in your answer), isn't there a problem of data duplication due to Firebase automatic event collect. Screen views won't be logged twice? – Julien Le Thuaut Nov 18 '20 at 11:15
  • @Laura, I have tried this but I am getting following error, Can you help us https://stackoverflow.com/questions/65843488/use-of-unresolved-identifier-analyticseventscreenview-in-swift-firebase-analyt – Anilkumar iOS - ReactNative Jan 22 '21 at 11:29
  • Hi @Laura I tried your suggestions, But I am getting following errors Use of unresolved identifier 'AnalyticsEventScreenView' Use of unresolved identifier 'AnalyticsParameterScreenName' Even I am using latest version of analytics from firebase using pods, Any suggestions? – Anilkumar iOS - ReactNative Jan 24 '21 at 05:50
  • Which file do we replace this in? – Harsh Phoujdar Jul 25 '21 at 10:57
  • @MavBzh you should place it in viewDidAppear, if you don't, Firebase (used to) automatically send it, with a "not set" screen name. Perhaps they changed it recently – Kowboj Apr 25 '22 at 13:47
  • @AnilkumariOS-ReactNative you probably need to import FirebaseAnalytics in this file, or use "kFIRParameterScreenName". – Kowboj Apr 25 '22 at 13:48
30

I did it in this way:

Deprecated code

Analytics.setScreenName(name, screenClass: className)

new code

Analytics.logEvent(AnalyticsEventScreenView, parameters: [AnalyticsParameterScreenName: name,
                                                          AnalyticsParameterScreenClass: className])
RunesReader
  • 7,137
  • 1
  • 14
  • 12
  • I tried this but, for me showing following error Use of unresolved identifier 'AnalyticsEventScreenView' Use of unresolved identifier 'AnalyticsParameterScreenName' Any Suggestions? – Anilkumar iOS - ReactNative Jan 24 '21 at 05:43
23

Just in case it can help someone, here is the Objective-C version:

[FIRAnalytics logEventWithName:kFIREventScreenView parameters: @{kFIRParameterScreenName: screenName}];
JonyMateos
  • 663
  • 6
  • 10
15

Here is the Android version:

Java

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName);
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, screenClass);
bundle.putString(MyAppAnalyticsConstants.Param.TOPIC, topic);
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);

Kotlin

val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS,classname)
firebase.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
F. Müller
  • 3,969
  • 8
  • 38
  • 49
georkost
  • 588
  • 6
  • 12
3
extension Analytics
{
    static func setScreenName(_ screenName:String, screenClass:String)
    {
        Analytics.logEvent(AnalyticsEventScreenView, parameters: [AnalyticsParameterScreenName: screenName,AnalyticsParameterScreenClass:screenClass])
    }
}

Simple add this extension in your project no need to change anything after this

Divyesh Gondaliya
  • 884
  • 11
  • 21