217 lines
6.0 KiB
C#
217 lines
6.0 KiB
C#
using System;
|
|
using PhxhSDK;
|
|
using System.Collections.Generic;
|
|
|
|
public class BIManager : Singlenton<BIManager>, IInitable
|
|
{
|
|
public BILevelInfo LevelInfo;
|
|
|
|
private IBIService _biService;
|
|
private StorageEvent _storageEvent;
|
|
private Dictionary<string, string> _tempEventDictionary = new(4);
|
|
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;
|
|
}
|
|
|
|
private void InitBiService()
|
|
{
|
|
//Firebase
|
|
|
|
/*var helperName = SDKManager.SdkName.FireBase;
|
|
_biService = SDKManager.Instance.GetSdkHelper(helperName) as IBIService;
|
|
if (_biService == null)
|
|
{
|
|
DebugUtil.LogError("Get BI Service error, SDK helper name is {0}", helperName);
|
|
return;
|
|
}*/
|
|
|
|
_biService?.SetUser(DeviceHelper.GetDeviceId());
|
|
_biService?.SetUserProperty("platform", DeviceHelper.GetPlatformString());
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
_biService?.ReportEvent(eventName, userParam);
|
|
|
|
_tempEventDictionary.Clear();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("BI Event: {0}, TrackEvent Error: {1}", eventName, e);
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
_storageEvent.EventList.Add(firstEventName);
|
|
_biService?.ReportEvent(firstEventName, userParam);
|
|
StorageManager.Instance.SyncForce = true;
|
|
_tempEventDictionary.Clear();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("BI Event: {0}, TrackEventOnce Error: {1}", firstEventName, e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上报关卡内普通事件
|
|
/// </summary>
|
|
public void TrackEventLevel(string eventName, string param1 = null, string param2 = null, string param3 = null)
|
|
{
|
|
if (!_isInit)
|
|
return;
|
|
try
|
|
{
|
|
var levelParam = new Dictionary<string, string>();
|
|
|
|
//TODO 初始化需要上传的LevelInfo
|
|
//LevelInfo.LevelID = ...
|
|
|
|
levelParam = Utils.PropertiesNameValue2Dictionary(LevelInfo);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
var levelParam = new Dictionary<string, string>();
|
|
|
|
//TODO 初始化需要上传的LevelInfo
|
|
//LevelInfo.LevelID = ...
|
|
|
|
levelParam = Utils.PropertiesNameValue2Dictionary(LevelInfo);
|
|
|
|
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);
|
|
_storageEvent.EventList.Add(firstEventName);
|
|
StorageManager.Instance.SyncForce = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugUtil.LogError("BI Event: {0}, TrackEventLevelOnce Error: {1}", firstEventName, e);
|
|
}
|
|
}
|
|
|
|
private bool IsRecorded(string eventName)
|
|
{
|
|
return _storageEvent.EventList.Contains(eventName);
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
_tempEventDictionary = null;
|
|
}
|
|
} |