BI实现多方打点逻辑 添加ThinkingData

main
zhangaotian 2025-10-21 16:57:38 +08:00
parent 0ae29a4652
commit ce8d9d65dc
2 changed files with 78 additions and 53 deletions

View File

@ -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<BIManager>, IInitable, IUpdatable
@ -18,9 +19,9 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
private BILevelInfo levelInfo;
/// <summary>
/// BI接口
/// BI接口 10.21 转为多个bi借口
/// </summary>
private IBIService _biService;
/// private IBIService _biService;
/// <summary>
/// 一次性事件缓存类
@ -50,8 +51,14 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
#if SDK_TAPTAP
SDKManager.SdkName.TapTap,
#endif
#if SDK_THINKINGDATA
SDKManager.SdkName.ThinkingData,
#endif
};
private List<IBIService> biServices = new List<IBIService>();
public PublicDictionaryPool DictionaryPool { get; } = new PublicDictionaryPool();
/// <summary>
@ -60,10 +67,12 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
private Dictionary<string, object> _tempEventDictionary = new(12);
private readonly string PhxhBiUrl = "https://bi.kedrgame.com/api/v1/events";
/// <summary>
/// BI专属accessKey请勿与中台key混用
/// </summary>
private readonly string AccessKey = "yd9kWI798IWyyq1t";
/// <summary>
/// BI专属密钥请勿与中台密钥混用
/// </summary>
@ -99,16 +108,15 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
/// <summary>
/// 登陆后初始化
/// </summary>
/// <param name="name"></param>
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);
}
/// <summary>
@ -118,8 +126,13 @@ public partial class BIManager : Singlenton<BIManager>, 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<BIManager>, IInitable, IUpdatable
/// </summary>
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<BIManager>, 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<BIManager>, IInitable, IUpdatable
_dictPool = null; // 释放对象池
if (_isInit && _networkAvailable && eventQueue.Count > 0)
{
{
SendQueuedEvents();
}
}
@ -207,7 +209,8 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
/// <summary>
/// 事件队列 - 增加时间戳信息
/// </summary>
private readonly Queue<(string eventName, Dictionary<string, object> param, long localTime, ulong serverTime)> eventQueue = new();
private readonly Queue<(string eventName, Dictionary<string, object> param, long localTime, ulong serverTime)>
eventQueue = new();
/// <summary>
/// 是否使用队列模式(无网络或设置了发送周期)
@ -329,7 +332,7 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
/// </summary>
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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, 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<BIManager>, IInitable, IUpdatable
Dictionary<string, string> headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
{
{ "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<BIManager>, 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<BIManager>, 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<BIManager>, IInitable, IUpdatable
_dictPoolIndex = (_dictPoolIndex + 1) % DICT_POOL_SIZE;
return dict;
}
#endregion
#region 外部打点字典对象池
/// <summary>
/// 对象池字典请使用BIManager内部的PublicDictionaryPool获取对象
/// </summary>
@ -749,6 +771,7 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
{
_dictPool.Add(new RecycleDictionary());
}
_dictPoolIndex = 0;
}
@ -760,6 +783,7 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
// TODO 也可以扩容池
return new RecycleDictionary();
}
var dict = _dictPool[_dictPoolIndex];
_dictPoolIndex++;
dict.Clear();
@ -774,5 +798,6 @@ public partial class BIManager : Singlenton<BIManager>, IInitable, IUpdatable
_dictPool[_dictPoolIndex] = dict;
}
}
#endregion
}

@ -1 +1 @@
Subproject commit 30a38e04fd749844a2c5d3d7bb10cf4737872dd9
Subproject commit 1d299c7a2ee10b60435484257b3fe1543b0ce3e6