NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/BI/BIManager.cs

499 lines
14 KiB
C#
Raw Normal View History

2024-05-14 15:29:31 +08:00
using PhxhSDK;
2024-07-25 17:06:02 +08:00
using System;
2024-05-14 15:29:31 +08:00
using System.Collections.Generic;
public partial class BIManager : Singlenton<BIManager>, IInitable
2024-05-14 15:29:31 +08:00
{
public BILevelInfo LevelInfo;
private IBIService _biService;
private StorageEvent _storageEvent;
2024-05-31 14:26:33 +08:00
private Dictionary<string, object> _tempEventDictionary = new(4);
2024-09-18 14:51:32 +08:00
//由于尚未初始化,所以使用队列缓存的待上传事件
private Queue<(string, Dictionary<string, object>)> eventQueue = new();
2024-05-14 15:29:31 +08:00
private bool _isInit;
public void Init()
{
//TODO 替换为需要使用的BI系统 Firebase or 自己的系统
InitBiService();
LevelInfo = new BILevelInfo();
_storageEvent = StorageMgr.Instance.GetStorage<StorageEvent>("StorageEvent");
_storageEvent ??= new StorageEvent();
_isInit = true;
//初次启动游戏打点
TrackEventOnce(BI_GameStart, param: new Dictionary<string, object>() { { "event", "first_open" } });
2024-05-14 15:29:31 +08:00
}
private void InitBiService()
{
//Firebase
2024-05-31 14:26:33 +08:00
var helperName = SDKManager.SdkName.TapTap;
#if SDK_FIREBASE
helperName = SDKManager.SdkName.FireBase;
2024-05-14 15:29:31 +08:00
_biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService;
2024-05-31 14:26:33 +08:00
#endif
2024-07-22 17:08:02 +08:00
2024-05-31 14:26:33 +08:00
#if SDK_TAPTAP
helperName = SDKManager.SdkName.TapTap;
_biService =SDKManager.Instance.GetSdkHelper(helperName) as IBIService;
#endif
2024-07-22 17:08:02 +08:00
#if SDK_TALKINGDATA
2024-09-18 14:51:32 +08:00
helperName = SDKManager.SdkName.TalkingData;
_biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService;
2024-07-22 17:08:02 +08:00
//TD在登录后初始化
#endif
2024-05-14 15:29:31 +08:00
if (_biService == null)
{
2024-05-31 16:36:36 +08:00
DebugUtil.Log("Get BI Service error, SDK helper name is {0}", helperName);
2024-05-14 15:29:31 +08:00
return;
2024-05-31 14:26:33 +08:00
}
2024-05-14 15:29:31 +08:00
2024-05-31 14:26:33 +08:00
//记录一个账号,当账号登录时调用
_biService?.SetUser(SDKManager.Instance.ApplicationUserID);
//设置通用属性 例如 服务器
2024-05-14 15:29:31 +08:00
_biService?.SetUserProperty("platform", DeviceHelper.GetPlatformString());
}
2024-07-22 17:08:02 +08:00
public void BiAfterLoginInit(SDKManager.SdkName name)
{
_biService = SDKManager.Instance.GetSdkHelper(name) as IBIService;
if(_biService == null)
{
DebugUtil.Log("Get BI Service error, SDK helper name is {0}", name);
return;
}
2024-09-18 14:51:32 +08:00
_biService.SetUser(SDKManager.Instance.ApplicationUserID);
while (eventQueue.Count > 0)
{
//将缓存的事件上报
var (eventName, param) = eventQueue.Dequeue();
_biService.ReportEvent(eventName, param);
}
2024-07-22 17:08:02 +08:00
}
2024-05-14 15:29:31 +08:00
/// <summary>
/// 上报普通事件
/// </summary>
public void TrackEvent(string eventName, string param1 = null, string param2 = null, string param3 = null)
{
if (!_isInit)
return;
try
{
_tempEventDictionary.Clear();
var userParam = _tempEventDictionary;
if (!string.IsNullOrEmpty(param1))
{
userParam.Add("param1", param1);
}
if (!string.IsNullOrEmpty(param2))
{
userParam.Add("param2", param2);
}
if (!string.IsNullOrEmpty(param3))
{
userParam.Add("param3", param3);
}
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(eventName, userParam);
}
else
{
eventQueue.Enqueue((eventName, userParam));
}
2024-05-14 15:29:31 +08:00
_tempEventDictionary.Clear();
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEvent Error: {1}", eventName, e);
}
}
public void TrackEvent(string eventName, object param1 = null, object param2 = null, object param3 = null)
{
if (!_isInit)
return;
try
{
_tempEventDictionary.Clear();
var userParam = _tempEventDictionary;
if (param1 != null)
{
userParam.Add("param1", param1);
}
if (param2 != null)
{
userParam.Add("param2", param2);
}
if (param3 != null)
{
userParam.Add("param3", param3);
}
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(eventName, userParam);
}
else
{
eventQueue.Enqueue((eventName, userParam));
}
_tempEventDictionary.Clear();
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEvent Error: {1}", eventName, e);
}
}
2024-05-14 15:29:31 +08:00
/// <summary>
/// 用于传输参数自定义命名事件
/// </summary>
/// <param name="eventName"></param>
/// <param name="param"></param>
public void TrackEvent(string eventName, Dictionary<string, object> param)
{
if (!_isInit)
return;
try
{
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(eventName, param);
}
else
{
eventQueue.Enqueue((eventName, param));
}
}
catch (Exception e)
{
2024-09-29 15:10:14 +08:00
DebugUtil.LogError("Track event fail! EventName:" + eventName + ",\n error msg: " + e.Message);
}
}
2024-05-14 15:29:31 +08:00
/// <summary>
/// 上报一次性事件
/// </summary>
public void TrackEventOnce(string firstEventName, string param1 = null, string param2 = null, string param3 = null)
{
if (!_isInit)
return;
try
{
if (IsRecorded(firstEventName))
{
DebugUtil.Log("已经上报过 {0} 事件, 不会再次上报", firstEventName);
return;
}
_tempEventDictionary.Clear();
var userParam = _tempEventDictionary;
if (!string.IsNullOrEmpty(param1))
{
userParam.Add("param1", param1);
}
if (!string.IsNullOrEmpty(param2))
{
userParam.Add("param2", param2);
}
if (!string.IsNullOrEmpty(param3))
{
userParam.Add("param3", param3);
}
2024-10-18 13:47:19 +08:00
_storageEvent.EventList.Add(firstEventName);
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(firstEventName, userParam);
}
else
{
eventQueue.Enqueue((firstEventName, userParam));
}
StorageMgr.Instance.SyncForce = true;
2024-05-14 15:29:31 +08:00
_tempEventDictionary.Clear();
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventOnce Error: {1}", firstEventName, e);
}
}
public void TrackEventOnce(string firstEventName, object param1 = null, object param2 = null, object param3 = null)
{
if (!_isInit)
return;
try
{
if (IsRecorded(firstEventName))
{
DebugUtil.Log("已经上报过 {0} 事件, 不会再次上报", firstEventName);
return;
}
_tempEventDictionary.Clear();
var userParam = _tempEventDictionary;
if (param1 != null)
{
userParam.Add("param1", param1);
}
if (param2 != null)
{
userParam.Add("param2", param2);
}
if (param3 != null)
{
userParam.Add("param3", param3);
}
2024-10-18 13:47:19 +08:00
_storageEvent.EventList.Add(firstEventName);
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(firstEventName, userParam);
}
else
{
eventQueue.Enqueue((firstEventName, userParam));
}
StorageMgr.Instance.SyncForce = true;
_tempEventDictionary.Clear();
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventOnce Error: {1}", firstEventName, e);
}
}
/// <summary>
/// 用于传输参数自定义命名事件
/// </summary>
/// <param name="firstEventName"></param>
/// <param name="param"></param>
public void TrackEventOnce(string firstEventName,Dictionary<string,object> param)
{
if (!_isInit)
return;
try
{
if (IsRecorded(firstEventName))
{
DebugUtil.Log("已经上报过 {0} 事件, 不会再次上报", firstEventName);
return;
}
2024-10-18 13:47:19 +08:00
_storageEvent.EventList.Add(firstEventName);
2024-09-18 14:51:32 +08:00
if (_biService != null)
{
_biService.ReportEvent(firstEventName, param);
}
else
{
eventQueue.Enqueue((firstEventName, param));
}
StorageMgr.Instance.SyncForce = true;
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventOnce Error: {1}", firstEventName, e);
}
}
2024-05-14 15:29:31 +08:00
/// <summary>
/// 上报关卡内普通事件
/// </summary>
public void TrackEventLevel(string eventName, string param1 = null, string param2 = null, string param3 = null)
{
if (!_isInit)
return;
try
{
2024-05-31 14:26:33 +08:00
var levelParam = new Dictionary<string, object>();
2024-05-14 15:29:31 +08:00
//TODO 初始化需要上传的LevelInfo
//LevelInfo.LevelID = ...
2024-05-31 14:26:33 +08:00
levelParam = Utils.PropertiesNameValue2ObjectDictionary(LevelInfo);
2024-05-14 15:29:31 +08:00
if (!string.IsNullOrEmpty(param1))
{
levelParam.Add("param1", param1);
}
if (!string.IsNullOrEmpty(param2))
{
levelParam.Add("param2", param2);
}
if (!string.IsNullOrEmpty(param3))
{
levelParam.Add("param3", param3);
}
_biService?.ReportEvent(eventName, levelParam);
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventLevel Error: {1}", eventName, e);
}
}
public void TrackEventLevel(string eventName, object param1 = null, object param2 = null, object param3 = null)
{
if (!_isInit)
return;
try
{
var levelParam = new Dictionary<string, object>();
//TODO 初始化需要上传的LevelInfo
//LevelInfo.LevelID = ...
levelParam = Utils.PropertiesNameValue2ObjectDictionary(LevelInfo);
if (param1 != null)
{
levelParam.Add("param1", param1);
}
if (param2 != null)
{
levelParam.Add("param2", param2);
}
if (param3 != null)
{
levelParam.Add("param3", param3);
}
_biService?.ReportEvent(eventName, levelParam);
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventLevel Error: {1}", eventName, e);
}
}
2024-05-14 15:29:31 +08:00
/// <summary>
/// 上报关卡内一次性事件
/// </summary>
public void TrackEventLevelOnce(string firstEventName, string param1 = null, string param2 = null,
string param3 = null)
{
if (!_isInit)
return;
try
{
if (IsRecorded(firstEventName))
{
DebugUtil.Log("已经上报过 {0} 关卡事件,不会再次上报", firstEventName);
return;
}
2024-05-31 14:26:33 +08:00
var levelParam = new Dictionary<string, object>();
2024-05-14 15:29:31 +08:00
//TODO 初始化需要上传的LevelInfo
//LevelInfo.LevelID = ...
2024-05-31 14:26:33 +08:00
levelParam = Utils.PropertiesNameValue2ObjectDictionary(LevelInfo);
2024-05-14 15:29:31 +08:00
if (!string.IsNullOrEmpty(param1))
{
levelParam.Add("param1", param1);
}
if (!string.IsNullOrEmpty(param2))
{
levelParam.Add("param2", param2);
}
if (!string.IsNullOrEmpty(param3))
{
levelParam.Add("param3", param3);
}
_biService?.ReportEvent(firstEventName, levelParam);
2024-10-18 13:47:19 +08:00
_storageEvent.EventList.Add(firstEventName);
StorageMgr.Instance.SyncForce = true;
2024-05-14 15:29:31 +08:00
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventLevelOnce Error: {1}", firstEventName, e);
}
}
public void TrackEventLevelOnce(string firstEventName, object param1 = null, object param2 = null,
object param3 = null)
{
if (!_isInit)
return;
try
{
if (IsRecorded(firstEventName))
{
DebugUtil.Log("已经上报过 {0} 关卡事件,不会再次上报", firstEventName);
return;
}
var levelParam = new Dictionary<string, object>();
//TODO 初始化需要上传的LevelInfo
//LevelInfo.LevelID = ...
levelParam = Utils.PropertiesNameValue2ObjectDictionary(LevelInfo);
if (param1 != null)
{
levelParam.Add("param1", param1);
}
if (param2 != null)
{
levelParam.Add("param2", param2);
}
if (param3 != null)
{
levelParam.Add("param3", param3);
}
_biService?.ReportEvent(firstEventName, levelParam);
2024-10-18 13:47:19 +08:00
_storageEvent.EventList.Add(firstEventName);
StorageMgr.Instance.SyncForce = true;
}
catch (Exception e)
{
DebugUtil.LogError("BI Event: {0}, TrackEventLevelOnce Error: {1}", firstEventName, e);
}
}
2024-05-14 15:29:31 +08:00
private bool IsRecorded(string eventName)
{
2024-10-18 13:47:19 +08:00
return _storageEvent.EventList.Contains(eventName);
2024-05-14 15:29:31 +08:00
}
public void Release()
{
_tempEventDictionary = null;
}
}