Files
MinFt/Client/Assets/Scripts/Services/AdsEventLogger.cs
2026-04-27 12:07:32 +08:00

125 lines
4.3 KiB
C#

using UnityEngine;
namespace game
{
public static class AdsEventLogger
{
private const string CRASH_FLAG_KEY = "ads_crash_flag";
public static void SetCrashFlag(string adType, string operation)
{
PlayerPrefs.SetString(CRASH_FLAG_KEY, $"{adType}_{operation}_{System.DateTimeOffset.UtcNow.ToUnixTimeSeconds()}");
PlayerPrefs.Save();
}
public static void ClearCrashFlag()
{
PlayerPrefs.DeleteKey(CRASH_FLAG_KEY);
PlayerPrefs.Save();
}
public static void CheckAndLogCrash()
{
string flag = PlayerPrefs.GetString(CRASH_FLAG_KEY, "");
if (string.IsNullOrEmpty(flag)) return;
PlayerPrefs.DeleteKey(CRASH_FLAG_KEY);
PlayerPrefs.Save();
string[] parts = flag.Split('_');
string adType = parts.Length > 0 ? parts[0] : "unknown";
string operation = parts.Length > 1 ? parts[1] : "unknown";
using (var e = GEvent.GameEvent("ads_app_crash"))
{
e.AddContent("crash_flag", flag);
e.AddContent("ad_type", adType);
e.AddContent("operation", operation);
}
}
public static void LogLoadStart(string adType, string adUnitId, string triggerSource)
{
using (var e = GEvent.GameEvent($"ads_{adType}_load_start"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("trigger_source", triggerSource);
}
}
public static void LogLoadSuccess(string adType, string adUnitId, string placement)
{
using (var e = GEvent.GameEvent($"ads_{adType}_load_success"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
}
}
public static void LogLoadFail(string adType, string adUnitId, int retryCount, string errorCode, string errorMessage)
{
using (var e = GEvent.GameEvent($"ads_{adType}_load_fail"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("retry_count", retryCount);
e.AddContent("error_code", errorCode);
e.AddContent("error_message", errorMessage);
}
}
public static void LogDisplay(string adType, string adUnitId, string placement)
{
using (var e = GEvent.GameEvent($"ads_{adType}_display"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
}
}
public static void LogDisplayFail(string adType, string adUnitId, string placement, string errorCode, string errorMessage)
{
using (var e = GEvent.GameEvent($"ads_{adType}_display_fail"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
e.AddContent("error_code", errorCode);
e.AddContent("error_message", errorMessage);
}
}
public static void LogHidden(string adType, string adUnitId, string placement)
{
using (var e = GEvent.GameEvent($"ads_{adType}_hidden"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
}
}
public static void LogRewarded(string adUnitId, string placement)
{
using (var e = GEvent.GameEvent("ads_reward_rewarded"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
}
}
public static void LogClick(string adType, string adUnitId, string placement)
{
using (var e = GEvent.GameEvent($"ads_{adType}_click"))
{
e.AddContent("ad_unit_id", adUnitId);
e.AddContent("placement", placement);
}
}
public static void LogNotReady(string adType, string placement)
{
using (var e = GEvent.GameEvent($"ads_{adType}_not_ready"))
{
e.AddContent("placement", placement);
}
}
}
}