1

This is Strange, I used to do GoogleAnalytics for my Unity Projects because I had two major uses, one was to see the number of users on Screen and second for some events. I heard about Firebase and wanted to explore it, I was successful in initializing the sdk and logging different events, but now there is one major problem which I can't seem to get over. Apparently when I try to Log my Current Screen using:

Firebase.Analytics.FirebaseAnalytics.SetCurrentScreen ("MainActivity", "MainMenu");

and then read somewhere to use it like this:

FirebaseAnalytics.SetCurrentScreen ("MainActivity", "MainMenu");

both of these functions gave the same error when viewed in Monitor(ddms)

SetCurrentScreen must be called from Main Thread

Everytime I called the function this came up, I don't know why this is happening and can't find a solution for this anywhere.

The Function calling the Method is:

public void AnalyticsLogScreen() {
    // Log an event with a float.
    DebugLog("Logging a Screen");
    Firebase.Analytics.FirebaseAnalytics.SetCurrentScreen ("MainActivity", "MainMenu");

}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Hunain Usman
  • 2,088
  • 5
  • 23
  • 32
  • What is your Firebase plugin version? This was once a bug. Also, are you calling SetCurrentScreen from another Thread? – Programmer May 23 '17 at 13:06
  • I am on Unity 5.5.1f1, firebase is 3.0.3, am only calling a function on click that sets the screen – Hunain Usman May 23 '17 at 13:09
  • 3.0.3 seems to be the latest version. Maybe this bug came back again. To verify that, please post the code that calls that function and show where they are being called. Just add edit in your question with your code. Maybe someone can figure out exactly what you are doing wrong. – Programmer May 23 '17 at 13:13

2 Answers2

2

It appears that you are calling SetCurrentScreen from worker thread. You can check this by looking at the name of the thread making the call or comparing the TID from the logcat to the process PID. The main thread with have name "main" and TID that matched the app PID.

Unity SetCurrentScreen is wrapped on Java FirebaseAnalytics.setCurrentScreen() method . setCurrentScreen can only be called from the main thread.

The reason for this requirement is that Activities in Android can only be displayed displayed from the UI thread and allowing SetCurrentScreen from worker thread creates a race condition between the Activity displayed in the UI thread and the worker thread executing the call to setCurrentScreen. To avoid this race condition Firebase requires the call to setCurrentScreen to be made on the UI thread. If you still like to set the screen from worker thread you can just call runOnUiThread though this will create the race condition so some event can be attributed to the wrong screen or appear logged not on any screen.

djabi
  • 5,601
  • 18
  • 25
  • Thanks for ur answer, although i understand why this is happening, but the solution you mentioned is for android i believe, while my question was for Unity, is there any way i could do this in Unity? – Hunain Usman May 24 '17 at 05:15
  • After some research i was able to implement what u said using AndroidJavaClass object in Unity, now this has produced something new, now the error is "SetCurrentScreen cannot be called using same class and name" – Hunain Usman May 24 '17 at 06:45
  • This warning means that the screen class and name is already set to the values you are trying to set. If the class and name match the values you want you can safely ignore this warning, it no-op. If you want to start new screen with the same class and name you should firat set both to null and then back to the value you want. This will count this pair of class/name as additional screen opened by the user. – djabi May 24 '17 at 14:26
2

https://developers.google.com/tag-manager/android/v4/ua#send-screen-views this is container settings. After that you can log screen :

 Firebase.Analytics.FirebaseAnalytics.LogEvent("openScreen", "screenName", "main_screen");

Tag settings: enter image description here

Trigger :

enter image description here

Varible: enter image description here