using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Text; using Newtonsoft.Json; using UnityEngine; namespace Guru { public class GuruAnalytics { // Plugin Version public const string Version = "1.8.4"; public static readonly string Tag = "[ANA]"; private static IAnalyticsAgent _agent; public static IAnalyticsAgent Agent { get { if (_agent == null) { #if UNITY_EDITOR _agent = new AnalyticsAgentStub(); #elif UNITY_ANDROID _agent = new AnalyticsAgentAndroid(); #elif UNITY_IOS _agent = new AnalyticsAgentIOS(); #endif } return _agent; } } private static Dictionary _userProperties; /// /// 用户属性缓存字典 /// public static Dictionary UserProperties { get { if (_userProperties == null) { _userProperties = new Dictionary(10); } return _userProperties; } } #region 公用接口 /// /// 初始化接口 /// public static void Init(string appId, string deviceInfo, bool isDebug = false) { Agent?.Init(appId, deviceInfo, isDebug); #if UNITY_IOS // AnalyticsAgentIOS.TestCrashEvent(); // 测试触发一下崩溃事件 #endif } /// /// 设置视图名称 /// /// public static void SetScreen(string screenName) { CacheUserProperty($"screen_name", screenName); Agent?.SetScreen(screenName); } /// /// 设置广告ID /// /// public static void SetAdId(string id) { CacheUserProperty($"ad_id", id); Agent?.SetAdId(id); } /// /// 设置用户属性 /// /// /// public static void SetUserProperty(string key, string value) { CacheUserProperty(key, value); // 添加用户属性 Agent?.SetUserProperty(key, value); } /// /// 设置Firebase ID /// /// public static void SetFirebaseId(string id) { CacheUserProperty($"firebase_id", id); Agent?.SetFirebaseId(id); } /// /// 设置Adjust ID /// /// public static void SetAdjustId(string id) { CacheUserProperty($"adjust_id", id); Agent?.SetAdjustId(id); } /// /// 设置设备ID /// /// public static void SetDeviceId(string deviceId) { CacheUserProperty($"device_id", deviceId); Agent?.SetDeviceId(deviceId); } /// /// 设置用户ID /// /// public static void SetUid(string uid) { CacheUserProperty($"uid", uid); Agent?.SetUid(uid); } /// /// 上报事件成功率 /// public static void ReportEventSuccessRate() => Agent?.ReportEventSuccessRate(); /// /// 上报打点事件 /// /// 事件名称 /// INT类型的值 public static void LogEvent(string eventName, Dictionary data = null) { string raw = ""; if (data != null && data.Count > 0) { raw = BuildParamsJson(data); } Debug.Log($"{Tag} event:{eventName} | raw: {raw}"); Agent?.LogEvent(eventName, raw); } private static string BuildParamsString(Dictionary data) { string raw = ""; List strList = new List(data.Count); foreach (var kvp in data) { strList.Add(BuildStringValue(kvp)); raw = string.Join(",", strList); } return raw; } private static string BuildParamsJson(Dictionary data) { try { // 强制转换加入国家设置 return JsonConvert.SerializeObject(data, new JsonSerializerSettings() { Culture = new CultureInfo("en-US"), }); } catch (Exception e) { Debug.LogError(e); } return ""; } /// /// 构建带有类型格式的Str值 /// /// /// private static string BuildStringValue(KeyValuePair kvp) { if (kvp.Value is int || kvp.Value is long) { return $"{kvp.Key}:i{kvp.Value}"; } if (kvp.Value is double || kvp.Value is float) { double dValue = (double)((object)kvp.Value); return $"{kvp.Key}:d{dValue.ToString("F11", new CultureInfo("en-US"))}"; // 保留精度进行转换 } return $"{kvp.Key}:s{kvp.Value}"; } /// /// 设置太极02值 /// /// public static void SetTch02Value(double value) { Debug.Log($"{Tag} set tch_02_value:{value}"); Agent?.SetTch02Value(value); } #endregion #region iOS独有接口 #if UNITY_IOS // 触发测试崩溃埋点 public static void TestCrash() => AnalyticsAgentIOS.TestCrashEvent(); #endif #endregion #region 用户属性 /// /// 记录用户属性 /// /// /// private static void CacheUserProperty(string key, string value) { UserProperties[key] = value; } #endregion } }