Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Unity 2021.3.14f1 Stop Analytics Service from initializing when unused?

Discussion in 'Unity Analytics' started by monstronauts, Nov 22, 2022.

  1. monstronauts


    Feb 15, 2016
    Hoping to find a way to stop some automatic start-up behavior by Unity Analytics service.

    We're on Unity 2021.3.14f1 and have Unity IAP, which requires the new Unity Analytics service package.

    We're not using this service, but it keeps spawning an error in console during early development.

    "No cloud project ID was found by the Analytics SDK. This means Analytics events will not be sent. Please make sure to link your cloud project in the Unity editor to fix this problem."

    We do AnalyticsService.Instance.SetAnalyticsEnabled(false) in [RuntimeInitializeOnLoadMethod], but AnalyticsService.Instance will lead to a call to AnalyticsServiceInstance(), which spawns that error message.

    Also, AnalyticsLifetime.cs static ContainerObject still creates a DontDestroyOnLoad GameObject named AnalyticsContainer (with intended AnalyticsLifetime component), which also calls AnalyticsService.internalInstance during Awake().

    Any call to AnalyticsService will also create the internal instance which will print out this error message.
    Code (CSharp):
    1.     public static class AnalyticsService
    2.     {
    3.         internal static AnalyticsServiceInstance internalInstance = new AnalyticsServiceInstance();
    5.         public static IAnalyticsService Instance => internalInstance;
    6.     }
    AnalyticsLifetime's Awake() also incurs a FindObjectsOfType<MonoBehaviour> call on the first scene (with an interesting comment), and all the while we're not using the Analytics service.
    Code (CSharp):
    1.         void Awake()
    2.         {
    3.             Instance = this;
    4.             hideFlags = HideFlags.NotEditable | HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild;
    7.             hideFlags |= HideFlags.HideInInspector;
    8. #endif
    10.             DontDestroyOnLoad(gameObject);
    12.             // i hate this, but i don't know of a better way to know if we're running in a test..
    13.             var mbs = FindObjectsOfType<MonoBehaviour>();
    14.             foreach (var mb in mbs)
    15.             {
    16.                 if (mb.GetType().FullName == "UnityEngine.TestTools.TestRunner.PlaymodeTestsController")
    17.                 {
    18.                     return;
    19.                 }
    20.             }
    22.             AnalyticsService.internalInstance.Startup();
    23.         }
    Is there any way to remove all of these unwanted start-up activities and still use Unity IAP?

    Thank you.
  2. RandolfKlemola


    Unity Technologies

    May 1, 2020
    Hey monstronauts,

    Thanks for reaching out! The error you're receiving is because you haven't linked your project with your Unity account in the Editor. You can find out how to do that Here. Once that's done, you shouldn't have the start-up activities that come with Unity Analytics (due to the SetAnalyticsEnabled(false) snippet).

    In order to use the Unity In-App Purchasing package, Unity Gaming Services needs to be initialized first followed by the initialization of Unity In-App Purchasing.