From ce8d9d65dc1ae46bc206d98708a0169cdcafd9fa Mon Sep 17 00:00:00 2001 From: zhangaotian <8474238@qq.com> Date: Tue, 21 Oct 2025 16:57:38 +0800 Subject: [PATCH] =?UTF-8?q?BI=E5=AE=9E=E7=8E=B0=E5=A4=9A=E6=96=B9=E6=89=93?= =?UTF-8?q?=E7=82=B9=E9=80=BB=E8=BE=91=20=E6=B7=BB=E5=8A=A0ThinkingData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Code/Scripts/Framework/BI/BIManager.cs | 129 +++++++++++------- ProjectNLD/Assets/PhxhSDK | 2 +- 2 files changed, 78 insertions(+), 53 deletions(-) diff --git a/ProjectNLD/Assets/Code/Scripts/Framework/BI/BIManager.cs b/ProjectNLD/Assets/Code/Scripts/Framework/BI/BIManager.cs index 1a2d4af450e..53e6d8c1d88 100644 --- a/ProjectNLD/Assets/Code/Scripts/Framework/BI/BIManager.cs +++ b/ProjectNLD/Assets/Code/Scripts/Framework/BI/BIManager.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using PhxhSDK; using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; public partial class BIManager : Singlenton, IInitable, IUpdatable @@ -18,9 +19,9 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable private BILevelInfo levelInfo; /// - /// BI接口 + /// BI接口 10.21 转为多个bi借口 /// - private IBIService _biService; + /// private IBIService _biService; /// /// 一次性事件缓存类 @@ -50,8 +51,14 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable #if SDK_TAPTAP SDKManager.SdkName.TapTap, #endif + +#if SDK_THINKINGDATA + SDKManager.SdkName.ThinkingData, +#endif }; + private List biServices = new List(); + public PublicDictionaryPool DictionaryPool { get; } = new PublicDictionaryPool(); /// @@ -60,10 +67,12 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable private Dictionary _tempEventDictionary = new(12); private readonly string PhxhBiUrl = "https://bi.kedrgame.com/api/v1/events"; + /// /// BI专属accessKey,请勿与中台key混用 /// private readonly string AccessKey = "yd9kWI798IWyyq1t"; + /// /// BI专属密钥,请勿与中台密钥混用 /// @@ -99,16 +108,15 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable /// /// 登陆后初始化 /// - /// - public void AfterLoginInitService(SDKManager.SdkName name) + public void AfterLoginInitService() { - _biService = SDKManager.Instance.GetSdkHelper(name) as IBIService; - if (_biService == null) + biServices.Clear(); + foreach (var sdkName in ExistBiService) { - DebugUtil.Log("After Login Get BI Service error, SDK helper name is {0}", name); - return; + var service = SDKManager.Instance.GetSdkHelper(sdkName) as IBIService; + if (service != null) + biServices.Add(service); } - DebugUtil.Log("After Login Get BI Service success, SDK helper name is {0}", name); } /// @@ -118,8 +126,13 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable { baseInfo.SetUserInfo(); baseInfo.PrintAllValues(); - if (!_isInit || (_biService == null && ExistBiService.Count != 0)) return; - _biService?.SetUser(baseInfo.actor_id); + if (!_isInit || biServices.Count <= 0) return; + + foreach (var biService in biServices) + { + biService?.SetUser(baseInfo.actor_id); + } + SetUserProperty(); // 发送积累事件 SendQueuedEvents(); @@ -130,26 +143,12 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable /// private void InitService() { - var helperName = SDKManager.SdkName.TalkingData; - -#if SDK_FIREBASE - helperName = SDKManager.SdkName.FireBase; - _biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService; -#endif - -#if SDK_TAPTAP - helperName = SDKManager.SdkName.TapTap; - _biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService; -#endif - -#if SDK_TALKINGDATA - helperName = SDKManager.SdkName.TalkingData; - _biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService; - -#endif - if ((_biService == null && ExistBiService.Count != 0)) + biServices.Clear(); + foreach (var sdkName in ExistBiService) { - DebugUtil.Log("Before Login Get BI Service error, SDK helper name is {0}", helperName); + var service = SDKManager.Instance.GetSdkHelper(sdkName) as IBIService; + if (service != null) + biServices.Add(service); } } @@ -180,9 +179,12 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable #endif } - public void SetUserProperty(string name, string value) + private void SetUserProperty(string name, string value) { - _biService?.SetUserProperty(name, value); + foreach (var biService in biServices) + { + biService?.SetUserProperty(name, value); + } } #endregion @@ -195,7 +197,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable _dictPool = null; // 释放对象池 if (_isInit && _networkAvailable && eventQueue.Count > 0) - { + { SendQueuedEvents(); } } @@ -207,7 +209,8 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable /// /// 事件队列 - 增加时间戳信息 /// - private readonly Queue<(string eventName, Dictionary param, long localTime, ulong serverTime)> eventQueue = new(); + private readonly Queue<(string eventName, Dictionary param, long localTime, ulong serverTime)> + eventQueue = new(); /// /// 是否使用队列模式(无网络或设置了发送周期) @@ -329,7 +332,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable /// private void SendQueuedEvents() { - if ((_biService == null && ExistBiService.Count != 0) || !_networkAvailable) + if (biServices.Count <= 0 || !_networkAvailable) return; // 只要队列有事件就全部处理 @@ -337,8 +340,12 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable { //将缓存的事件上报 var (eventName, param, localTime, serverTime) = eventQueue.Dequeue(); - DebugUtil.Log("发送缓存事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(param)); - _biService?.ReportEvent(eventName, param); + foreach (var biService in biServices) + { + biService?.ReportEvent(eventName, param); + DebugUtil.Log("{0} 发送缓存事件: {1}, 事件参数: {2}", biService, eventName, JsonConvert.SerializeObject(param)); + } + TrackEventPhxh(eventName, param, localTime, serverTime); } } @@ -379,7 +386,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable try { var localTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - + _tempEventDictionary.Clear(); var userParam = _tempEventDictionary; if (!string.IsNullOrEmpty(param1Key) && param1 != null) @@ -394,15 +401,18 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable if (!string.IsNullOrEmpty(param4Key) && param4 != null) userParam.Add(param4Key, param4); - if (_useQueueMode || (_biService == null && ExistBiService.Count != 0)) + if (_useQueueMode || biServices.Count <= 0) { DebugUtil.Log("缓存发送事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(userParam)); EnqueueEvent(eventName, userParam, localTime, serverTime); } else { - DebugUtil.Log("发送事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(userParam)); - _biService?.ReportEvent(eventName, userParam); + foreach (var biService in biServices) + { + biService?.ReportEvent(eventName, userParam); + DebugUtil.Log("{0} 发送缓存事件: {1}, 事件参数: {2}", biService, eventName, JsonConvert.SerializeObject(userParam)); + } TrackEventPhxh(eventName, userParam, localTime, serverTime); } } @@ -422,7 +432,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable try { var localTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - + _tempEventDictionary.Clear(); var userParam = _tempEventDictionary; foreach (var item in recycleDictionary.dict) @@ -432,15 +442,19 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable userParam.Add(item.Key, item.Value); } } + DictionaryPool.ReturnDictToPool(recycleDictionary); - if (_useQueueMode || (_biService == null && ExistBiService.Count != 0)) + if (_useQueueMode || biServices.Count <= 0) { EnqueueEvent(eventName, userParam, localTime, serverTime); } else { - DebugUtil.Log("发送事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(userParam)); - _biService?.ReportEvent(eventName, userParam); + foreach (var biService in biServices) + { + biService?.ReportEvent(eventName, userParam); + DebugUtil.Log("{0} 发送缓存事件: {1}, 事件参数: {2}", biService, eventName, JsonConvert.SerializeObject(userParam)); + } TrackEventPhxh(eventName, userParam, localTime, serverTime); } } @@ -484,7 +498,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable try { var localTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - + _tempEventDictionary.Clear(); var userParam = _tempEventDictionary; userParam.Add("level_id", levelInfo.level_id); // 关卡ID @@ -505,13 +519,17 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable DebugUtil.Log("发送关卡事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(userParam)); - if (_useQueueMode || (_biService == null && ExistBiService.Count != 0)) + if (_useQueueMode || biServices.Count <= 0) { EnqueueEvent(eventName, userParam, localTime, serverTime); } else { - _biService?.ReportEvent(eventName, userParam); + foreach (var biService in biServices) + { + biService?.ReportEvent(eventName, userParam); + DebugUtil.Log("{0} 发送缓存事件: {1}, 事件参数: {2}", biService, eventName, JsonConvert.SerializeObject(userParam)); + } TrackEventPhxh(eventName, userParam, localTime, serverTime); } } @@ -535,6 +553,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable DebugUtil.Log("已经上报过 {0} 关卡事件,不会再次上报", eventName); return; } + _storageEvent.EventList.Add(eventName); StorageMgr.Instance.SyncForce = true; TrackLevelEvent(eventName, serverTime, param1Key, param1, param2Key, param2, param3Key, param3); @@ -550,6 +569,8 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable return; try { + DebugUtil.Log("PHXH 发送事件: {0}, 事件参数: {1}", eventName, JsonConvert.SerializeObject(param)); + var url = PhxhBiUrl; baseInfo ??= new BIBaseInfo(); //var timestampMs = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); @@ -584,7 +605,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable Dictionary headData = PostHeader.PostHeaderInfo(new Dictionary { { "event_key", sendData.event_key }, - { "app_name",sendData.app_name }, + { "app_name", sendData.app_name }, { "event_value", sendData.event_value }, { "channel", sendData.channel }, { "model", sendData.model }, @@ -596,14 +617,13 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable { "build_code", sendData.build_code }, { "assets_version", sendData.assets_version }, { "language", sendData.language }, - { "region",sendData.region }, + { "region", sendData.region }, { "actor_id", sendData.actor_id }, { "third_platform", sendData.third_platform }, { "third_id", sendData.third_id }, { "local_time", sendData.local_time.ToString() }, { "server_time", sendData.server_time.ToString() }, { "access_key", sendData.access_key }, - }, SecretKey, AccessKey); HttpHelper.PostAsync(url, sendData, headData).Forget(); @@ -636,7 +656,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable public string third_platform; // 第三方账号源 public string third_id; // 第三方账号ID - public string access_key;//中台accesskey,包体打包内自带 + public string access_key; //中台accesskey,包体打包内自带 // 新增的时间戳字段 public long local_time; // 本地时间 @@ -695,9 +715,11 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable _dictPoolIndex = (_dictPoolIndex + 1) % DICT_POOL_SIZE; return dict; } + #endregion #region 外部打点字典对象池 + /// /// 对象池字典,请使用BIManager内部的PublicDictionaryPool获取对象 /// @@ -749,6 +771,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable { _dictPool.Add(new RecycleDictionary()); } + _dictPoolIndex = 0; } @@ -760,6 +783,7 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable // TODO 也可以扩容池 return new RecycleDictionary(); } + var dict = _dictPool[_dictPoolIndex]; _dictPoolIndex++; dict.Clear(); @@ -774,5 +798,6 @@ public partial class BIManager : Singlenton, IInitable, IUpdatable _dictPool[_dictPoolIndex] = dict; } } + #endregion } \ No newline at end of file diff --git a/ProjectNLD/Assets/PhxhSDK b/ProjectNLD/Assets/PhxhSDK index 30a38e04fd7..1d299c7a2ee 160000 --- a/ProjectNLD/Assets/PhxhSDK +++ b/ProjectNLD/Assets/PhxhSDK @@ -1 +1 @@ -Subproject commit 30a38e04fd749844a2c5d3d7bb10cf4737872dd9 +Subproject commit 1d299c7a2ee10b60435484257b3fe1543b0ce3e6