Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Unity Analytics making my app sluggish?

Discussion in 'Unity Analytics' started by realragnvaldr, Nov 14, 2022.

  1. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    41
    (Unity version 2021.3.2f1 with Purchasing Package 4.5.1)

    I recently started noticing a warning

    "Unity In-App Purchasing requires Unity Gaming Services to have been initialized before use."

    ...in the console, so I decided to add the following two lines of code at the start of the app (as per the documentation):

    Code (CSharp):
    1. var options = new InitializationOptions().SetEnvironmentName("production");
    2. await UnityServices.InitializeAsync(options);
    After this change, I noticed that my app got slightly sluggish once every minute or so (e.g., when scrolling a list, it feels like the frame rate drops to single digits for 2-3 sec -- nothing major, but aesthetically quite annoying, especially since it happens so often).

    Investigating the issue, it turned out that the sluggishness always co-occurs with this debug message (in intervals of exactly 60 sec):

    Flushed up to token index 34368, cache file is 981155B
    Unity.Services.Analytics.Internal.Buffer:FlushToDisk()
    Unity.Services.Analytics.Internal.<>c__DisplayClass13_0:<FlushBufferToService>b__1(Int64)


    Hence, it looks like UnityServices schedules a cache flushing process every minute, during which my app becomes temporarily sluggish.

    The thing is that I'm not even actively using Unity Analytics, but from what I understand, Google Admob requires In-App Purchases and In-App Purchases requires Unity Analytics.

    Could any of the following be a functional solution?
    1. Wait with the above initialization until a user goes to the purchase screen (this would solve it for >99% of the app sessions, as purchases are rare)
    2. Initialize only the IAP-related Unity Services and skip all other services that supposedly get initialized - sadly, I couldn't find any documentation on the configurability of the 'options' in the above code
    Any other thoughts and suggestions are of course also very welcome. Thanks!
     
    Last edited: Nov 15, 2022
  2. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    41
    Digging a bit deeper, I found out that I can simply ignore the warning and turn off the UGS initialization:

    https://forum.unity.com/threads/faq-for-iap-4-2-and-adoption-of-ugs-analytics.1327011/

    @John_Corbett : Can you elaborate a bit on why the warning is there? If I turn off UGS initialization, does it mean that while IAP works for now, it might suddenly stop working in the future? Or is the warning inaccurate and should it instead read "Warning: Collecting analytics for Unity In-App Purchasing requires Unity Gaming Services to have been initialized before use"?
     
    Last edited: Nov 16, 2022
  3. Julian-Unity3D

    Julian-Unity3D

    Unity Technologies

    Joined:
    Apr 28, 2022
    Posts:
    192
    @realragnvaldr
    1. What device/s are you testing this on?
    2. Can you turn off the debug log?
    This would most likely delay the sluggishness you experience.

    Let me find out more for you.
     
  4. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    41
    Hi @Julian-Unity3D!

    1. I'm testing on Android Galaxy phones (one from 2017, one from 2019 - both have the same issue)
    2. I don't think so - what I was referring to is the internal Android log, viewed with logcat; but just to be clear: the logging of the error is not what causes the sluggishness; it is the cache flushing that seems to do it

    I have now removed Unity Gaming Services from my app, because it turns out that they are not needed after all (see my message from yesterday). That solved it. (But i would of course still be very much interested in hearing other solutions)
     
  5. Julian-Unity3D

    Julian-Unity3D

    Unity Technologies

    Joined:
    Apr 28, 2022
    Posts:
    192
    1. The warning can be ignored, as the services, including Analytics are optional. Developers who don't want Analytics or Telemetry can ignore it.

    2. The InitializeAsync call will initialize all services, the Core Services doesn't allow a pick-and choose. This means a developer initializing the services for something else, like Remote Config, will have this problem anyway.

    3. Yes, developers who only want the services for IAP can delay the call as long as they want, as you suggested. It's possible there'll be a bunch of Telemetry events that get dumped at once.

    4. As an alternative in your use-case, you can use
      AnalyticsService.Instance.SetAnalyticsEnabled(false
      ) after initialisation to disable the Analytics SDK. This will stop it from recording events or attempting to upload them, so you won't suffer the performance impact.
     
    Tural-Muslim likes this.
  6. realragnvaldr

    realragnvaldr

    Joined:
    Jul 21, 2019
    Posts:
    41
    Thanks a lot, Julian! Right now I completely turned it off, but for the next update i'll switch to your suggestion #4, because that seems a tiny little bit safer in case IAP *will* require UGS initialization in a future version
     
    Julian-Unity3D likes this.
  7. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,840
    This doesn't seem to resolve OPs original problem though regarding the sluggishness. I have recently implemented the latest analytics 4.2.0 and see this flush message in the editor log quite often.

    How do we prevent tthis flush operation from causing lag spikes in our apps?
     
  8. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    I'm seeing this
    Flushed up to token index
    message being logged a lot too in some of my bug reports.

    Not a great user experience if the game suffers freezing every minute because of this, especially if this is default behaviour...?

    @Julian-Unity3D
    Will this affect IAP functionality at all? I don't need Unity's analytics / statistics; I just want the IAP system to continue functioning.
     
    Last edited: Dec 2, 2022
  9. Julian-Unity3D

    Julian-Unity3D

    Unity Technologies

    Joined:
    Apr 28, 2022
    Posts:
    192
    It shouldn't have any effect on IAP other than not being able to see the transaction events.
     
    -chris likes this.