12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using mixpanel;
- namespace KairoEngine.Analytics
- {
- /// <summary>Send analytics events to Mixpanel</summary>
- [HideMonoScript] public class AnalyticsComponent : MonoBehaviour
- {
- public static AnalyticsComponent 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<string, System.Object> 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()}");
- }
- }
- }
|