update: 打点初始化重构,添加日志缓存功能

--story=1021118 --user=yufei.hu 【中台】【打点】打点初始化重构,添加日志缓存功能 https://www.tapd.cn/33527076/s/1159704

Signed-off-by: huyufei <yufei.hu@castbox.fm>
dev
胡宇飞 2024-07-25 22:59:47 +08:00
parent 6d5bc2c3a4
commit 85c8058158
40 changed files with 829 additions and 496 deletions

View File

@ -72,7 +72,7 @@ namespace Guru
for (int i = 0; i < _params.Count; i++) for (int i = 0; i < _params.Count; i++)
{ {
// 上报实验AB属性 // 上报实验AB属性
GuruAnalytics.SetUserProperty(_params[i].id, _params[i].group); GuruAnalytics.Instance.SetUserProperty(_params[i].id, _params[i].group);
#if UNITY_EDITOR #if UNITY_EDITOR
Debug.Log($"[AB] --- Add AB Param <color=cyan>{_params[i].ToString()}</color>"); Debug.Log($"[AB] --- Add AB Param <color=cyan>{_params[i].ToString()}</color>");
#else #else

View File

@ -18,7 +18,7 @@ namespace Guru
public const string K_IAP_PURCHASE = "iap_purchase"; // 固定点位事件 public const string K_IAP_PURCHASE = "iap_purchase"; // 固定点位事件
public const string K_SUB_PURCHASE = "sub_purchase"; // 固定点位事件 public const string K_SUB_PURCHASE = "sub_purchase"; // 固定点位事件
private static Action<string> _onSessionSuccessCallback; private static Action<string> _onInitComplete;
private static string _adId = ""; private static string _adId = "";
@ -42,6 +42,9 @@ namespace Guru
return _adjustId; // Adjust AdId; return _adjustId; // Adjust AdId;
} }
} }
private static bool _isReady = false;
public static bool IsReady => _isReady;
#region 启动服务 #region 启动服务
@ -50,9 +53,9 @@ namespace Guru
/// </summary> /// </summary>
/// <param name="appToken"></param> /// <param name="appToken"></param>
/// <param name="fbAppId">MIR 追踪 AppID</param> /// <param name="fbAppId">MIR 追踪 AppID</param>
/// <param name="onSessionSuccess"></param> /// <param name="onInitComplete"></param>
/// <param name="onDeeplinkCallback"></param> /// <param name="onDeeplinkCallback"></param>
public static void StartService(string appToken, string fbAppId = "", Action<string> onSessionSuccess = null, Action<string> onDeeplinkCallback = null) public static void StartService(string appToken, string fbAppId = "", Action<string> onInitComplete = null, Action<string> onDeeplinkCallback = null)
{ {
if (string.IsNullOrEmpty(appToken)) if (string.IsNullOrEmpty(appToken))
{ {
@ -60,7 +63,7 @@ namespace Guru
return; return;
} }
_onSessionSuccessCallback = onSessionSuccess; _onInitComplete = onInitComplete;
InstallEvent(IPMConfig.FIREBASE_ID, IPMConfig.IPM_DEVICE_ID); // 注入启动参数 InstallEvent(IPMConfig.FIREBASE_ID, IPMConfig.IPM_DEVICE_ID); // 注入启动参数
@ -173,7 +176,8 @@ namespace Guru
var adid = sessionSuccessData.Adid; var adid = sessionSuccessData.Adid;
_adjustId = adid; _adjustId = adid;
_onSessionSuccessCallback?.Invoke(adid); _isReady = true;
_onInitComplete?.Invoke(adid);
} }
@ -367,16 +371,16 @@ namespace Guru
/// 广告收入上报 (Adjust 特有的接口) /// 广告收入上报 (Adjust 特有的接口)
/// </summary> /// </summary>
/// <param name="adInfo"></param> /// <param name="adInfo"></param>
public static void TrackADRevenue(MaxSdkBase.AdInfo adInfo) public static void TrackADRevenue(AdImpressionData impressionData)
{ {
if (adInfo == null) if (impressionData == null)
return; return;
var adRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceAppLovinMAX); var adRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceAppLovinMAX);
adRevenue.setRevenue(adInfo.Revenue, "USD"); adRevenue.setRevenue(impressionData.value, "USD");
adRevenue.setAdRevenueNetwork(adInfo.NetworkName); adRevenue.setAdRevenueNetwork(impressionData.ad_source);
adRevenue.setAdRevenueUnit(adInfo.AdUnitIdentifier); adRevenue.setAdRevenueUnit(impressionData.ad_unit_name);
adRevenue.setAdRevenuePlacement(adInfo.Placement); adRevenue.setAdRevenuePlacement(impressionData.ad_placement);
Adjust.trackAdRevenue(adRevenue); Adjust.trackAdRevenue(adRevenue);
} }

View File

