# 📦 KairoEngine.Analytics.v0.2.0 The Analytics package uses the [Mixpanel](https://mixpanel.com) service to store analytics. It can be easly changed to use another service without having to change the actual event tracking inside the game. ### Required packages - `Mixpanel.Unity` ([github](https://github.com/mixpanel/mixpanel-unity)) - ``KairoEngine.Core`` ### Namespaces - `KairoEngine.Analytics` ### Modules ### Components - `Analytics` - The analytics system component that needs to be filled out with the mixpanel Key. ### Getting Started Create a new class called ``GameAnalytics`` or use the name of the current game and add the following libraries: ```csharp using KairoEngine.Core; using KairoEngine.Utilities; using KairoEngine.Analytics; /// Listen to game events and emmit analytic events. public class GameAnalytics : MonoBehaviour { } ``` Then to start tracking a player, use these snippets: ```csharp // Get or create a Player ID var randomID = KairoEngine.Core.Utilities.RandomString(12); var playerID = PlayerPrefs.GetString("PlayerID", randomID); PlayerPrefs.SetString("PlayerID", playerID); // Set the ID for all tracking done from now on Analytics.Identify(playerID); // Set App Version Analytics.Register("App Version", Application.version); // Send data to the server in 2 seconds StartCoroutine(Timer.Start(2f, false, () => { Analytics.Flush(); })); ``` To track stuff: ```csharp // Simple event Analytics.Track("Game Initialized"); // Event with payload var data = new Dictionary(); data.Add("Level", level); Analytics.Track("Level Started", data); // Timed event Analytics.StartTimedEvent("Level Finished"); StartCoroutine(Timer.Start(10f, false, () => { Analytics.Track("Level Finished", data); })); ``` To send analytics before quitting the application: ```csharp // On game init, run this function that adds an // event listener for when the game wants to quit [RuntimeInitializeOnLoadMethod] static void RunOnStart() => Application.wantsToQuit += OnApplicationQuit; // Singleton for this component so that it can be // referenced in a Lambda function public static GameAnalytics instance; // When quitting the application: // Cancel quitting, send quit event, flush analytics, // wait 2 seconds and finally quit the application. static bool OnApplicationQuit() { // Finished the Timed event Analytics.Track("Quit"); // Send data to the Analytics server // This takes about one second to happen // If the app quits now data might not be sent Analytics.Flush(); if(instance != null) { instance.StartCoroutine(Timer.Start(2f, false, () => { // Actualy close the application Application.Quit(); })); // Remove the event listener so no bugs pop up Application.wantsToQuit -= OnApplicationQuit; // Return false so that the app doesn't quit return false; } // If there is no instance return true so that the app can quit return true; } ``` ### 📄Changelog ##### v0.2.0 - Upgraded GameModule system ### Functions ### Back Log - [ ] Create a module that loads the analytics component on init and holds its data