Unity Analytics making my app sluggish?

(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):

var options = new InitializationOptions().SetEnvironmentName("production");
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: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?

  • 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)

  • 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!

Digging a bit deeper, I found out that I can simply ignore the warning and turn off the UGS initialization:

@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”?

@realragnvaldr

  • What device/s are you testing this on?
  • Can you turn off the debug log?

This would most likely delay the sluggishness you experience.

Let me find out more for you.

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)

  • The warning can be ignored, as the services, including Analytics are optional. Developers who don’t want Analytics or Telemetry can ignore it.

  • 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.

  • 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.

  • 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.

1 Like

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

1 Like

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?

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.

It shouldn’t have any effect on IAP other than not being able to see the transaction events.

1 Like