@ -18,33 +18,56 @@ namespace Guru
public static readonly string Tag = "[ANU]"; public static readonly string Tag = "[ANU]";
private static readonly string ActionName = "logger_error"; private static readonly string ActionName = "logger_error";
internal const int EventPriorityDefault = 10; private const int EventPriorityDefault = 10;
private static GuruAnalytics _instance;
public static GuruAnalytics Instance
{
get
{
if (_instance == null)
{
throw new Exception("GuruAnalytics not initialized. Please call <Analytics.InitAnalytics()> first.");
}
return _instance;
}
}
private bool _isReady = false;
public bool IsReady => _isReady;
private static IAnalyticsAgent _agent; private IAnalyticsAgent _agent;
private IAnalyticsAgent Agent
public static IAnalyticsAgent Agent
{ {
get get
{ {
if (_agent == null) if (_agent == null)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
_agent = new AnalyticsAgentStub(); _agent = new AnalyticsAgentMock();
#elif UNITY_ANDROID #elif UNITY_ANDROID
_agent = new AnalyticsAgentAndroid(); _agent = new AnalyticsAgentAndroid();
#elif UNITY_IOS #elif UNITY_IOS
_agent = new AnalyticsAgentIOS(); _agent = new AnalyticsAgentIOS();
#endif #endif
} }
if (_agent == null)
{
throw new NotImplementedException("You Should Implement IAnalyticsAgent on platform first.");
}
return _agent; return _agent;
} }
} }
private static Dictionary<string, string> _userProperties; private Dictionary<string, string> _userProperties;
/// <summary> /// <summary>
/// 用户属性缓存字典 /// 用户属性缓存字典
/// </summary> /// </summary>
public static Dictionary<string, string> UserProperties private Dictionary<string, string> UserProperties
{ {
get get
{ {
@ -59,13 +82,13 @@ namespace Guru
/// <summary> /// <summary>
/// 错误 code 表 /// 错误 code 表
/// </summary> /// </summary>
public static List<int> ErrorCodeList = new List<int>(); public List<int> ErrorCodeList = new List<int>();
private static bool _enableErrorLog = false; private bool _enableErrorLog = false;
/// <summary> /// <summary>
/// 启动日志错误上报 /// 启动日志错误上报
/// </summary> /// </summary>
public static bool EnableErrorLog public bool EnableErrorLog
{ {
get => _enableErrorLog; get => _enableErrorLog;
set set
@ -81,35 +104,40 @@ namespace Guru
/// <summary> /// <summary>
/// 初始化接口 /// 初始化接口
/// </summary> /// </summary>
public static void Init(string appId, string deviceInfo, bool isDebug = false, public static void Init(string appId, string deviceInfo, Action onInitComplete, bool isDebug = false,
bool enableErrorLog = false) bool enableErrorLog = false)
{ {
Debug.Log($"{Tag} --- Guru Analytics [{Version}] initialing..."); Debug.Log($"{Tag} --- Guru Analytics [{Version}] initialing...");
_enableErrorLog = enableErrorLog;
Agent?.Init(appId, deviceInfo, isDebug); if (_instance == null)
if(_enableErrorLog) InitCallbacks(); // 激活错误日志回调 {
_instance = new GuruAnalytics();
_instance.Agent.Init(appId, deviceInfo, onInitComplete, isDebug);
_instance.EnableErrorLog = enableErrorLog;
_instance._isReady = true;
}
} }
/// <summary> /// <summary>
/// 设置视图名称 /// 设置视图名称
/// </summary> /// </summary>
/// <param name="screenName"></param> /// <param name="screenName"></param>
public static void SetScreen(string screenName) public void SetScreen(string screenName)
{ {
if (string.IsNullOrEmpty(screenName)) return; if (string.IsNullOrEmpty(screenName)) return;
CacheUserProperty($"screen_name", screenName); CacheUserProperty($"screen_name", screenName);
Agent?.SetScreen(screenName); Agent.SetScreen(screenName);
} }
/// <summary> /// <summary>
/// 设置广告ID /// 设置广告ID
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
public static void SetAdId(string id) public void SetAdId(string id)
{ {
if (string.IsNullOrEmpty(id)) return; if (string.IsNullOrEmpty(id)) return;
CacheUserProperty($"ad_id", id); CacheUserProperty($"ad_id", id);
Agent?.SetAdId(id); Agent.SetAdId(id);
} }
/// <summary> /// <summary>
@ -117,59 +145,59 @@ namespace Guru
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
/// <param name="value"></param> /// <param name="value"></param>
public static void SetUserProperty(string key, string value) public void SetUserProperty(string key, string value)
{ {
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
CacheUserProperty(key, value); // 添加用户属性 CacheUserProperty(key, value); // 添加用户属性
Agent?.SetUserProperty(key, value); Agent.SetUserProperty(key, value);
} }
/// <summary> /// <summary>
/// 设置Firebase ID /// 设置Firebase ID
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
public static void SetFirebaseId(string id) public void SetFirebaseId(string id)
{ {
if (string.IsNullOrEmpty(id)) return; if (string.IsNullOrEmpty(id)) return;
CacheUserProperty($"firebase_id", id); CacheUserProperty($"firebase_id", id);
Agent?.SetFirebaseId(id); Agent.SetFirebaseId(id);
} }
/// <summary> /// <summary>
/// 设置Adjust ID /// 设置Adjust ID
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
public static void SetAdjustId(string id) public void SetAdjustId(string id)
{ {
if (string.IsNullOrEmpty(id)) return; if (string.IsNullOrEmpty(id)) return;
CacheUserProperty($"adjust_id", id); CacheUserProperty($"adjust_id", id);
Agent?.SetAdjustId(id); Agent.SetAdjustId(id);
} }
/// <summary> /// <summary>
/// 设置设备ID /// 设置设备ID
/// </summary> /// </summary>
/// <param name="deviceId"></param> /// <param name="deviceId"></param>
public static void SetDeviceId(string deviceId) public void SetDeviceId(string deviceId)
{ {
if (string.IsNullOrEmpty(deviceId)) return; if (string.IsNullOrEmpty(deviceId)) return;
CacheUserProperty($"device_id", deviceId); CacheUserProperty($"device_id", deviceId);
Agent?.SetDeviceId(deviceId); Agent.SetDeviceId(deviceId);
} }
public static void SetAndroidID(string androidId) public void SetAndroidID(string androidId)
{ {
if (string.IsNullOrEmpty(androidId)) return; if (string.IsNullOrEmpty(androidId)) return;
CacheUserProperty(Analytics.PropertyAndroidID, androidId); CacheUserProperty(Analytics.PropertyAndroidID, androidId);
} }
public static void SetIDFV(string idfv) public void SetIDFV(string idfv)
{ {
if (string.IsNullOrEmpty(idfv)) return; if (string.IsNullOrEmpty(idfv)) return;
CacheUserProperty(Analytics.PropertyIDFV, idfv); CacheUserProperty(Analytics.PropertyIDFV, idfv);
} }
public static void SetIDFA(string idfa) public void SetIDFA(string idfa)
{ {
if (string.IsNullOrEmpty(idfa)) return; if (string.IsNullOrEmpty(idfa)) return;
CacheUserProperty(Analytics.PropertyIDFA, idfa); CacheUserProperty(Analytics.PropertyIDFA, idfa);
@ -180,17 +208,17 @@ namespace Guru
/// 设置用户ID /// 设置用户ID
/// </summary> /// </summary>
/// <param name="uid"></param> /// <param name="uid"></param>
public static void SetUid(string uid) public void SetUid(string uid)
{ {
if (string.IsNullOrEmpty(uid)) return; if (string.IsNullOrEmpty(uid)) return;
CacheUserProperty($"uid", uid); CacheUserProperty($"uid", uid);
Agent?.SetUid(uid); Agent.SetUid(uid);
} }
/// <summary> /// <summary>
/// 上报事件成功率 /// 上报事件成功率
/// </summary> /// </summary>
public static void ReportEventSuccessRate() => Agent?.ReportEventSuccessRate(); public void ReportEventSuccessRate() => Agent.ReportEventSuccessRate();
/// <summary> /// <summary>
/// 上报打点事件 /// 上报打点事件
@ -198,7 +226,7 @@ namespace Guru
/// <param name="eventName">事件名称</param> /// <param name="eventName">事件名称</param>
/// <param name="data">INT类型的值</param> /// <param name="data">INT类型的值</param>
/// <param name="priority"></param> /// <param name="priority"></param>
public static void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1) public void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1)
{ {
string raw = ""; string raw = "";
if (data != null && data.Count > 0) if (data != null && data.Count > 0)
@ -207,9 +235,10 @@ namespace Guru
} }
if (priority < 0) priority = EventPriorityDefault; if (priority < 0) priority = EventPriorityDefault;
Debug.Log($"{Tag} --- LogEvent GuruAnalytics:{eventName} | raw: {raw} | priority: {priority}"); Debug.Log($"{Tag} --- LogEvent GuruAnalytics:{eventName} | raw: {raw} | priority: {priority}");
Agent?.LogEvent(eventName, raw, priority); Agent.LogEvent(eventName, raw, priority);
} }
/*
private static string BuildParamsString(Dictionary<string, dynamic> data) private static string BuildParamsString(Dictionary<string, dynamic> data)
{ {
string raw = ""; string raw = "";
@ -221,8 +250,9 @@ namespace Guru
} }
return raw; return raw;
} }
*/
private static string BuildParamsJson(Dictionary<string, dynamic> data) private string BuildParamsJson(Dictionary<string, dynamic> data)
{ {
try try
{ {
@ -240,6 +270,7 @@ namespace Guru
return ""; return "";
} }
/*
/// <summary> /// <summary>
/// 构建带有类型格式的Str值 /// 构建带有类型格式的Str值
/// </summary> /// </summary>
@ -260,15 +291,16 @@ namespace Guru
return $"{kvp.Key}:s{kvp.Value}"; return $"{kvp.Key}:s{kvp.Value}";
} }
*/
/// <summary> /// <summary>
/// 设置太极02值 /// 设置太极02值
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
public static void SetTch02Value(double value) public void SetTch02Value(double value)
{ {
Debug.Log($"{Tag} set tch_02_value:{value}"); Debug.Log($"{Tag} set tch_02_value:{value}");
Agent?.SetTch02Value(value); Agent.SetTch02Value(value);
} }
#endregion #endregion
@ -289,37 +321,19 @@ namespace Guru
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
/// <param name="value"></param> /// <param name="value"></param>
private static void CacheUserProperty(string key, string value) private void CacheUserProperty(string key, string value)
{ {
bool needUpdate = !UserProperties.ContainsKey(key) || UserProperties[key] != value; // bool needUpdate = !UserProperties.ContainsKey(key) || UserProperties[key] != value;
UserProperties[key] = value; UserProperties[key] = value;
// if (needUpdate) UpdateAllUserProperties(); // if (needUpdate) UpdateAllUserProperties();
} }
private static void UpdateAllUserProperties()
{
if (UserProperties != null && UserProperties.Count > 0)
{
var keys = UserProperties.Keys.ToArray();
int i = 0;
string key = "";
while (i < keys.Length)
{
key = keys[i];
if(!string.IsNullOrEmpty(key)) SetUserProperty(key, UserProperties[key]);
i++;
}
keys = null;
}
}
#endregion #endregion
#region 日志回调 #region 日志回调
private static void InitCallbacks() private void InitCallbacks()
{ {
try try
{ {
@ -339,8 +353,8 @@ namespace Guru
/// <summary> /// <summary>
/// 获取SDK回调 /// 获取SDK回调
/// </summary> /// </summary>
/// <param name="msg"></param> /// <param name="raw"></param>
private static void OnSDKCallback(string raw) private void OnSDKCallback(string raw)
{ {
if (string.IsNullOrEmpty(raw)) return; if (string.IsNullOrEmpty(raw)) return;
if (!raw.Contains($"\"{ActionName}\"")) return; // 不对其他行为的日志进行过滤 if (!raw.Contains($"\"{ActionName}\"")) return; // 不对其他行为的日志进行过滤
@ -352,7 +366,7 @@ namespace Guru
/// </summary> /// </summary>
/// <param name="code"></param> /// <param name="code"></param>
/// <param name="errorInfo"></param> /// <param name="errorInfo"></param>
private static void OnLoggerErrorEvent(int code, string errorInfo = "") private void OnLoggerErrorEvent(int code, string errorInfo = "")
{ {
// Debug.Log($"{Tag} --- OnLoggerErrorEvent: code:{code}\tinfo:{errorInfo}"); // Debug.Log($"{Tag} --- OnLoggerErrorEvent: code:{code}\tinfo:{errorInfo}");
@ -381,17 +395,13 @@ namespace Guru
parameters["err"] = errorInfo; parameters["err"] = errorInfo;
Debug.Log($"{Tag} ------ ErrorLogInfo:: code:{codeString}\tinfo:{errorInfo}"); Debug.Log($"{Tag} ------ ErrorLogInfo:: code:{codeString}\tinfo:{errorInfo}");
#if !UNITY_EDITOR
// Only for firebase GA // Only for firebase GA
Analytics.LogEvent("dev_audit", parameters, Analytics.TrackEvent("dev_audit", parameters, new Analytics.EventSetting() { EnableFirebaseAnalytics = true });
new Analytics.EventSetting() { EnableFirebaseAnalytics = true });
#endif
} }
private static bool ParseWithJson(string json) private bool ParseWithJson(string json)
{ {
Debug.Log($"{Tag} ------ ParseWithJson: json:\n{json}"); Debug.Log($"{Tag} ------ ParseWithJson: json:\n{json}");
@ -428,7 +438,7 @@ namespace Guru
{ {
string p = "\"msg\":\""; string p = "\"msg\":\"";
string m = json; string m = json;
if (json.Contains(p)) m = json.Substring(json.IndexOf(p) + p.Length); if (json.Contains(p)) m = json.Substring(json.IndexOf(p, StringComparison.Ordinal) + p.Length);
info = $"JsonEX:{m}"; info = $"JsonEX:{m}";
// Debug.Log($"{Tag} --- {info}"); // Debug.Log($"{Tag} --- {info}");
Analytics.LogCrashlytics(json, false); Analytics.LogCrashlytics(json, false);
@ -438,22 +448,17 @@ namespace Guru
return false; return false;
} }
private static void ParseWithRaw(string raw) /**
private void ParseWithRaw(string raw)
{ {
int code = (int)AnalyticsCode.Unknown; var code = (int)AnalyticsCode.Unknown;
string info = raw; string info;
//------- message send to unity ---------- //------- message send to unity ----------
Debug.Log($"{Tag} get callback errorInfo:\n{raw}"); Debug.Log($"{Tag} get callback errorInfo:\n{raw}");
string patten = "";
string patten2 = "";
int idx = 0;
int idx2 = 0;
int len = 0;
var patten = "msg\":\"";
patten = "msg\":\"";
if (raw.Contains(patten)) if (raw.Contains(patten))
{ {
@ -476,16 +481,16 @@ namespace Guru
try try
{ {
idx = raw.IndexOf(patten, StringComparison.Ordinal) + patten.Length; var idx = raw.IndexOf(patten, StringComparison.Ordinal) + patten.Length;
string act = raw.Substring(idx, ActionName.Length); string act = raw.Substring(idx, ActionName.Length);
if (act == ActionName) if (act == ActionName)
{ {
patten = "code\":"; patten = "code\":";
patten2 = ",\"msg"; var patten2 = ",\"msg";
idx = raw.IndexOf(patten); idx = raw.IndexOf(patten, StringComparison.Ordinal);
idx2 = raw.IndexOf(patten2); var idx2 = raw.IndexOf(patten2, StringComparison.Ordinal);
len = idx2 - (idx + patten.Length); var len = idx2 - (idx + patten.Length);
if (len > 0) if (len > 0)
{ {
string c = raw.Substring(idx + patten.Length, len); string c = raw.Substring(idx + patten.Length, len);
@ -524,12 +529,12 @@ namespace Guru
{ {
Analytics.LogCrashlytics(raw, false); Analytics.LogCrashlytics(raw, false);
Analytics.LogCrashlytics($"{Tag} --- format error:{raw}"); Analytics.LogCrashlytics($"{Tag} --- format error:{raw}");
OnLoggerErrorEvent(code, raw.Substring(raw.IndexOf("msg\":" ) + 5)); OnLoggerErrorEvent(code, raw.Substring(raw.IndexOf("msg\":", StringComparison.Ordinal) + 5));
} }
} }
**/
private void ReportCodeInfo(int code, string info)
private static void ReportCodeInfo(int code, string info)
{ {
var ac = (AnalyticsCode)code; var ac = (AnalyticsCode)code;
Debug.Log($"{Tag} ------ Get Code And Info: code:{code}[{ac}] \tinfo:{info}"); Debug.Log($"{Tag} ------ Get Code And Info: code:{code}[{ac}] \tinfo:{info}");
@ -585,7 +590,7 @@ namespace Guru
public static void TestOnCallback(string msg) public static void TestOnCallback(string msg)
{ {
OnSDKCallback(msg); Instance.OnSDKCallback(msg);
} }
#endif #endif

View File

@ -1,11 +1,13 @@
namespace Guru namespace Guru
{ {
using System;
/// <summary> /// <summary>
/// 自打点代理接口 /// 自打点代理接口
/// </summary> /// </summary>
public interface IAnalyticsAgent public interface IAnalyticsAgent
{ {
void Init(string appId, string deviceInfo, bool isDebug = false); void Init(string appId, string deviceInfo, Action onInitComplete, bool isDebug = false);
void SetScreen(string screenName); void SetScreen(string screenName);
void SetAdId(string id); void SetAdId(string id);
void SetUserProperty(string key, string value); void SetUserProperty(string key, string value);

View File

@ -1,5 +1,7 @@
using System.Threading.Tasks;
namespace Guru namespace Guru
{ {
using System; using System;
@ -76,12 +78,16 @@ namespace Guru
#region 接口实现 #region 接口实现
public void Init(string appId, string deviceInfo, bool isDebug = false) public async void Init(string appId, string deviceInfo, Action onInitComplete, bool isDebug = false)
{ {
_isDebug = isDebug; _isDebug = isDebug;
string bundleId = Application.identifier; string bundleId = Application.identifier;
// public static void init(String appId, String deviceInfo, String bundleId, boolean isDebug, boolean useWorker, boolean useCronet, String baseUrl) // public static void init(String appId, String deviceInfo, String bundleId, boolean isDebug, boolean useWorker, boolean useCronet, String baseUrl)
// TODO: 将来把 CallStatic 转为异步实现
CallStatic("init", appId, deviceInfo, bundleId, isDebug, UseWorker, UseCronet, BaseUrl); // 调用接口 CallStatic("init", appId, deviceInfo, bundleId, isDebug, UseWorker, UseCronet, BaseUrl); // 调用接口
onInitComplete?.Invoke();
} }
public void SetScreen(string screenName) public void SetScreen(string screenName)

View File

@ -1,7 +1,9 @@
using System.Runtime.InteropServices;
namespace Guru namespace Guru
{ {
using System;
using System.Runtime.InteropServices;
public class AnalyticsAgentIOS: IAnalyticsAgent public class AnalyticsAgentIOS: IAnalyticsAgent
{ {
@ -55,13 +57,14 @@ namespace Guru
} }
public void Init(string appId, string deviceInfo, bool isDebug = false) public void Init(string appId, string deviceInfo, Action onInitComplete, bool isDebug = false)
{ {
_isDebug = isDebug; _isDebug = isDebug;
#if UNITY_IOS #if UNITY_IOS
unityInitAnalytics(appId, deviceInfo, isDebug); unityInitAnalytics(appId, deviceInfo, isDebug);
unityInitException(); // 初始化报错守护进程 unityInitException(); // 初始化报错守护进程
#endif #endif
onInitComplete?.Invoke();
} }
public void SetScreen(string screenName) public void SetScreen(string screenName)

View File

@ -1,9 +1,12 @@
using System.Text;
using UnityEngine;
namespace Guru namespace Guru
{ {
public class AnalyticsAgentStub: IAnalyticsAgent using System.Text;
using UnityEngine;
using System;
public class AnalyticsAgentMock: IAnalyticsAgent
{ {
public static readonly string TAG = "[EDT]"; public static readonly string TAG = "[EDT]";
@ -24,7 +27,7 @@ namespace Guru
} }
public void Init(string appId, string deviceInfo, bool isDebug = false) public void Init(string appId, string deviceInfo, Action onInitComplete, bool isDebug = false)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
_isShowLog = true; _isShowLog = true;
@ -32,6 +35,8 @@ namespace Guru
_isDebug = isDebug; _isDebug = isDebug;
if(_isShowLog) if(_isShowLog)
Debug.Log($"{TAG} init with Debug: <color=orange>{isDebug}</color> appId:{appId} deviceInfo:{deviceInfo}"); Debug.Log($"{TAG} init with Debug: <color=orange>{isDebug}</color> appId:{appId} deviceInfo:{deviceInfo}");
onInitComplete?.Invoke();
} }

View File

@ -5,7 +5,7 @@ using UnityEngine.UI;
namespace Guru namespace Guru
{ {
public class CuruAnalyticsDemo: MonoBehaviour public class GuruAnalyticsDemo: MonoBehaviour
{ {
[SerializeField] private bool _isDebug = true; [SerializeField] private bool _isDebug = true;
[SerializeField] private Button _btnInitSDK; [SerializeField] private Button _btnInitSDK;
@ -54,17 +54,22 @@ namespace Guru
private void OnClickInit() private void OnClickInit()
{ {
Debug.Log($"---- [DEMO] Call Analytics init"); Debug.Log($"---- [DEMO] Call Analytics init");
GuruAnalytics.Init(AppID, DeviceInfo, _isDebug); GuruAnalytics.Init(AppID, DeviceInfo, OnGuruAnalyticsInitComplete, _isDebug);
} }
private void OnGuruAnalyticsInitComplete()
{
}
private void OnClickStatus() private void OnClickStatus()
{ {
Debug.Log($"---- [DEMO] Report Stats IDs: UID:{UID} DeviceID:{DeviceID} FirebaseID:{FirebaseID} AdID:{AdID} AdjustID:{AdjustID}"); Debug.Log($"---- [DEMO] Report Stats IDs: UID:{UID} DeviceID:{DeviceID} FirebaseID:{FirebaseID} AdID:{AdID} AdjustID:{AdjustID}");
GuruAnalytics.SetUid(UID); GuruAnalytics.Instance.SetUid(UID);
GuruAnalytics.SetDeviceId(DeviceID); GuruAnalytics.Instance.SetDeviceId(DeviceID);
GuruAnalytics.SetFirebaseId(FirebaseID); GuruAnalytics.Instance.SetFirebaseId(FirebaseID);
GuruAnalytics.SetAdId(AdID); GuruAnalytics.Instance.SetAdId(AdID);
GuruAnalytics.SetAdjustId(AdjustID); GuruAnalytics.Instance.SetAdjustId(AdjustID);
} }
private void OnClickUserProperties() private void OnClickUserProperties()
@ -72,14 +77,14 @@ namespace Guru
string item_category = "main"; string item_category = "main";
int level = 7; int level = 7;
Debug.Log($"---- [DEMO] Call SetUserProperty: item_category:{item_category} level:{level}"); Debug.Log($"---- [DEMO] Call SetUserProperty: item_category:{item_category} level:{level}");
GuruAnalytics.SetUserProperty("item_category", item_category); GuruAnalytics.Instance.SetUserProperty("item_category", item_category);
GuruAnalytics.SetUserProperty("level", level.ToString()); GuruAnalytics.Instance.SetUserProperty("level", level.ToString());
} }
private void OnClickEvents() private void OnClickEvents()
{ {
Debug.Log($"---- [DEMO] Report Screen: {ScreenName}"); Debug.Log($"---- [DEMO] Report Screen: {ScreenName}");
GuruAnalytics.SetScreen(ScreenName); GuruAnalytics.Instance.SetScreen(ScreenName);
string eventName = "user_get_coin"; string eventName = "user_get_coin";
Dictionary<string, dynamic> data = new Dictionary<string, dynamic>() Dictionary<string, dynamic> data = new Dictionary<string, dynamic>()
@ -99,19 +104,19 @@ namespace Guru
Debug.Log(s); Debug.Log(s);
Debug.Log($"---- [DEMO] Call LogEvent"); Debug.Log($"---- [DEMO] Call LogEvent");
GuruAnalytics.LogEvent(eventName, data); GuruAnalytics.Instance.LogEvent(eventName, data);
} }
private void OnClickEvents2() private void OnClickEvents2()
{ {
string eventName = "user_data_loaded"; string eventName = "user_data_loaded";
GuruAnalytics.LogEvent(eventName); GuruAnalytics.Instance.LogEvent(eventName);
} }
private void OnClickReport() private void OnClickReport()
{ {
GuruAnalytics.ReportEventSuccessRate(); GuruAnalytics.Instance.ReportEventSuccessRate();
} }

View File

@ -140,7 +140,7 @@ namespace Guru
DmaResult = result; DmaResult = result;
//----------- Guru Analytics report --------------- //----------- Guru Analytics report ---------------
Analytics.LogEvent("dma_gg", new Dictionary<string, dynamic>() Analytics.TrackEvent("dma_gg", new Dictionary<string, dynamic>()
{ {
{ "purpose", purposeStr }, { "purpose", purposeStr },
{ "result", result } { "result", result }

View File

@ -1,3 +1,5 @@
using System.Linq;
namespace Guru namespace Guru
{ {
using System; using System;
@ -18,7 +20,7 @@ namespace Guru
} }
} }
protected static readonly string Tag = "[Ads]"; protected static readonly string Tag = "[SDK][ADS]";
public bool IsInitialized => MaxSdk.IsInitialized() || _isServiceStarted; public bool IsInitialized => MaxSdk.IsInitialized() || _isServiceStarted;
protected bool IsNetworkEnabled => Application.internetReachability != NetworkReachability.NotReachable; protected bool IsNetworkEnabled => Application.internetReachability != NetworkReachability.NotReachable;
@ -40,7 +42,10 @@ namespace Guru
public static Action OnRewardLoaded; public static Action OnRewardLoaded;
public static Action OnRewardFailed; public static Action OnRewardFailed;
public static Action OnRewardClosed; public static Action OnRewardClosed;
private Dictionary<string, string> _reviewCreativeIds = new Dictionary<string, string>(10); // Creative ID 缓存: Cid : RCid
private Dictionary<string, AdImpressionData> _impressionCache = new Dictionary<string, AdImpressionData>(10);
protected AdsModel _model; protected AdsModel _model;
protected AdsInitSpec _initSpec = null; protected AdsInitSpec _initSpec = null;
@ -89,10 +94,12 @@ namespace Guru
MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent; MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnBannerRevenuePaidEvent; MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnBannerRevenuePaidEvent;
MaxSdkCallbacks.MRec.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent; MaxSdkCallbacks.MRec.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
//--------------- Banner 回调 ----------------- //--------------- Banner 回调 -----------------
MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerLoadedEvent; MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerLoadedEvent;
MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerFailedEvent; MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerFailedEvent;
MaxSdkCallbacks.Banner.OnAdClickedEvent += OnBannerClickedEvent; MaxSdkCallbacks.Banner.OnAdClickedEvent += OnBannerClickedEvent;
MaxSdkCallbacks.Banner.OnAdReviewCreativeIdGeneratedEvent += OnAdReviewCreativeIdGeneratedEvent;
//--------------- IV 回调 ----------------- //--------------- IV 回调 -----------------
MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent; MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialFailedEvent; MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialFailedEvent;
@ -100,6 +107,7 @@ namespace Guru
MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickEvent; MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickEvent;
MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayEvent; MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayEvent;
MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialDismissedEvent; MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialDismissedEvent;
MaxSdkCallbacks.Interstitial.OnAdReviewCreativeIdGeneratedEvent += OnAdReviewCreativeIdGeneratedEvent;
//--------------- RV 回调 ----------------- //--------------- RV 回调 -----------------
MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent; MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdFailedEvent; MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdFailedEvent;
@ -108,7 +116,12 @@ namespace Guru
MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent; MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdDismissedEvent; MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdDismissedEvent;
MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent; MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
MaxSdkCallbacks.Rewarded.OnAdReviewCreativeIdGeneratedEvent += OnAdReviewCreativeIdGeneratedEvent;
//--------------- Creative 回调 -----------------
//-------------- SDK 初始化 ------------------- //-------------- SDK 初始化 -------------------
MaxSdk.SetExtraParameter("enable_black_screen_fixes", "true"); // 修复黑屏 MaxSdk.SetExtraParameter("enable_black_screen_fixes", "true"); // 修复黑屏
} }
@ -174,7 +187,7 @@ namespace Guru
#endregion #endregion
#region ILRD #region 收益打点
private double TchAD001RevValue private double TchAD001RevValue
{ {
@ -188,25 +201,46 @@ namespace Guru
set => _model.TchAD02RevValue = value; set => _model.TchAD02RevValue = value;
} }
public void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) /// <summary>
/// 上报广告收益
/// </summary>
/// <param name="adInfo"></param>
/// <param name="reviewedCreativeId"></param>
private void ReportAdsRevenue(AdImpressionData impressionData)
{
// #1 ad_impression
Analytics.ADImpression(impressionData);
// #2 tch_001
double revenue = impressionData.value;
CalcTaichi001Value(revenue);
CalcTaichi02Value(revenue);
// #3 adjust_ad_revenue
AdjustService.TrackADRevenue(impressionData);
}
private void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{ {
if (adInfo == null) return; if (adInfo == null) return;
Debug.Log( $"{Tag} --- [Revenue] > [{adInfo.AdFormat}] --- adUnitId:{adUnitId}: UnitID:{adInfo.AdUnitIdentifier} Revenue:{adInfo.Revenue} CreativeId:{adInfo.CreativeIdentifier}");
try try
{ {
Log.I( $"[ADRevenue] - adUnitId:{adUnitId}, Revenue:{adInfo?.Revenue : 0}"); if (_reviewCreativeIds.TryGetValue(adInfo.CreativeIdentifier, out var reviewCreativeId))
{
// #1 ad_impression // 找到 RCid 的话则直接上报广告收益
OnAdImpression(adInfo); TryReportImpression(adInfo, reviewCreativeId);
}
// #2 tch_001 else
double revenue = adInfo.Revenue; {
CalcTaichi001Value(revenue); // 找不到的话会缓存 adInfo, 等待获取 RCid
CalcTaichi02Value(revenue); _impressionCache[adInfo.CreativeIdentifier] = CreateImpressionData(adInfo, "", Analytics.AdMAX);
}
// #3 adjust_ad_revenue
AdjustService.TrackADRevenue(adInfo);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -216,13 +250,30 @@ namespace Guru
} }
/// <summary> /// <summary>
/// 广告ARO收益打点 /// 构建 Impression 数据
/// </summary> /// </summary>
/// <param name="adInfo"></param> /// <param name="adInfo"></param>
/// <param name="reviewCreativeId"></param>
/// <param name="platform"></param> /// <param name="platform"></param>
private void OnAdImpression(MaxSdkBase.AdInfo adInfo, string platform = "") /// <returns></returns>
private AdImpressionData CreateImpressionData(MaxSdkBase.AdInfo adInfo, string reviewCreativeId = "", string platform = "")
{ {
Analytics.ADImpression(adInfo, platform); if (string.IsNullOrEmpty(platform)) platform = Analytics.AdMAX;
var impression = new AdImpressionData()
{
value = adInfo.Revenue,
currency = Analytics.USD,
ad_platform = platform,
ad_format = adInfo.AdFormat,
ad_unit_name = adInfo.AdUnitIdentifier,
ad_source = adInfo.NetworkName,
ad_placement = adInfo.NetworkPlacement,
ad_creative_id = adInfo.CreativeIdentifier,
review_creative_id = reviewCreativeId
};
return impression;
} }
@ -386,6 +437,7 @@ namespace Guru
// --- fixed by Yufei 2024-5-29 为 don't report bads_loaded any more. --- // --- fixed by Yufei 2024-5-29 为 don't report bads_loaded any more. ---
// Analytics.ADBadsLoaded(AdParams.Build(adUnitId, adInfo, // Analytics.ADBadsLoaded(AdParams.Build(adUnitId, adInfo,
// duration: GetAdsLoadDuration(ref _badsloadStartTime), category: _badsCategory)); // duration: GetAdsLoadDuration(ref _badsloadStartTime), category: _badsCategory));
Debug.Log( $"[SDK][Ads][Loaded] --- adUnitId:{adUnitId} Revenue:{adInfo.Revenue} Type:{adInfo.AdFormat} CreativeId:{adInfo.CreativeIdentifier}");
OnBadsLoaded(); OnBadsLoaded();
} }
@ -509,6 +561,8 @@ namespace Guru
duration: GetAdsLoadDuration(ref _iadsLoadStartTime), category: _iadsCategory)); duration: GetAdsLoadDuration(ref _iadsLoadStartTime), category: _iadsCategory));
_interstitialRetryAttempt = 0; _interstitialRetryAttempt = 0;
Debug.Log( $"[SDK][Ads][Loaded] --- adUnitId:{adUnitId} Revenue:{adInfo.Revenue} Type:{adInfo.AdFormat} CreativeId:{adInfo.CreativeIdentifier}");
OnInterstitialLoaded?.Invoke(); OnInterstitialLoaded?.Invoke();
} }
@ -660,6 +714,8 @@ namespace Guru
Analytics.ADRadsLoaded(AdParams.Build(adUnitId, Analytics.ADRadsLoaded(AdParams.Build(adUnitId,
duration: GetAdsLoadDuration(ref _radsLoadStartTime), category: _iadsCategory)); duration: GetAdsLoadDuration(ref _radsLoadStartTime), category: _iadsCategory));
_rewardRetryAttempt = 0; _rewardRetryAttempt = 0;
Debug.Log( $"[SDK][Ads][Loaded] --- adUnitId:{adUnitId} Revenue:{adInfo.Revenue} Type:{adInfo.AdFormat} CreativeId:{adInfo.CreativeIdentifier}");
OnRewardLoaded?.Invoke(); OnRewardLoaded?.Invoke();
} }
@ -782,5 +838,78 @@ namespace Guru
} }
#endregion #endregion
#region CreativeID
/// <summary>
/// 获取 AdReviewCreativeId
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="reviewCreativeId"></param>
/// <param name="adInfo"></param>
private void OnAdReviewCreativeIdGeneratedEvent(string adUnitId, string reviewCreativeId, MaxSdkBase.AdInfo adInfo)
{
Debug.Log($"{Tag} --- ReviewCreativeId:{reviewCreativeId} adUnitId: {adUnitId} Type:{adInfo?.AdFormat ?? "NULL"} CreativeId: {adInfo?.CreativeIdentifier ?? "NULL"} Revenue:{adInfo.Revenue}");
if (adInfo == null || string.IsNullOrEmpty(adInfo.CreativeIdentifier))
{
Debug.LogError($"{Tag} --- Get ReviewCreativeId:{reviewCreativeId} but CreativeIdentifier is null");
return;
}
if (_reviewCreativeIds == null) _reviewCreativeIds = new Dictionary<string, string>(10);
// 尝试直接上报广告收益 (可能存在异步上报的情况)
if (!TryReportImpression(adInfo, reviewCreativeId))
{
_reviewCreativeIds[adInfo.CreativeIdentifier] = reviewCreativeId; // 如果上报未成功, 则缓存reviewCreativeId
}
}
/// <summary>
/// 尝试上报数据
/// </summary>
/// <param name="adInfo"></param>
/// <param name="reviewCreativeId"></param>
/// <returns></returns>
private bool TryReportImpression(MaxSdk.AdInfo adInfo, string reviewCreativeId = "")
{
string creativeId = adInfo.CreativeIdentifier;
bool result = false;
List<string> removeList = new List<string>();
if (_impressionCache.TryGetValue(creativeId, out var impressionData))
{
if(string.IsNullOrEmpty(impressionData.review_creative_id))
impressionData.review_creative_id = reviewCreativeId;
ReportAdsRevenue(impressionData);
removeList.Add(creativeId);
result = true;
}
// 清理超过 3 秒未上报的数据
foreach (var imp in _impressionCache.Values)
{
if (imp.GetPassedSecond() > 3.0)
{
ReportAdsRevenue(imp);
removeList.Add(imp.ad_creative_id);
}
}
if (removeList.Count > 0)
{
foreach (var k in removeList)
{
_impressionCache.Remove(k);
_reviewCreativeIds.Remove(k);
}
}
return result;
}
#endregion
} }
} }

View File

@ -0,0 +1,40 @@
namespace Guru
{
using System;
/// <summary>
/// AdImpression 对象
/// </summary>
public class AdImpressionData
{
public double value;
public string currency = "USD";
public string ad_platform = "MAX";
public string ad_source;
public string ad_format;
public string ad_unit_name;
public string ad_placement;
public string ad_creative_id;
public string review_creative_id;
public DateTime createTime;
public AdImpressionData()
{
createTime = DateTime.UtcNow;
}
/// <summary>
/// 获取自创建开始经过的秒数
/// </summary>
/// <returns></returns>
public double GetPassedSecond()
{
return (DateTime.UtcNow - createTime).TotalSeconds;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1aca2a4507b94dfe9f88040125708edb
timeCreated: 1721883318

View File

@ -22,7 +22,7 @@ namespace Guru
} }
} }
private static AdjustEvent CreateAdjustEvent(string eventName) internal static AdjustEvent CreateAdjustEvent(string eventName)
{ {
string tokenID = GetAdjustEventToken(eventName); string tokenID = GetAdjustEventToken(eventName);
if (string.IsNullOrEmpty(tokenID)) if (string.IsNullOrEmpty(tokenID))

View File

@ -59,26 +59,26 @@ namespace Guru
//---------------------- BANNER ------------------------- //---------------------- BANNER -------------------------
public static void ADBadsLoad(AdParams adParams) public static void ADBadsLoad(AdParams adParams)
{ {
LogEvent(EventBadsLoad, BuildAdEventData(adParams)); TrackEvent(EventBadsLoad, BuildAdEventData(adParams));
} }
public static void ADBadsLoaded(AdParams adParams) public static void ADBadsLoaded(AdParams adParams)
{ {
LogEvent(EventBadsLoaded, BuildAdEventData(adParams)); TrackEvent(EventBadsLoaded, BuildAdEventData(adParams));
} }
public static void ADBadsFailed(AdParams adParams) public static void ADBadsFailed(AdParams adParams)
{ {
LogEvent(EventBadsFailed, BuildAdEventData(adParams)); TrackEvent(EventBadsFailed, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
/// 广告点击 /// 广告点击
/// </summary> /// </summary>
public static void ADBadsClick(AdParams adParams) public static void ADBadsClick(AdParams adParams)
{ {
LogEvent(EventBadsClick, BuildAdEventData(adParams)); TrackEvent(EventBadsClick, BuildAdEventData(adParams));
} }
public static void ADBadsImp(AdParams adParams) public static void ADBadsImp(AdParams adParams)
{ {
LogEvent(EventBadsImp, BuildAdEventData(adParams)); TrackEvent(EventBadsImp, BuildAdEventData(adParams));
} }
public static void ADBadsHide( int loadedTimes, int failTimes) public static void ADBadsHide( int loadedTimes, int failTimes)
@ -88,7 +88,7 @@ namespace Guru
["loaded_times"] = loadedTimes, ["loaded_times"] = loadedTimes,
["fail_times"] = failTimes ["fail_times"] = failTimes
}; };
LogEvent(EventBadsHide, dict); TrackEvent(EventBadsHide, dict);
} }
//---------------------- INTERSTITIAL ------------------------- //---------------------- INTERSTITIAL -------------------------
/// <summary> /// <summary>
@ -96,7 +96,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsLoad(AdParams adParams) public static void ADIadsLoad(AdParams adParams)
{ {
LogEvent(EventIadsLoad, BuildAdEventData(adParams)); TrackEvent(EventIadsLoad, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -104,7 +104,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsLoaded(AdParams adParams) public static void ADIadsLoaded(AdParams adParams)
{ {
LogEvent(EventIadsLoaded, BuildAdEventData(adParams)); TrackEvent(EventIadsLoaded, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -112,7 +112,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsFailed(AdParams adParams) public static void ADIadsFailed(AdParams adParams)
{ {
LogEvent(EventIadsFailed, BuildAdEventData(adParams)); TrackEvent(EventIadsFailed, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -120,7 +120,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsImp(AdParams adParams) public static void ADIadsImp(AdParams adParams)
{ {
LogEvent(EventIadsImp, BuildAdEventData(adParams)); TrackEvent(EventIadsImp, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -128,7 +128,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsClick(AdParams adParams) public static void ADIadsClick(AdParams adParams)
{ {
LogEvent(EventIadsClick, BuildAdEventData(adParams)); TrackEvent(EventIadsClick, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -136,7 +136,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADIadsClose(AdParams adParams) public static void ADIadsClose(AdParams adParams)
{ {
LogEvent(EventIadsClose, BuildAdEventData(adParams)); TrackEvent(EventIadsClose, BuildAdEventData(adParams));
} }
//---------------------- REWARDS ------------------------- //---------------------- REWARDS -------------------------
@ -145,28 +145,28 @@ namespace Guru
/// </summary> /// </summary>
public static void ADRadsLoad(AdParams adParams) public static void ADRadsLoad(AdParams adParams)
{ {
LogEvent(EventRadsLoad, BuildAdEventData(adParams)); TrackEvent(EventRadsLoad, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
/// 广告拉取成功 /// 广告拉取成功
/// </summary> /// </summary>
public static void ADRadsLoaded(AdParams adParams) public static void ADRadsLoaded(AdParams adParams)
{ {
LogEvent(EventRadsLoaded, BuildAdEventData(adParams)); TrackEvent(EventRadsLoaded, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
/// 广告拉取失败 /// 广告拉取失败
/// </summary> /// </summary>
public static void ADRadsFailed(AdParams adParams) public static void ADRadsFailed(AdParams adParams)
{ {
LogEvent(EventRadsFailed, BuildAdEventData(adParams)); TrackEvent(EventRadsFailed, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
/// 广告展示 /// 广告展示
/// </summary> /// </summary>
public static void ADRadsImp(AdParams adParams) public static void ADRadsImp(AdParams adParams)
{ {
LogEvent(EventRadsImp, BuildAdEventData(adParams)); TrackEvent(EventRadsImp, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
/// 广告完成观看发奖励 /// 广告完成观看发奖励
@ -174,7 +174,7 @@ namespace Guru
public static void ADRadsRewarded(AdParams adParams) public static void ADRadsRewarded(AdParams adParams)
{ {
var data = BuildAdEventData(adParams); var data = BuildAdEventData(adParams);
LogEvent(EventRadsRewarded, data); TrackEvent(EventRadsRewarded, data);
if (RadsRewardCount == 0) if (RadsRewardCount == 0)
{ {
@ -195,7 +195,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADRadsFirstRewarded(Dictionary<string, object> data) public static void ADRadsFirstRewarded(Dictionary<string, object> data)
{ {
LogEvent(EventFirstRadsRewarded, data); TrackEvent(EventFirstRadsRewarded, data);
} }
/// <summary> /// <summary>
@ -203,7 +203,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADRadsClick(AdParams adParams) public static void ADRadsClick(AdParams adParams)
{ {
LogEvent(EventRadsClick, BuildAdEventData(adParams)); TrackEvent(EventRadsClick, BuildAdEventData(adParams));
} }
/// <summary> /// <summary>
@ -211,7 +211,7 @@ namespace Guru
/// </summary> /// </summary>
public static void ADRadsClose(AdParams adParams) public static void ADRadsClose(AdParams adParams)
{ {
LogEvent(EventRadsClose, BuildAdEventData(adParams)); TrackEvent(EventRadsClose, BuildAdEventData(adParams));
} }
#endregion #endregion

View File

@ -16,6 +16,7 @@ namespace Guru
public static readonly string USD = "USD"; public static readonly string USD = "USD";
// 广告平台 // 广告平台
public static readonly string AdMAX = "MAX"; public static readonly string AdMAX = "MAX";
public static readonly string AdIronSource = "IronSource";
//IAP打点事件 //IAP打点事件
public static readonly string EventIAPFirst = "first_iap"; public static readonly string EventIAPFirst = "first_iap";
@ -42,6 +43,7 @@ namespace Guru
public static readonly string EventIadsFailed = "iads_failed"; public static readonly string EventIadsFailed = "iads_failed";
public static readonly string EventIadsImp = "iads_imp"; public static readonly string EventIadsImp = "iads_imp";
public static readonly string EventIadsClick = "iads_clk"; public static readonly string EventIadsClick = "iads_clk";
public static readonly string EventIadsPaid = "iads_paid";
public static readonly string EventIadsClose = "iads_close"; public static readonly string EventIadsClose = "iads_close";
//激励视频广告打点事件 //激励视频广告打点事件
@ -51,6 +53,7 @@ namespace Guru
public static readonly string EventRadsImp = "rads_imp"; public static readonly string EventRadsImp = "rads_imp";
public static readonly string EventRadsRewarded = "rads_rewarded"; public static readonly string EventRadsRewarded = "rads_rewarded";
public static readonly string EventRadsClick = "rads_clk"; public static readonly string EventRadsClick = "rads_clk";
public static readonly string EventRadsPaid = "rads_paid";
public static readonly string EventRadsClose = "rads_close"; public static readonly string EventRadsClose = "rads_close";
public static readonly string EventFirstRadsRewarded = "first_rads_rewarded"; public static readonly string EventFirstRadsRewarded = "first_rads_rewarded";
@ -78,7 +81,10 @@ namespace Guru
public static readonly string ParameterReplay = "replay"; // 游戏重玩 public static readonly string ParameterReplay = "replay"; // 游戏重玩
public static readonly string ParameterContinue = "continue"; // 游戏继续 public static readonly string ParameterContinue = "continue"; // 游戏继续
public static readonly string ParameterAdUnitName = "ad_unit_name"; public static readonly string ParameterAdUnitName = "ad_unit_name";
public static readonly string ParameterAdPlacement = "ad_placement";
public static readonly string ParameterAdCreativeId = "ad_creative_id"; public static readonly string ParameterAdCreativeId = "ad_creative_id";
public static readonly string ParameterReviewCreativeId = "review_creative_id";
// 评价参数 // 评价参数
public static readonly string EventRateImp = "rate_imp"; // 评价弹窗展示 public static readonly string EventRateImp = "rate_imp"; // 评价弹窗展示

View File

@ -26,9 +26,7 @@ namespace Guru
public static bool IsDebug { get; set; } = false; public static bool IsDebug { get; set; } = false;
private static bool _hasInited = false; private static bool _isGuruAnalyticInitOnce = false;
public static bool IsReady => _hasInited;
/// <summary> /// <summary>
/// 初始化Guru自打点系统 (请优先于 Firebase 初始化调用) /// 初始化Guru自打点系统 (请优先于 Firebase 初始化调用)
@ -36,7 +34,7 @@ namespace Guru
public static void InstallGuruAnalytics(bool isDebug = false, bool enableErrorLog = false) public static void InstallGuruAnalytics(bool isDebug = false, bool enableErrorLog = false)
{ {
if (_hasInited) return; if (_isGuruAnalyticInitOnce) return;
try try
{ {
@ -47,7 +45,7 @@ namespace Guru
#endif #endif
string appId = IPMConfig.IPM_X_APP_ID; string appId = IPMConfig.IPM_X_APP_ID;
string deviceInfo = new DeviceInfoData().ToString(); string deviceInfo = new DeviceInfoData().ToString();
GuruAnalytics.Init(appId, deviceInfo, IsDebug, enableErrorLog); // 初始化(带Header) GuruAnalytics.Init(appId, deviceInfo, OnGuruAnalyticsInitComplete, IsDebug, enableErrorLog); // 初始化(带Header)
_hasGotFirebaseId = false; _hasGotFirebaseId = false;
_hasGotAdId = false; _hasGotAdId = false;
@ -59,7 +57,7 @@ namespace Guru
UpdateAllValues(); UpdateAllValues();
_hasInited = true; _isGuruAnalyticInitOnce = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -79,7 +77,7 @@ namespace Guru
if (!string.IsNullOrEmpty(IPMConfig.IPM_UID)) if (!string.IsNullOrEmpty(IPMConfig.IPM_UID))
{ {
Debug.Log($"---[ANA] UID: {IPMConfig.IPM_UID}"); Debug.Log($"---[ANA] UID: {IPMConfig.IPM_UID}");
GuruAnalytics.SetUid(IPMConfig.IPM_UID); GuruAnalytics.Instance.SetUid(IPMConfig.IPM_UID);
FirebaseAnalytics.SetUserProperty(PropertyUserID, IPMConfig.IPM_UID); FirebaseAnalytics.SetUserProperty(PropertyUserID, IPMConfig.IPM_UID);
_hasGotUid = true; _hasGotUid = true;
} }
@ -95,7 +93,7 @@ namespace Guru
if (!string.IsNullOrEmpty(IPMConfig.IPM_DEVICE_ID)) if (!string.IsNullOrEmpty(IPMConfig.IPM_DEVICE_ID))
{ {
GuruAnalytics.SetDeviceId(IPMConfig.IPM_DEVICE_ID); GuruAnalytics.Instance.SetDeviceId(IPMConfig.IPM_DEVICE_ID);
FirebaseAnalytics.SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); FirebaseAnalytics.SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
_hasGotDeviceId = true; _hasGotDeviceId = true;
} }
@ -121,7 +119,7 @@ namespace Guru
if (!string.IsNullOrEmpty(IPMConfig.ADJUST_ID)) if (!string.IsNullOrEmpty(IPMConfig.ADJUST_ID))
{ {
GuruAnalytics.SetAdjustId(IPMConfig.ADJUST_ID); GuruAnalytics.Instance.SetAdjustId(IPMConfig.ADJUST_ID);
_hasGotAdjustId = true; _hasGotAdjustId = true;
} }
else else
@ -152,7 +150,7 @@ namespace Guru
if (!string.IsNullOrEmpty(IPMConfig.ADJUST_ADID)) if (!string.IsNullOrEmpty(IPMConfig.ADJUST_ADID))
{ {
GuruAnalytics.SetAdId(IPMConfig.ADJUST_ADID); GuruAnalytics.Instance.SetAdId(IPMConfig.ADJUST_ADID);
_hasGotAdId = true; _hasGotAdId = true;
} }
@ -171,7 +169,7 @@ namespace Guru
if (!string.IsNullOrEmpty(IPMConfig.FIREBASE_ID)) if (!string.IsNullOrEmpty(IPMConfig.FIREBASE_ID))
{ {
GuruAnalytics.SetFirebaseId(IPMConfig.FIREBASE_ID); GuruAnalytics.Instance.SetFirebaseId(IPMConfig.FIREBASE_ID);
_hasGotFirebaseId = true; _hasGotFirebaseId = true;
} }
else else
@ -243,7 +241,7 @@ namespace Guru
/// </summary> /// </summary>
private static void SetAndroidId() private static void SetAndroidId()
{ {
GuruAnalytics.SetAndroidID(DeviceIDHelper.AndroidID); GuruAnalytics.Instance.SetAndroidID(DeviceIDHelper.AndroidID);
} }
#endif #endif
@ -278,7 +276,7 @@ namespace Guru
var interval = (DateTime.Now - _lastReportRateDate).TotalSeconds; var interval = (DateTime.Now - _lastReportRateDate).TotalSeconds;
if (interval > _reportSuccessInterval) if (interval > _reportSuccessInterval)
{ {
GuruAnalytics.ReportEventSuccessRate(); GuruAnalytics.Instance.ReportEventSuccessRate();
_lastReportRateDate = DateTime.Now; _lastReportRateDate = DateTime.Now;
} }
} }
@ -296,7 +294,7 @@ namespace Guru
{ {
try try
{ {
GuruAnalytics.SetUserProperty(key, value); GuruAnalytics.Instance.SetUserProperty(key, value);
UpdateAllValues(); // 同步所有的ID UpdateAllValues(); // 同步所有的ID
} }
catch (Exception e) catch (Exception e)
@ -311,12 +309,12 @@ namespace Guru
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
/// <param name="data"></param> /// <param name="data"></param>
private static void CustomLogEvent(string key, Dictionary<string, dynamic> data = null, int priority = -1) private static void TrackEventGuru(string key, Dictionary<string, dynamic> data = null, int priority = -1)
{ {
try try
{ {
if (data == null) data = new Dictionary<string, dynamic>(); if (data == null) data = new Dictionary<string, dynamic>();
GuruAnalytics.LogEvent(key, data, priority); GuruAnalytics.Instance.LogEvent(key, data, priority);
UpdateAllValues(); // 同步所有的ID UpdateAllValues(); // 同步所有的ID
} }
catch (Exception e) catch (Exception e)
@ -339,7 +337,7 @@ namespace Guru
if (Math.Abs(_tch02TargetValue - value) > 0.001d) if (Math.Abs(_tch02TargetValue - value) > 0.001d)
{ {
_tch02TargetValue = value; _tch02TargetValue = value;
GuruAnalytics.SetTch02Value(value); GuruAnalytics.Instance.SetTch02Value(value);
} }
} }
} }

View File

@ -103,7 +103,7 @@ namespace Guru
if(!string.IsNullOrEmpty(scene)) data[ParameterScene] = scene; // 获取的虚拟货币或者道具的场景 if(!string.IsNullOrEmpty(scene)) data[ParameterScene] = scene; // 获取的虚拟货币或者道具的场景
if (extra != null) data.AddRange(extra, isOverride: true); if (extra != null) data.AddRange(extra, isOverride: true);
LogEvent(EventEarnVirtualCurrency, data, new EventSetting() { EnableFirebaseAnalytics = true }); TrackEvent(EventEarnVirtualCurrency, data, new EventSetting() { EnableFirebaseAnalytics = true });
// FB 上报收入点 // FB 上报收入点
FBService.LogEvent(EventEarnVirtualCurrency, value, data); FBService.LogEvent(EventEarnVirtualCurrency, value, data);
@ -131,7 +131,7 @@ namespace Guru
if(!string.IsNullOrEmpty(scene)) data[ParameterScene] = scene; // 获取的虚拟货币或者道具的场景 if(!string.IsNullOrEmpty(scene)) data[ParameterScene] = scene; // 获取的虚拟货币或者道具的场景
LogEvent(EventSpendVirtualCurrency, data, new EventSetting() { EnableFirebaseAnalytics = true }); TrackEvent(EventSpendVirtualCurrency, data, new EventSetting() { EnableFirebaseAnalytics = true });
// FB 上报消费点 // FB 上报消费点
FBService.LogEvent(EventSpendVirtualCurrency, value, data); FBService.LogEvent(EventSpendVirtualCurrency, value, data);

View File

@ -27,7 +27,7 @@ namespace Guru
}; };
if (extra != null) dict.AddRange(extra, isOverride:true); if (extra != null) dict.AddRange(extra, isOverride:true);
LogEvent(EventLevelUp, dict); TrackEvent(EventLevelUp, dict);
} }
/// <summary> /// <summary>
@ -42,7 +42,7 @@ namespace Guru
}; };
if (extra != null) dict.AddRange(extra, isOverride:true); if (extra != null) dict.AddRange(extra, isOverride:true);
LogEvent(EventUnlockAchievement, dict); TrackEvent(EventUnlockAchievement, dict);
} }
/// <summary> /// <summary>
@ -53,7 +53,7 @@ namespace Guru
[Obsolete("Obsolete method, please use <LogLevelStart> instead. will be discard in next version.")] [Obsolete("Obsolete method, please use <LogLevelStart> instead. will be discard in next version.")]
public static void LevelStart(int level) public static void LevelStart(int level)
{ {
LogEvent(EventLevelStart, new Dictionary<string, object>() TrackEvent(EventLevelStart, new Dictionary<string, object>()
{ {
{ ParameterLevel, level }, { ParameterLevel, level },
{ ParameterItemCategory, "main" }, { ParameterItemCategory, "main" },
@ -91,7 +91,7 @@ namespace Guru
dict.AddRange(extra, isOverride:true); dict.AddRange(extra, isOverride:true);
} }
LogEvent(EventLevelStart, dict); TrackEvent(EventLevelStart, dict);
} }
/// <summary> /// <summary>
@ -131,7 +131,7 @@ namespace Guru
if(extra != null) dict.AddRange(extra, isOverride:true); if(extra != null) dict.AddRange(extra, isOverride:true);
LogEvent(EventLevelEnd, dict); TrackEvent(EventLevelEnd, dict);
// if (isSuccess) // if (isSuccess)
// { // {
@ -160,7 +160,7 @@ namespace Guru
["level"] = level, ["level"] = level,
}; };
} }
LogEvent(eventName, extra, new EventSetting() TrackEvent(eventName, extra, new EventSetting()
{ {
EnableFirebaseAnalytics = true, EnableFirebaseAnalytics = true,
EnableFacebookAnalytics = true, EnableFacebookAnalytics = true,
@ -188,7 +188,7 @@ namespace Guru
if (extra != null) if (extra != null)
dict.AddRange(extra, isOverride: true); dict.AddRange(extra, isOverride: true);
LogEvent(EventLevelFirstEnd, dict); TrackEvent(EventLevelFirstEnd, dict);
} }
#endregion #endregion
@ -215,7 +215,7 @@ namespace Guru
}; };
if(extra != null) dict.AddRange(extra, isOverride: true); if(extra != null) dict.AddRange(extra, isOverride: true);
LogEvent(EventEarnVirtualCurrency, dict); TrackEvent(EventEarnVirtualCurrency, dict);
} }
/// <summary> /// <summary>
@ -237,7 +237,7 @@ namespace Guru
}; };
if(extra != null) dict.AddRange(extra, isOverride: true); if(extra != null) dict.AddRange(extra, isOverride: true);
LogEvent(EventSpendVirtualCurrency, dict); TrackEvent(EventSpendVirtualCurrency, dict);
} }
#endregion #endregion
@ -260,7 +260,7 @@ namespace Guru
}; };
if(extra != null) dict.AddRange(extra, isOverride: true); if(extra != null) dict.AddRange(extra, isOverride: true);
LogEvent("hit_points", dict); TrackEvent("hit_points", dict);
} }
#endregion #endregion
@ -387,7 +387,7 @@ namespace Guru
//--------- Extra data for IAP receipt --------------- //--------- Extra data for IAP receipt ---------------
LogEvent(evtName, data); TrackEvent(evtName, data);
} }
@ -416,7 +416,6 @@ namespace Guru
/// Google ARO买量点 /// Google ARO买量点
/// </summary> /// </summary>
/// <param name="impressionData">广告收入数据</param> /// <param name="impressionData">广告收入数据</param>
/// <param name="platform">广告平台</param>
/// <a href="https://docs.google.com/spreadsheets/d/1lFWLeOGJgq34QDBTfl6OpNh7MoI37ehGrhdbxlOrJgs/edit#gid=983654222"></a> /// <a href="https://docs.google.com/spreadsheets/d/1lFWLeOGJgq34QDBTfl6OpNh7MoI37ehGrhdbxlOrJgs/edit#gid=983654222"></a>
/// <li> /// <li>
/// value double eg0.002 /// value double eg0.002
@ -427,25 +426,25 @@ namespace Guru
/// ad_unit_name string 广告位名称 /// ad_unit_name string 广告位名称
/// ad_creative_id string 广告素材id /// ad_creative_id string 广告素材id
/// </li> /// </li>
public static void ADImpression(MaxSdkBase.AdInfo impressionData, string platform = "") public static void ADImpression(AdImpressionData impressionData)
{ {
if (string.IsNullOrEmpty(platform)) platform = AdMAX; TrackEvent(EventAdImpression, new Dictionary<string, dynamic>()
double revenue = impressionData.Revenue;
LogEvent(EventAdImpression, new Dictionary<string, dynamic>()
{ {
[ParameterValue] = revenue, [ParameterValue] = impressionData.value,
[ParameterCurrency] = USD, [ParameterCurrency] = impressionData.currency,
[ParameterAdPlatform] = platform, [ParameterAdPlatform] = impressionData.ad_platform,
[ParameterAdSource] = impressionData.NetworkName, [ParameterAdSource] = impressionData.ad_source,
[ParameterAdFormat] = impressionData.AdFormat, [ParameterAdFormat] = impressionData.ad_format,
[ParameterAdUnitName] = impressionData.AdUnitIdentifier, [ParameterAdUnitName] = impressionData.ad_unit_name,
[ParameterAdCreativeId] = impressionData.CreativeIdentifier, [ParameterAdPlacement] = impressionData.ad_placement,
[ParameterAdCreativeId] = impressionData.ad_creative_id,
[ParameterReviewCreativeId] = impressionData.review_creative_id,
}); });
} }
public static void TchAdAbnormalEvent(double value) public static void TchAdAbnormalEvent(double value)
{ {
LogEvent(EventTchAdRevAbnormal, new Dictionary<string, dynamic>() TrackEvent(EventTchAdRevAbnormal, new Dictionary<string, dynamic>()
{ {
{ ParameterAdPlatform, AdMAX }, { ParameterAdPlatform, AdMAX },
{ ParameterCurrency, USD }, { ParameterCurrency, USD },
@ -470,7 +469,7 @@ namespace Guru
}; };
if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra); if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra);
LogEvent(EventIAPImp, dict); TrackEvent(EventIAPImp, dict);
} }
/// <summary> /// <summary>
@ -486,7 +485,7 @@ namespace Guru
}; };
if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra); if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra);
LogEvent(EventIAPClose, dict); TrackEvent(EventIAPClose, dict);
} }
/// <summary> /// <summary>
@ -512,7 +511,7 @@ namespace Guru
}; };
if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra); if(extra != null) dict = GuruSDKUtils.MergeDictionary(dict, extra);
LogEvent(EventIAPClick, dict); TrackEvent(EventIAPClick, dict);
} }
/// <summary> /// <summary>
@ -543,7 +542,7 @@ namespace Guru
if(!string.IsNullOrEmpty(offerId)) if(!string.IsNullOrEmpty(offerId))
dict["basePlan"] = offerId; dict["basePlan"] = offerId;
LogEvent(EventIAPReturnTrue, dict, new EventSetting() TrackEvent(EventIAPReturnTrue, dict, new EventSetting()
{ {
EnableFirebaseAnalytics = true, EnableFirebaseAnalytics = true,
EnableAdjustAnalytics = true, EnableAdjustAnalytics = true,
@ -559,7 +558,7 @@ namespace Guru
/// <param name="failReason"></param> /// <param name="failReason"></param>
internal static void IAPRetFalse(string itemCategory, string productId, string failReason) internal static void IAPRetFalse(string itemCategory, string productId, string failReason)
{ {
LogEvent(EventIAPReturnFalse, new Dictionary<string, object>() TrackEvent(EventIAPReturnFalse, new Dictionary<string, object>()
{ {
{ ParameterItemCategory, itemCategory }, { ParameterItemCategory, itemCategory },
{ ParameterItemName, productId }, { ParameterItemName, productId },
@ -576,7 +575,7 @@ namespace Guru
/// <param name="currency">币种</param> /// <param name="currency">币种</param>
public static void FirstIAP(string itemName, double value, string currency) public static void FirstIAP(string itemName, double value, string currency)
{ {
LogEvent(EventIAPFirst, new Dictionary<string, object>() TrackEvent(EventIAPFirst, new Dictionary<string, object>()
{ {
{ ParameterItemName, itemName }, { ParameterItemName, itemName },
{ ParameterValue, value }, { ParameterValue, value },
@ -597,7 +596,7 @@ namespace Guru
if (productName.Contains(".")) productName = productName.Replace(".", "_"); if (productName.Contains(".")) productName = productName.Replace(".", "_");
string eventName = $"iap_{productName}"; string eventName = $"iap_{productName}";
LogEvent(eventName, new Dictionary<string, object>() TrackEvent(eventName, new Dictionary<string, object>()
{ {
{ ParameterItemName, itemName }, { ParameterItemName, itemName },
{ ParameterValue, value }, { ParameterValue, value },
@ -724,10 +723,8 @@ namespace Guru
["sandbox"] = isSandbox? "true": "false" ["sandbox"] = isSandbox? "true": "false"
}; };
// 上报Firebase + 自打点 // 上报Firebase + 自打点
LogEvent(eventName, dict, new EventSetting() { EnableFirebaseAnalytics = true }); TrackEvent(eventName, dict, new EventSetting() { EnableFirebaseAnalytics = true });
// 上报 Adjust 支付事件 // 上报 Adjust 支付事件
LogAdjustRevenueEvent(eventName, value, productId, orderId, purchaseToken, receipt, dict); LogAdjustRevenueEvent(eventName, value, productId, orderId, purchaseToken, receipt, dict);
@ -746,7 +743,7 @@ namespace Guru
if (data == null) return; if (data == null) return;
data["country"] = IPMConfig.IPM_COUNTRY_CODE; data["country"] = IPMConfig.IPM_COUNTRY_CODE;
data["network"] = Application.internetReachability.ToString(); data["network"] = Application.internetReachability.ToString();
LogEvent(EventDevAudit, data, new EventSetting() { EnableFirebaseAnalytics = true }); TrackEvent(EventDevAudit, data, new EventSetting() { EnableFirebaseAnalytics = true });
} }
#endregion #endregion

View File

@ -1,5 +1,7 @@
using System.Threading;
namespace Guru namespace Guru
{ {
using System; using System;
@ -40,18 +42,18 @@ namespace Guru
private static EventSetting DefaultEventSetting => EventSetting.GetDefaultSetting(); private static EventSetting DefaultEventSetting => EventSetting.GetDefaultSetting();
private static bool _isInited; //Analytics是否初始化完成 private static bool _isInitOnce; //Analytics是否初始化完成
public static bool EnableDebugAnalytics; //允许Debug包上报打点 public static bool EnableDebugAnalytics; //允许Debug包上报打点
public static bool IsDebugMode => PlatformUtil.IsDebug(); public static bool IsDebugMode => PlatformUtil.IsDebug();
private static bool IsFirebaseReady => FirebaseUtil.IsFirebaseInitialized; private static bool IsFirebaseReady => FirebaseUtil.IsFirebaseInitialized;
private static bool IsEnable private static bool IsReady
{ {
get get
{ {
//Analytics没有初始化不上报打点 //Analytics没有初始化不上报打点
if (!_isInited) return false; if (!_isInitOnce) return false;
//Firebase服务没有初始化完成不上报打点 //Firebase服务没有初始化完成不上报打点
if (!IsFirebaseReady) return false; if (!IsFirebaseReady) return false;
@ -65,42 +67,62 @@ namespace Guru
} }
} }
private static AdjustEventDriver _adjustEventDriver;
private static FBEventDriver _fbEventDriver;
private static FirebaseEventDriver _firebaseEventDriver;
private static GuruEventDriver _guruEventDriver;
#region 初始化 #region 初始化
public static void InitAnalytics() public static void InitAnalytics()
{ {
if (_isInited) return; if (_isInitOnce) return;
_isInited = true; _isInitOnce = true;
// -------- 初始化 Exception ---------- _adjustEventDriver = new AdjustEventDriver();
CrashlyticsAgent.Install(); _fbEventDriver = new FBEventDriver();
_firebaseEventDriver = new FirebaseEventDriver();
_guruEventDriver = new GuruEventDriver();
// ------- 初始化自打点 ---------- // ------- 初始化自打点 ----------
InstallGuruAnalytics(IsDebug); InstallGuruAnalytics(IsDebug);
FirebaseUtil.onInitComplete += OnFirebaseCompleted;
} }
private static void OnFirebaseCompleted(bool success)
public static void OnFirebaseInitCompleted()
{ {
FirebaseUtil.onInitComplete -= OnFirebaseCompleted; Debug.Log($"[SDK] --- Analytics Init After FirebaseReady:{IsFirebaseReady}");
Debug.Log($"[SDK] --- Analytics Init After FirebaseCompleted: {success}");
ConsumeAllCachedEvent(); // -------- 初始化 Crashlytics ----------
CrashlyticsAgent.Init();
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime);
if (success) _firebaseEventDriver.TriggerFlush();
{
Crashlytics.IsCrashlyticsCollectionEnabled = true;
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime);
}
} }
public static void OnFBInitComplete()
{
_fbEventDriver.TriggerFlush();
}
public static void OnAdjustInitComplete()
{
_adjustEventDriver.TriggerFlush();
}
private static void OnGuruAnalyticsInitComplete()
{
_guruEventDriver.TriggerFlush();
}
#endregion #endregion
@ -109,9 +131,9 @@ namespace Guru
public static void SetCurrentScreen(string screenName, string className) public static void SetCurrentScreen(string screenName, string className)
{ {
Log.I(TAG,$"SetCurrentScreen -> screenName:{screenName}, className:{className}"); Log.I(TAG,$"SetCurrentScreen -> screenName:{screenName}, className:{className}");
GuruAnalytics.SetScreen(screenName); GuruAnalytics.Instance.SetScreen(screenName);
if (!IsEnable) return; if (!IsReady) return;
FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventScreenView, FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventScreenView,
new Parameter(FirebaseAnalytics.ParameterScreenClass, className), new Parameter(FirebaseAnalytics.ParameterScreenClass, className),
new Parameter(FirebaseAnalytics.ParameterScreenName, screenName) new Parameter(FirebaseAnalytics.ParameterScreenName, screenName)
@ -129,7 +151,7 @@ namespace Guru
public static void SetUserIDProperty(string userID) public static void SetUserIDProperty(string userID)
{ {
Log.I(TAG,$"SetUserIDProperty -> userID:{userID}"); Log.I(TAG,$"SetUserIDProperty -> userID:{userID}");
if (!IsEnable) return; if (!IsReady) return;
FirebaseAnalytics.SetUserId(userID); FirebaseAnalytics.SetUserId(userID);
} }
@ -141,7 +163,7 @@ namespace Guru
{ {
Log.I(TAG,$"SetUserProperty -> propertyName:{propertyName}, propertyValue:{propertyValue}"); Log.I(TAG,$"SetUserProperty -> propertyName:{propertyName}, propertyValue:{propertyValue}");
if (!IsEnable) if (!IsReady)
return; return;
FirebaseAnalytics.SetUserProperty(propertyName, propertyValue); FirebaseAnalytics.SetUserProperty(propertyName, propertyValue);
@ -151,119 +173,62 @@ namespace Guru
#endregion #endregion
#region 打点上报 #region 打点上报
/// <summary>
/// 打点上报
/// </summary>
/// <param name="eventName"></param>
/// <param name="eventSetting"></param>
internal static void LogEvent(string eventName, EventSetting eventSetting = null, int priority = -1)
{
if (eventSetting == null) eventSetting = DefaultEventSetting;
if(eventSetting.EnableGuruAnalytics)
CustomLogEvent(eventName, null, priority); // 自定义打点上报
CheckLogCache(eventName, null, eventSetting); // log缓存和消费
if (!IsEnable) return;
Log.I(TAG, $" --- logEvent:{eventName}, eventSetting:{eventSetting}");
if (eventSetting.EnableFirebaseAnalytics)
{
FirebaseAnalytics.LogEvent(eventName);
}
if (eventSetting.EnableAdjustAnalytics)
{
Adjust.trackEvent(CreateAdjustEvent(eventName));
}
if (eventSetting.EnableFacebookAnalytics)
{
FBService.LogEvent(eventName);
}
}
/// <summary> /// <summary>
/// 打点上报 (带参数) /// 打点上报 (带参数)
/// </summary> /// </summary>
/// <param name="eventName"></param> /// <param name="eventName"></param>
/// <param name="extras"></param> /// <param name="data"></param>
/// <param name="eventSetting"></param> /// <param name="eventSetting"></param>
/// <param name="priority"></param> /// <param name="priority"></param>
internal static void LogEvent(string eventName, Dictionary<string, dynamic> extras, EventSetting eventSetting = null, int priority = -1) internal static void TrackEvent(string eventName, Dictionary<string, dynamic> data,
EventSetting eventSetting = null, int priority = -1)
{ {
if (!_isInitOnce)
{
throw new Exception($"【SDK] Analytics did not initialized, Call <Analytics.{nameof(InitAnalytics)}()> first!");
}
if (eventSetting == null) eventSetting = DefaultEventSetting; if (eventSetting == null) eventSetting = DefaultEventSetting;
if(eventSetting.EnableGuruAnalytics)
CustomLogEvent(eventName, extras, priority); // 自定义打点上报
CheckLogCache(eventName, extras, eventSetting); // log缓存和消费
if (!IsEnable) return;
if (extras == null)
{
LogEvent(eventName, eventSetting, priority); // 防空判定
return;
}
string paramStr = string.Join(",", extras); var dataStr = "";
Log.I(TAG, $" --- logEvent:{eventName}, params:{paramStr}, eventSetting:{eventSetting}"); if (data != null) dataStr = JsonParser.ToJson(data);
Debug.Log($"{TAG} --- [SDK] TrackEvent: {eventName} | priority: {priority} | data:{dataStr} | eventSetting: {eventSetting}");
if (eventSetting.EnableFirebaseAnalytics) try
{ {
List<Parameter> parameters = new List<Parameter>(); // 填充相关的追踪事件
foreach (var kv in extras) if (eventSetting.EnableGuruAnalytics)
{ {
if(kv.Value is string strValue) _guruEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority));
parameters.Add(new Parameter(kv.Key, strValue));
else if (kv.Value is bool boolValue)
parameters.Add(new Parameter(kv.Key, boolValue ? "true" : "false"));
else if (kv.Value is int intValue)
parameters.Add(new Parameter(kv.Key, intValue));
else if (kv.Value is long longValue)
parameters.Add(new Parameter(kv.Key, longValue));
else if (kv.Value is float floatValue)
parameters.Add(new Parameter(kv.Key, floatValue));
else if (kv.Value is double doubleValue)
parameters.Add(new Parameter(kv.Key, doubleValue));
else if (kv.Value is decimal decimalValue)
parameters.Add(new Parameter(kv.Key, decimal.ToDouble(decimalValue)));
else
parameters.Add(new Parameter(kv.Key, kv.Value.ToString()));
} }
if (eventSetting.EnableFirebaseAnalytics)
FirebaseAnalytics.LogEvent(eventName, parameters.ToArray());
}
Dictionary<string, object> dict = new Dictionary<string, object>();
GuruSDKUtils.MergeDictionary(dict, extras);
if (eventSetting.EnableAdjustAnalytics)
{
AdjustEvent adjustEvent = Analytics.CreateAdjustEvent(eventName);
if (adjustEvent != null)
{ {
if (dict.Count > 0) _firebaseEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority));
{ }
foreach (var kv in dict) if (eventSetting.EnableAdjustAnalytics)
{ {
adjustEvent.AddEventParameter(kv.Key, kv.Value.ToString()); _adjustEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority));
} }
} if (eventSetting.EnableFacebookAnalytics)
Adjust.trackEvent(adjustEvent); {
_fbEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority));
} }
} }
catch (Exception ex)
if (eventSetting.EnableFacebookAnalytics)
{ {
FBService.LogEvent(eventName, null, dict); if (FirebaseUtil.IsReady)
{
Crashlytics.LogException(ex);
}
else
{
Debug.Log($"Catch Error: {ex}");
}
} }
} }
/// <summary> /// <summary>
/// 上报 Adjust 事件 /// 上报 Adjust 事件
@ -316,14 +281,7 @@ namespace Guru
/// <param name="priority"></param> /// <param name="priority"></param>
public static void Track(string key, Dictionary<string, dynamic> data = null, EventSetting setting = null, int priority = -1) public static void Track(string key, Dictionary<string, dynamic> data = null, EventSetting setting = null, int priority = -1)
{ {
if (null != data) TrackEvent(key, data, setting, priority);
{
LogEvent(key, data, setting, priority);
}
else
{
LogEvent(key, setting, priority);
}
} }
@ -334,7 +292,7 @@ namespace Guru
/// <param name="isException"></param> /// <param name="isException"></param>
public static void LogCrashlytics(string msg, bool isException = true) public static void LogCrashlytics(string msg, bool isException = true)
{ {
if (!_isInited) return; if (!_isInitOnce) return;
if (isException) if (isException)
{ {
LogCrashlytics(new Exception(msg)); LogCrashlytics(new Exception(msg));
@ -348,7 +306,7 @@ namespace Guru
public static void LogCrashlytics(Exception exp) public static void LogCrashlytics(Exception exp)
{ {
if (!_isInited) return; if (!_isInitOnce) return;
CrashlyticsAgent.LogException(exp); CrashlyticsAgent.LogException(exp);
} }
@ -356,95 +314,58 @@ namespace Guru
#region 打点缓存 #region 打点缓存
private static Queue<SavedLog> _savedLogs; private static Queue<TrackingEvent> _savedLogs;
internal static Queue<SavedLog> SavedLogs internal static Queue<TrackingEvent> SavedLogs
{ {
get get
{ {
if (_savedLogs == null) _savedLogs = new Queue<SavedLog>(20); if (_savedLogs == null) _savedLogs = new Queue<TrackingEvent>(20);
return _savedLogs; return _savedLogs;
} }
} }
private static void CheckLogCache(string key, Dictionary<string, dynamic> data = null, EventSetting setting = null, int priority = -1)
{
try
{
if (!IsEnable)
{
// 保存打点
if (data == null) data = new Dictionary<string, dynamic>();
data["log_stamp"] = TimeUtil.GetCurrentTimeStamp().ToString();
if (setting == null) setting = DefaultEventSetting;
setting.EnableGuruAnalytics = false;
SavedLogs.Enqueue(new SavedLog(key, data, setting, priority));
Log.W(TAG,$" --- LogEvent [{key}] has been cached before SDK init!");
}
else
{
ConsumeAllCachedEvent();
}
}
catch (Exception ex)
{
if (FirebaseUtil.IsReady)
{
Crashlytics.LogException(ex);
}
else
{
Debug.Log($"Catch Error: {ex}");
}
}
}
/// <summary>
/// 消耗所有的缓存日志
/// </summary>
private static void ConsumeAllCachedEvent()
{
// 消耗打点
int len = SavedLogs.Count;
if (len <= 0) return;
while (SavedLogs.Count > 0)
{
var log = SavedLogs.Dequeue();
LogEvent(log.key, log.data, log.setting, log.priority);
}
}
#endregion #endregion
} }
internal class SavedLog public class TrackingEvent
{ {
public string key; public string eventName;
public int priority; public int priority;
public Dictionary<string, dynamic> data; public Dictionary<string, dynamic> data;
public Analytics.EventSetting setting; public Analytics.EventSetting setting;
public SavedLog() public TrackingEvent()
{ {
} }
/// <summary> /// <summary>
/// 保存打点信息 /// 保存打点信息
/// </summary> /// </summary>
/// <param name="_key"></param> /// <param name="eventName"></param>
/// <param name="_data"></param> /// <param name="_data"></param>
/// <param name="_setting"></param> /// <param name="_setting"></param>
/// <param name="_priority"></param> /// <param name="_priority"></param>
public SavedLog(string _key, Dictionary<string, dynamic> _data = null, Analytics.EventSetting _setting = null, int _priority = -1) public TrackingEvent(string eventName, Dictionary<string, dynamic> data = null, Analytics.EventSetting setting = null, int priority = -1)
{ {
key = _key; this.eventName = eventName;
data = _data; this.data = data;
setting = _setting; this.setting = setting;
priority = _priority; this.priority = priority;
}
public void Flush()
{
Analytics.TrackEvent(eventName, data, setting, priority);
}
public override string ToString()
{
return $"eventName: {eventName}, data: {data}, setting: {setting}, priority: {priority}";
} }
} }
} }

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d13f245a2ea24c8ebdb957ad4ba79ca4
timeCreated: 1721908201

View File

@ -0,0 +1,46 @@
namespace Guru
{
public abstract class AbstractEventDriver: IEventDriver
{
private GuruEventBuffer _buffer = new GuruEventBuffer();
// Firebase 是否可用
private bool _isDriverReady = false;
public void TriggerFlush()
{
_isDriverReady = true;
FlushAll();
}
public void Append(TrackingEvent trackingEvent)
{
if (_isDriverReady)
{
FlushTrackingEvent(trackingEvent);
}
else
{
_buffer.Push(trackingEvent);
}
}
private void FlushAll()
{
while(_buffer.Pop(out var trackingEvent))
{
FlushTrackingEvent(trackingEvent);
}
}
/// <summary>
/// 发送事件
/// </summary>
/// <param name="trackEvent"></param>
protected abstract void FlushTrackingEvent(TrackingEvent trackEvent);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fe02e66845f64aafa94654814822930a
timeCreated: 1721916181

View File

@ -0,0 +1,35 @@
using System.Collections.Generic;
namespace Guru
{
using com.adjust.sdk;
public class AdjustEventDriver : AbstractEventDriver
{
/// <summary>
/// 发送事件
/// </summary>
/// <param name="trackingEvent"></param>
protected override void FlushTrackingEvent(TrackingEvent trackingEvent)
{
var eventName = trackingEvent.eventName;
var data = trackingEvent.data;
AdjustEvent adjustEvent = Analytics.CreateAdjustEvent(eventName);
if (adjustEvent != null)
{
UnityEngine.Debug.Log($"[SDK] --- Adjust logEvent: {trackingEvent}");
if (data != null && data.Count > 0)
{
foreach (var kv in data)
{
adjustEvent.AddEventParameter(kv.Key, ((object)kv.Value).ToString());
}
}
Adjust.trackEvent(adjustEvent);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f064544085c8401d988b9eae91bd79b1
timeCreated: 1721910840

View File

@ -0,0 +1,21 @@
using System;
using UnityEngine;
namespace Guru
{
public class FBEventDriver: AbstractEventDriver
{
/// <summary>
/// 发送事件
/// </summary>
/// <param name="trackingEvent"></param>
protected override void FlushTrackingEvent(TrackingEvent trackingEvent)
{
var eventName = trackingEvent.eventName;
var data = trackingEvent.data;
Debug.Log($"[SDK] --- FB logEvent: {trackingEvent}");
FBService.LogEvent(eventName, null, data);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 305a62f1c0fe4a16b7c9101264be523b
timeCreated: 1721910530

View File

@ -0,0 +1,55 @@
namespace Guru
{
using Firebase.Analytics;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Firebase 专用
/// </summary>
internal class FirebaseEventDriver : AbstractEventDriver
{
protected override void FlushTrackingEvent(TrackingEvent trackingEvent)
{
var eventName = trackingEvent.eventName;
var data = trackingEvent.data;
Debug.Log($"[SDK] --- Firebase logEvent: {trackingEvent}");
if (data != null)
{
List<Parameter> parameters = new List<Parameter>();
foreach (var kv in data)
{
if(kv.Value is string strValue)
parameters.Add(new Parameter(kv.Key, strValue));
else if (kv.Value is bool boolValue)
parameters.Add(new Parameter(kv.Key, boolValue ? "true" : "false"));
else if (kv.Value is int intValue)
parameters.Add(new Parameter(kv.Key, intValue));
else if (kv.Value is long longValue)
parameters.Add(new Parameter(kv.Key, longValue));
else if (kv.Value is float floatValue)
parameters.Add(new Parameter(kv.Key, floatValue));
else if (kv.Value is double doubleValue)
parameters.Add(new Parameter(kv.Key, doubleValue));
else if (kv.Value is decimal decimalValue)
parameters.Add(new Parameter(kv.Key, decimal.ToDouble(decimalValue)));
else
parameters.Add(new Parameter(kv.Key, kv.Value.ToString()));
}
FirebaseAnalytics.LogEvent(eventName, parameters.ToArray());
}
else
{
FirebaseAnalytics.LogEvent(eventName);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7a4c2262ab86431ba6d5961e7453d60f
timeCreated: 1721905429

View File

@ -0,0 +1,55 @@
using System.Collections.Generic;
namespace Guru
{
using System.Collections.Concurrent;
public class GuruEventBuffer
{
private ConcurrentQueue<TrackingEvent> _eventQueue;
public int Count => _eventQueue?.Count ?? -1;
/// <summary>
/// 构造函数
/// </summary>
public GuruEventBuffer()
{
_eventQueue = new ConcurrentQueue<TrackingEvent>();
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="trackEvent"></param>
public void Push(TrackingEvent trackEvent)
{
_eventQueue.Enqueue(trackEvent);
}
/// <summary>
/// 出栈
/// </summary>
/// <param name="trackingEvent"></param>
/// <returns></returns>
public bool Pop(out TrackingEvent trackingEvent)
{
return _eventQueue.TryDequeue(out trackingEvent);
}
}
public interface IEventDriver
{
void TriggerFlush();
void Append(TrackingEvent trackingEvent);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f6dd65ae849944c59be2182eb64db34a
timeCreated: 1721899117

View File

@ -0,0 +1,11 @@
namespace Guru
{
public class GuruEventDriver: AbstractEventDriver
{
protected override void FlushTrackingEvent(TrackingEvent trackingEvent)
{
GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d3fee5c262ca43c387ce76d52fb149c8
timeCreated: 1721908696

View File

@ -58,7 +58,7 @@ namespace Guru
if (!string.IsNullOrEmpty(value)) if (!string.IsNullOrEmpty(value))
{ {
PlayerPrefs.SetString(nameof(FirebaseId), value); PlayerPrefs.SetString(nameof(FirebaseId), value);
GuruAnalytics.SetFirebaseId(value); GuruAnalytics.Instance.SetFirebaseId(value);
} }
} }
} }
@ -73,7 +73,7 @@ namespace Guru
if (!string.IsNullOrEmpty(value)) if (!string.IsNullOrEmpty(value))
{ {
PlayerPrefs.SetString(nameof(AdjustId), value); PlayerPrefs.SetString(nameof(AdjustId), value);
GuruAnalytics.SetAdjustId(value); GuruAnalytics.Instance.SetAdjustId(value);
} }
} }
} }
@ -88,7 +88,7 @@ namespace Guru
if (!string.IsNullOrEmpty(value)) if (!string.IsNullOrEmpty(value))
{ {
PlayerPrefs.SetString(nameof(GoogleAdId), value); PlayerPrefs.SetString(nameof(GoogleAdId), value);
GuruAnalytics.SetAdId(value); GuruAnalytics.Instance.SetAdId(value);
} }
} }
} }

View File

@ -1,44 +1,38 @@
using System.Collections.Generic;
using Facebook.Unity;
using UnityEngine;
namespace Guru namespace Guru
{ {
using System;
using System.Collections.Generic;
using Facebook.Unity;
using UnityEngine;
[MonoSingleton(EMonoSingletonType.CreateOnNewGameObject, false)] [MonoSingleton(EMonoSingletonType.CreateOnNewGameObject, false)]
public class FBService : MonoSingleton<FBService> public class FBService : MonoSingleton<FBService>
{ {
public static readonly string LOG_TAG = "FB"; private const string Tag = "[FB]";
private bool _isInitOnce;
private Action _onInitComplete;
public void StartService() public void StartService(Action onInitComplete)
{ {
if (!FB.IsInitialized) if(_isInitOnce) return;
{ _isInitOnce = true;
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity); _onInitComplete = onInitComplete;
} // Initialize the Facebook SDK
else FB.Init(InitCallback, OnHideUnity);
{
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
} }
private void InitCallback() private void InitCallback()
{ {
if (FB.IsInitialized)
{ // Signal an app activation App Event
// Signal an app activation App Event FB.ActivateApp();
FB.ActivateApp(); FB.Mobile.SetAdvertiserIDCollectionEnabled(true);
FB.Mobile.SetAdvertiserIDCollectionEnabled(true); FB.Mobile.SetAutoLogAppEventsEnabled(false); // 关闭自动打点上报
FB.Mobile.SetAutoLogAppEventsEnabled(false); // 关闭自动打点上报
#if UNITY_IOS #if UNITY_IOS
FB.Mobile.SetAdvertiserTrackingEnabled(true); FB.Mobile.SetAdvertiserTrackingEnabled(true);
#endif #endif
} _onInitComplete?.Invoke();
else
{
Log.E(LOG_TAG, "Failed to Initialize the Facebook SDK");
}
} }
private void OnHideUnity(bool isGameShown) private void OnHideUnity(bool isGameShown)
@ -81,13 +75,13 @@ namespace Guru
} }
private static bool IsAvailable public static bool IsAvailable
{ {
get get
{ {
if (!FB.IsInitialized) if (!FB.IsInitialized)
{ {
Debug.LogError("[FB] FB is not initialized, please call <FBService.StartService> first."); Debug.LogError($"{Tag} FB is not initialized, please call <FBService.StartService> first.");
return false; return false;
} }
return true; return true;

View File

@ -24,7 +24,7 @@ namespace Guru
public static void InitFirebase(Action callback) public static void InitFirebase(Action callback)
{ {
_isReady = false; _isReady = false;
Analytics.InitAnalytics(); // 打点提前初始化 // Analytics.InitAnalytics(); // 打点提前初始化
// Loom.StartUp(); // 确保主线程开启 // Loom.StartUp(); // 确保主线程开启
@ -137,6 +137,8 @@ namespace Guru
IPMConfig.ADJUST_ID = adjustId; IPMConfig.ADJUST_ID = adjustId;
} }
ReportAdjustId(adjustId); ReportAdjustId(adjustId);
Analytics.OnAdjustInitComplete();
} }

View File

@ -9,9 +9,8 @@ namespace Guru
public static class CrashlyticsAgent public static class CrashlyticsAgent
{ {
private static bool _initOnce; private static bool _initOnce;
private static bool _isReady; private static bool IsFirebaseReady => FirebaseUtil.IsFirebaseInitialized;
private static Queue<Exception> _expCache;
private static bool _hasSetUser = false; private static bool _hasSetUser = false;
/// <summary> /// <summary>
@ -32,45 +31,18 @@ namespace Guru
LogType.Assert, LogType.Assert,
}; };
public static void Install() public static void Init()
{ {
if (_initOnce) return; if (_initOnce) return;
_initOnce = true; _initOnce = true;
_expCache = new Queue<Exception>(20);
Application.logMessageReceived -= OnReceivedMessage; Application.logMessageReceived -= OnReceivedMessage;
Application.logMessageReceived += OnReceivedMessage; Application.logMessageReceived += OnReceivedMessage;
// Application.logMessageReceivedThreaded -= OnReceivedMessage; Crashlytics.IsCrashlyticsCollectionEnabled = true;
// Application.logMessageReceivedThreaded += OnReceivedMessage;
if (FirebaseUtil.IsReady)
{
OnFirebaseComplete(true);
return;
}
FirebaseUtil.onInitComplete -= OnFirebaseComplete;
FirebaseUtil.onInitComplete += OnFirebaseComplete;
} }
private static void OnFirebaseComplete(bool success)
{
FirebaseUtil.onInitComplete -= OnFirebaseComplete;
if (success)
{
_isReady = true;
Crashlytics.IsCrashlyticsCollectionEnabled = true;
if (_expCache != null && _expCache.Count > 0)
{
while (_expCache.Count > 0)
{
LogException(_expCache.Dequeue());
}
_expCache.Clear();
}
}
}
private static string ToLogTypeString(LogType type) private static string ToLogTypeString(LogType type)
{ {
@ -101,14 +73,8 @@ namespace Guru
public static void LogException(Exception ex) public static void LogException(Exception ex)
{ {
if (!_isReady) if (!IsFirebaseReady) return;
{
Install();
_expCache.Enqueue(ex);
return;
}
Crashlytics.LogException(ex); Crashlytics.LogException(ex);
// CheckSetUser();
} }
public static void LogException(string msg) public static void LogException(string msg)
@ -118,15 +84,14 @@ namespace Guru
public static void Log(string msg) public static void Log(string msg)
{ {
if (!_isReady) return; if (!IsFirebaseReady) return;
Crashlytics.Log(msg); Crashlytics.Log(msg);
// CheckSetUser();
} }
public static void SetCustomKey(string key, string value) public static void SetCustomKey(string key, string value)
{ {
if (!_isReady) return; if (!IsFirebaseReady) return;
Crashlytics.SetCustomKey(key, value); Crashlytics.SetCustomKey(key, value);
} }

View File

@ -140,9 +140,13 @@ namespace Guru
public class AnalyticsSetting public class AnalyticsSetting
{ {
[SerializeField] private int levelEndSuccessNum = 50; [SerializeField] private int levelEndSuccessNum = 50;
[Obsolete("Will not use in next version", false)]
[SerializeField] private bool enalbeFirebaseAnalytics = true; [SerializeField] private bool enalbeFirebaseAnalytics = true;
[Obsolete("Will not use in next version", false)]
[SerializeField] private bool enalbeFacebookAnalytics = true; [SerializeField] private bool enalbeFacebookAnalytics = true;
[Obsolete("Will not use in next version", false)]
[SerializeField] private bool enalbeAdjustAnalytics = true; [SerializeField] private bool enalbeAdjustAnalytics = true;
[SerializeField] internal List<AdjustEvent> adjustEventList; [SerializeField] internal List<AdjustEvent> adjustEventList;
public int LevelEndSuccessNum => levelEndSuccessNum; public int LevelEndSuccessNum => levelEndSuccessNum;

View File

@ -1132,7 +1132,7 @@ namespace Guru
private void ReportGoogleOrderLost(GoogleOrderData data) private void ReportGoogleOrderLost(GoogleOrderData data)
{ {
Analytics.LogEvent("google_order_lost", new Dictionary<string, dynamic>() Analytics.TrackEvent("google_order_lost", new Dictionary<string, dynamic>()
{ {
["data"] = data.ToString(), ["data"] = data.ToString(),
}, new Analytics.EventSetting() }, new Analytics.EventSetting()
@ -1144,7 +1144,7 @@ namespace Guru
private void ReportAppleOrderLost(AppleOrderData data) private void ReportAppleOrderLost(AppleOrderData data)
{ {
Analytics.LogEvent("apple_order_lost", new Dictionary<string, dynamic>() Analytics.TrackEvent("apple_order_lost", new Dictionary<string, dynamic>()
{ {
["data"] = data.ToString(), ["data"] = data.ToString(),
}, new Analytics.EventSetting() }, new Analytics.EventSetting()