using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using mixpanel; namespace KairoEngine.Analytics { /// Send analytics events to Mixpanel [HideMonoScript] public class Analytics : MonoBehaviour { public static Analytics instance; public bool trackEvents = true; public bool debugEvents = true; public bool clearEventsOnStart = false; void OnApplicationQuit() => Mixpanel.Disable(); void Awake() { if(instance == null) instance = this; else Destroy(this); if(clearEventsOnStart) Mixpanel.ClearTimedEvents(); Mixpanel.Init(); } public static bool IsTracking() => instance != null ? instance.trackEvents : false; public static bool ShowDebug() => instance != null ? instance.debugEvents : false; public static void Identify(string playerID) => Mixpanel.Identify(playerID); public static void Register(string propertieTitle, int propertyValue) => Mixpanel.Register(propertieTitle, propertyValue); public static void Register(string propertieTitle, float propertyValue) => Mixpanel.Register(propertieTitle, propertyValue); public static void Register(string propertieTitle, string propertyValue) => Mixpanel.Register(propertieTitle, propertyValue); public static void StartTimedEvent(string title) => Mixpanel.StartTimedEvent(title); public static void Flush() => Mixpanel.Flush(); public static void Track(string eventTitle) { if(IsTracking()) Mixpanel.Track(eventTitle); if(ShowDebug()) Debug.Log($"Tracking event \'{eventTitle}\'"); } public static void Track(string eventTitle, Value props) { if(IsTracking()) Mixpanel.Track(eventTitle, props); if(ShowDebug()) Debug.Log($"Tracking event \'{eventTitle}\', {props.ToString()}"); } public static void Track(string eventTitle, Dictionary dictionary) { if(!IsTracking()) return; var props = new Value(); foreach (var item in dictionary) { if(item.Value.GetType() == typeof(int)) props[item.Key] = (int)item.Value; else if(item.Value.GetType() == typeof(string)) props[item.Key] = (string)item.Value; else if(item.Value.GetType() == typeof(float)) props[item.Key] = (float)item.Value; } Mixpanel.Track(eventTitle, props); if(ShowDebug()) Debug.Log($"Tracking event \'{eventTitle}\', {props.ToString()}"); } } }