chicken_dy/Assets/HCMiniSdk/Scripts/Analytics/HCAnalyticsManager.cs

412 lines
14 KiB
C#
Raw Permalink Normal View History

2024-03-07 12:12:48 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using HC.Plugins;
namespace HC
{
public class HCAnalyticsManager : HCSingleton<HCAnalyticsManager>
{
private bool m_isInitialized = false;
private Dictionary<TrackEventPlatform, HCIBaseAnalytics> plugins;
public Action<TrackEventPlatform, Dictionary<string, object>> RemoteConfigListen;
protected override void OnInstanceCreate()
{
base.OnInstanceCreate();
plugins = new Dictionary<TrackEventPlatform, HCIBaseAnalytics>
{
#if UNITY_WEBGL || WEBGL_BYTEDANCE
[TrackEventPlatform.WebGL] = HCWebGL.Instance,
#endif
[TrackEventPlatform.HC] = HCAnalytics.Instance,
};
#if UNITY_WEBGL && WEBGL_WX
if (!(string.IsNullOrEmpty(HCStaticParams.TDAppId) || string.IsNullOrEmpty(HCStaticParams.TDServerUrl)))
{
plugins.Add(TrackEventPlatform.TD, HCTDAnalyticsWebGL.Instance);
}
#elif (UNITY_ANDROID && TE_DISABLE_ANDROID_JAVA) || UNITY_IOS || (UNITY_WEBGL && WEBGL_BYTEDANCE)
if (!(string.IsNullOrEmpty(HCStaticParams.TDAppId) || string.IsNullOrEmpty(HCStaticParams.TDServerUrl)))
{
plugins.Add(TrackEventPlatform.TD, HCTDAnalyticsUnity.Instance);
}
#endif
HCDebugger.LogDebug($"analytics plugins = {plugins.Count}");
}
public HCIBaseAnalytics GetAnalyticsPlugin(TrackEventPlatform platform)
{
return plugins.GetValueOrDefault(platform, null);
}
public void InitializeSdk()
{
if (m_isInitialized) return;
PrepareDeviceID();
foreach (var plugin in plugins.Values)
{
plugin.InitializeSdk();
}
m_isInitialized = true;
}
#region 广
public void TrackRevenueEvent(Dictionary<string, object> param)
{
foreach (var plugin in plugins.Values)
{
plugin.TrackAdRevenue(HCInnerStaticSting.HC_Ad_Impression, param);
}
}
#endregion
#region deviceID
private void PrepareDeviceID()
{
string deviceID = HCTools.GetPlayerPrefsString(HCAccountManager.Instance.staticSaveKey_DeviceID);
if (string.IsNullOrEmpty(deviceID))
{
deviceID = HCNativeInterface.Instance.GetDeviceID();
if (string.IsNullOrEmpty(deviceID))
{
deviceID = System.Guid.NewGuid().ToString();
}
HCTools.SavePlayerPrefsString(HCAccountManager.Instance.staticSaveKey_DeviceID, deviceID);
SetSuperProperties(new Dictionary<string, object>() { { "device_id", deviceID } });
}
}
#endregion
public string GetRemoteConfigStr(string _key, string _defaultValue)
{
foreach (var plugin in plugins.Values.Where(plugin => plugin.ContainsRemoteConfigKey(_key)))
{
return plugin.GetRemoteConfigStr(_key, _defaultValue);
}
return _defaultValue;
}
public int GetRemoteConfigInt(string _key, int _defaultValue)
{
foreach (var plugin in plugins.Values.Where(plugin => plugin.ContainsRemoteConfigKey(_key)))
{
return plugin.GetRemoteConfigInt(_key, _defaultValue);
}
return _defaultValue;
}
public bool GetRemoteConfigBool(string _key, bool _defaultValue)
{
foreach (var plugin in plugins.Values.Where(plugin => plugin.ContainsRemoteConfigKey(_key)))
{
return plugin.GetRemoteConfigBool(_key, _defaultValue);
}
return _defaultValue;
}
public void SetSuperProperties(Dictionary<string, object> _mPoperties)
{
foreach (var plugin in plugins.Values)
{
plugin.SetSuperProperties(_mPoperties);
}
}
public void SetUserProperties(Dictionary<string, object> _mPoperties)
{
foreach (var plugin in plugins.Values)
{
plugin.UserSet(_mPoperties);
}
}
public void SetUserId(string _userId)
{
foreach (var plugin in plugins.Values)
{
plugin.Login(_userId);
}
}
public void UserSetOnce(Dictionary<string, object> properties)
{
foreach (var plugin in plugins.Values)
{
plugin.UserSetOnce(properties);
}
}
public void SetLogEnable(bool _enable)
{
foreach (var plugin in plugins.Values)
{
plugin.SetLogEnable(_enable);
}
}
#region track event
public void TrackEvent(string _eventName, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All)
{
TrackEvent(_eventName, new Dictionary<string, object>(), _trackEventPlatform);
}
public void TrackEvent(string _eventName, Dictionary<string, object> _eventDic, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All)
{
_eventDic ??= new Dictionary<string, object>();
_eventDic = _eventDic.Concat(GetCommonAttribute()).ToDictionary(postParK => postParK.Key, PostParV => PostParV.Value);
if (_trackEventPlatform == TrackEventPlatform.All)
{
foreach (var analytics in plugins.Values)
{
analytics.TrackEvent(_eventName, _eventDic);
}
}
else
{
if (plugins.TryGetValue(_trackEventPlatform, out var plugin))
{
plugin.TrackEvent(_eventName, _eventDic);
}
}
}
public void TrackEvent(string _eventName, string _key01, object _value01, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All)
{
TrackEvent(_eventName, new Dictionary<string, object> { { _key01, _value01 } }, _trackEventPlatform);
}
public void TrackEvent(string _eventName, string _key01, object _value01, string _key02, object _value02, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All)
{
TrackEvent(_eventName, new Dictionary<string, object> { { _key01, _value01 }, { _key02, _value02 } }, _trackEventPlatform);
}
#endregion
#region reward btn
public void LogRewardBtnShow(string _pos)
{
TrackEvent(HCInnerStaticSting.HC_RV_Button_Show, new Dictionary<string, object>() { { HCInnerStaticSting.HC_Position, _pos } });
}
public void LogRewardBtnClick(string _pos)
{
TrackEvent(HCInnerStaticSting.HC_RV_Button_Click, new Dictionary<string, object>() { { HCInnerStaticSting.HC_Position, _pos } });
}
#endregion
#region Iap event
public void IAPBtnShow(string productName, string productID, string currency, int price)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("Currency", currency);
args.Add("Price", price);
TrackEvent(HCInnerStaticSting.HC_IAP_Button_Show, args);
}
public void IAPBtnClick(string productName, string productID, string currency, int price, string gameExtra)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("Currency", currency);
args.Add("Price", price);
#if UNITY_IOS
args.Add("payment_type", "Appstore");
#else
args.Add("payment_type", "GooglePlay");
#endif
args.Add("payment_num", gameExtra);
TrackEvent(HCInnerStaticSting.HC_IAP_Button_Click, args);
}
public void IAPSuccess(string productName, string productID, string orderID, string currency, int price, string gameExtra, int balance = 0, int restoreOrders = 0)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("Currency", currency);
args.Add("Price", price);
args.Add("order_id", orderID);
args.Add("is_first", HCTools.GetPlayerPrefsInt(HCInnerStaticSting.HC_First_Purchase, 0) == 0);
#if UNITY_IOS
args.Add("payment_type", "Appstore");
#elif UNITY_ANDROID
args.Add("payment_type", "GooglePlay");
#elif UNITY_WEBGL && WEBGL_WX
args.Add("payment_type", "wechat");
#elif UNITY_WEBGL && WEBGL_BYTEDANCE
args.Add("payment_type", "douyin");
#else
args.Add("payment_type", "other");
#endif
args.Add("payment_num", gameExtra);
args.Add("balance", balance);
args.Add("restoreOrders", restoreOrders);
TrackEvent(HCInnerStaticSting.HC_IAP_Success, args);
float fPrice = price * 1.0f;
fPrice = fPrice + HCTools.GetPlayerPrefsFloat("PURCHASE_PRICE", 0.0f);
HCTools.SavePlayerPrefsFloat("PURCHASE_PRICE", fPrice);
int payTimes = HCTools.GetPlayerPrefsInt("PAY_TIME", 0) + 1;
HCTools.SavePlayerPrefsInt("PAY_TIME", payTimes);
SetUserProperties(new Dictionary<string, object>() { { "total_pay_amount", fPrice }, { "total_pay_times", payTimes } });
HCTools.SavePlayerPrefsInt(HCInnerStaticSting.HC_First_Purchase, 1);
SetSuperProperties(new Dictionary<string, object>() { { "pay_times", payTimes } });
FirstPurchase();
}
public void IAPChannelSuccess(string productName, string productID, string orderID, string currency, int price, string gameExtra)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("Currency", currency);
args.Add("Price", price);
args.Add("order_id", orderID);
args.Add("is_first", HCTools.GetPlayerPrefsInt(HCInnerStaticSting.HC_First_Purchase, 0) == 0);
#if UNITY_IOS
args.Add("payment_type", "Appstore");
#elif UNITY_ANDROID
args.Add("payment_type", "GooglePlay");
#elif UNITY_WEBGL && WEBGL_WX
args.Add("payment_type", "wechat");
#elif UNITY_WEBGL && WEBGL_BYTEDANCE
args.Add("payment_type", "douyin");
#else
args.Add("payment_type", "other");
#endif
args.Add("payment_num", gameExtra);
TrackEvent(HCInnerStaticSting.HC_IAP_Channel_Success, args);
}
public void IAPFail(string productName, string productID, string orderID, string currency, int price, string gameExtra, string failReason, int restoreOrders = 0)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("currency_type", currency);
args.Add("pay_amount", price);
args.Add("order_id", orderID);
args.Add("restoreOrders", restoreOrders);
#if UNITY_IOS
args.Add("payment_type", "Appstore");
#elif UNITY_ANDROID
args.Add("payment_type", "GooglePlay");
#elif UNITY_WEBGL && WEBGL_WX
args.Add("payment_type", "wechat");
#elif UNITY_WEBGL && WEBGL_BYTEDANCE
args.Add("payment_type", "douyin");
#else
args.Add("payment_type", "other");
#endif
args.Add("payment_num", gameExtra);
args.Add("fail_reason", failReason);
TrackEvent(HCInnerStaticSting.HC_IAP_Fail, args);
}
public void IAPChannelFail(string productName, string productID, string orderID, string currency, int price, string gameExtra, string failReason)
{
var args = new Dictionary<string, object>();
args.Add("IAP", productName);
args.Add("ID", productID);
args.Add("currency_type", currency);
args.Add("pay_amount", price);
args.Add("order_id", orderID);
#if UNITY_IOS
args.Add("payment_type", "Appstore");
#elif UNITY_ANDROID
args.Add("payment_type", "GooglePlay");
#elif UNITY_WEBGL && WEBGL_WX
args.Add("payment_type", "wechat");
#elif UNITY_WEBGL && WEBGL_BYTEDANCE
args.Add("payment_type", "douyin");
#else
args.Add("payment_type", "other");
#endif
args.Add("payment_num", gameExtra);
args.Add("fail_reason", failReason);
TrackEvent(HCInnerStaticSting.HC_IAP_Channel_Fail, args);
}
public void FirstPurchase()
{
UserSetOnce(new Dictionary<string, object>() { { "first_pay_time", HCTimeTools.FormatDateTime(HCTimeTools.GetCurrentTimestamp(), "yyyy 年 M 月 d 日 HH:mm:ss.fff") } });
}
#endregion
#region common attribute
/// <summary>
/// 公共事件属性
/// </summary>
private Dictionary<string, object> commonAttributeDic = new Dictionary<string, object> { };
public Dictionary<string, object> GetCommonAttribute()
{
commonAttributeDic ??= new Dictionary<string, object>();
if (commonAttributeDic.Count != 0) return commonAttributeDic;
commonAttributeDic = new Dictionary<string, object>
{
{ "is_new", GetIntervalDays() == 0 ? "Y" : "N" },
{ "D_2", GetAttributeDayValue(2) },
{ "D_3", GetAttributeDayValue(3) },
{ "D_7", GetAttributeDayValue(7) },
{ "D_15", GetAttributeDayValue(15) },
{ "D_30", GetAttributeDayValue(30) },
{ "D_45", GetAttributeDayValue(45) },
{ "D_60", GetAttributeDayValue(60) },
{ "Group", GetRemoteConfigStr("AddGroup", "") },
{ "unity_sdk_version", HCNativeInterface.SDKVersion }
};
return commonAttributeDic;
}
private string GetAttributeDayValue(int day)
{
return (day - 1) <= GetIntervalDays() ? "Y" : "N";
}
public static int GetIntervalDays()
{
var firstTime = HCTools.GetPlayerPrefsInt(HCTools.FIRST_TIME_OPEN_GAME);
return HCTimeTools.GetDaysBetweenTimestamps(firstTime, HCTimeTools.GetCurrentTimestamp());
}
#endregion
}
}