Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom event at end of session

Discussion in 'Unity Analytics' started by kenli_touchten, Jun 10, 2015.

  1. kenli_touchten

    kenli_touchten

    Joined:
    Jan 26, 2015
    Posts:
    13
    Hello,

    We tried to create a custom event at the end of a user's session to record how much player use their energy in one session.
    1. This event is not fired when player end their session, instead they are fired when player begin a new session after that but it's not recorded, instead it says "Analytic not initialized" ?
    2. Also, this custom event somehow is fired every time the game went to background even just for one second, instead of "went idle for 30 minutes" as Unity Analytic would normally count one session?

    Appreciate any advice.

    void OnApplicationPause (bool isPause) {

    if (isPause)

    RecordSessionEnd ();

    }


    void RecordSessionEnd () {

    var staminaUsed = PlayerPrefs.GetInt (KeyConstants.SESSION_STAMINA_USE, 0);

    CharacterManager.onStaminaUse -= RecordStaminaUse;


    var totalStamina = 0;

    foreach (var pc in CharacterManager.GetAllCharacters ()) {

    if (pc.boughtStatus)

    totalStamina += pc.GetStamina ();

    }


    Analytics.RecordSessionEnd (staminaUsed, totalStamina);

    }

    Thank you
     
  2. kentunity

    kentunity

    Unity Technologies

    Joined:
    Sep 16, 2014
    Posts:
    55
    #1 If you fire an event right before an app goes into background(ex: OnApplicationPause), this doesn't guarantee that it will get sent, because the app has limited time and resources right before going to background. However it shouldn't show as "Analytics not initialized"
    What version of Unity are you using and what platform do you see this behaviour or "Analytics not initialized"?

    #2 We have our own logic that the user must have minimized/loggedout of the app for 30mins for a new session to start.
    If you want to mimic this, you can store the timestamp OnApplicationPause(false) and OnApplication(true) and compare the diff to see if it has been 30 mins.
     
  3. kenli_touchten

    kenli_touchten

    Joined:
    Jan 26, 2015
    Posts:
    13
    Thanks for the answer. We are using Unity 4.6.3F1 on Android.