Kairoengine analytics library. Sends data to the Mixpanel service.
|
2 anni fa | |
---|---|---|
Runtime | 2 anni fa | |
.gitignore | 2 anni fa | |
Readme.md | 2 anni fa | |
Readme.md.meta | 2 anni fa | |
Runtime.meta | 2 anni fa | |
package.json | 2 anni fa | |
package.json.meta | 2 anni fa |
The Analytics package uses the Mixpanel service to store analytics. It can be easly changed to use another service without having to change the actual event tracking inside the game.
Mixpanel.Unity
(github)KairoEngine.Core
KairoEngine.Analytics
Analytics
- The analytics system component that needs to be filled out with the mixpanel Key.Create a new class called GameAnalytics
or use the name of the current game and add the following libraries:
using KairoEngine.Core;
using KairoEngine.Utilities;
using KairoEngine.Analytics;
/// <summary>Listen to game events and emmit analytic events.</summary>
public class GameAnalytics : MonoBehaviour
{
}
Then to start tracking a player, use these snippets:
// 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:
// Simple event
Analytics.Track("Game Initialized");
// Event with payload
var data = new Dictionary<string, System.Object>();
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:
// 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;
}