199 lines
No EOL
7.3 KiB
C#
199 lines
No EOL
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ShiroginSDK.Runtime.Core.SDK;
|
|
using ShiroginSDK.Runtime.Modules.Analytics.Enums;
|
|
using ShiroginSDK.Runtime.Services.Base;
|
|
using ShiroginSDK.Runtime.Services.Interfaces;
|
|
using UnityEngine;
|
|
#if SHIROGIN_ANALYTICS
|
|
using ShiroginSDK.Runtime.Modules.Analytics.Scripts;
|
|
#endif
|
|
|
|
namespace ShiroginSDK.Runtime.Services.Implementations.Analytics
|
|
{
|
|
#if SHIROGIN_ANALYTICS
|
|
/// <summary>
|
|
/// Unified Analytics Service that bridges Firebase, AppsFlyer, and Facebook SDKs.
|
|
/// </summary>
|
|
public class AnalyticsService : ServiceBase, IAnalyticsService
|
|
{
|
|
private IAppsFlyerService _appsflyer;
|
|
private ShiroginConfig _config;
|
|
private IFacebookService _facebook;
|
|
|
|
private IFirebaseService _firebase;
|
|
|
|
// --------------------------------------------------------
|
|
// Unified Event Logging
|
|
// --------------------------------------------------------
|
|
public void LogEvent(AnalyticsEventType eventType, Dictionary<string, object> parameters = null)
|
|
{
|
|
Debug.Log($"[AnalyticsService] 📊 Logging event: {eventType}");
|
|
|
|
// FIREBASE
|
|
if (_config.enableFirebaseAnalytics && _firebase != null)
|
|
{
|
|
var firebaseName = AnalyticsEventMapper.GetFirebaseName(eventType);
|
|
var firebaseParams = MapParameters(parameters, AnalyticsEventMapper.GetFirebaseName);
|
|
_firebase.LogEvent(firebaseName, firebaseParams);
|
|
}
|
|
|
|
// APPSFLYER
|
|
if (_config.enableAppsFlyerAnalytics && _appsflyer != null)
|
|
{
|
|
var appsFlyerName = AnalyticsEventMapper.GetAppsFlyerName(eventType);
|
|
var appsFlyerParams = MapParametersToString(parameters, AnalyticsEventMapper.GetAppsFlyerName);
|
|
_appsflyer.TrackCustomEvent(appsFlyerName, appsFlyerParams);
|
|
}
|
|
|
|
// FACEBOOK
|
|
if (_config.enableFacebook && _facebook != null)
|
|
{
|
|
var fbName = AnalyticsEventMapper.GetFacebookName(eventType);
|
|
var fbParams = MapParameters(parameters, AnalyticsEventMapper.GetFacebookName);
|
|
_facebook.LogEvent(fbName, null, fbParams);
|
|
}
|
|
}
|
|
|
|
protected override void OnInitialize()
|
|
{
|
|
_config = ShiroginConfig.Load();
|
|
if (_config == null)
|
|
{
|
|
Debug.LogError("[ShiroginSDK] AnalyticsService failed to initialize — missing SDKConfig.");
|
|
return;
|
|
}
|
|
|
|
// Resolve dependencies
|
|
_firebase = ServiceLocator.Get<IFirebaseService>();
|
|
_facebook = ServiceLocator.Get<IFacebookService>();
|
|
_appsflyer = ServiceLocator.Get<IAppsFlyerService>();
|
|
|
|
Debug.Log("[AnalyticsService] ✅ Analytics service ready. Sub-services initialized via registry.");
|
|
}
|
|
|
|
protected override void OnDispose()
|
|
{
|
|
_firebase = null;
|
|
_facebook = null;
|
|
_appsflyer = null;
|
|
Debug.Log("[AnalyticsService] 🔻 Disposed.");
|
|
}
|
|
|
|
// --------------------------------------------------------
|
|
// Parameter Mapping Helpers
|
|
// --------------------------------------------------------
|
|
private static Dictionary<string, object> MapParameters(
|
|
Dictionary<string, object> input,
|
|
Func<AnalyticsEventType, string> nameMapper)
|
|
{
|
|
if (input == null) return null;
|
|
var mapped = new Dictionary<string, object>();
|
|
|
|
foreach (var kvp in input)
|
|
if (Enum.TryParse(kvp.Key, true, out AnalyticsEventType key))
|
|
mapped[nameMapper(key)] = kvp.Value;
|
|
else
|
|
mapped[kvp.Key] = kvp.Value;
|
|
|
|
return mapped;
|
|
}
|
|
|
|
private static Dictionary<string, string> MapParametersToString(
|
|
Dictionary<string, object> input,
|
|
Func<AnalyticsEventType, string> nameMapper)
|
|
{
|
|
if (input == null) return null;
|
|
var mapped = new Dictionary<string, string>();
|
|
|
|
foreach (var kvp in input)
|
|
if (Enum.TryParse(kvp.Key, true, out AnalyticsEventType key))
|
|
mapped[nameMapper(key)] = kvp.Value?.ToString();
|
|
else
|
|
mapped[kvp.Key] = kvp.Value?.ToString();
|
|
|
|
return mapped;
|
|
}
|
|
}
|
|
#else
|
|
/// <summary>
|
|
/// Unified Analytics Service that bridges Firebase, AppsFlyer, and Facebook SDKs.
|
|
/// </summary>
|
|
public class AnalyticsService : ServiceBase, IAnalyticsService
|
|
{
|
|
private IAppsFlyerService _appsflyer;
|
|
private ShiroginConfig _config;
|
|
private IFacebookService _facebook;
|
|
|
|
private IFirebaseService _firebase;
|
|
|
|
// --------------------------------------------------------
|
|
// Unified Event Logging
|
|
// --------------------------------------------------------
|
|
public void LogEvent(AnalyticsEventType eventType, Dictionary<string, object> parameters = null)
|
|
{
|
|
Debug.Log($"[AnalyticsService] Dummy Logging event: {eventType}");
|
|
}
|
|
|
|
protected override void OnInitialize()
|
|
{
|
|
_config = ShiroginConfig.Load();
|
|
if (_config == null)
|
|
{
|
|
Debug.LogError("[ShiroginSDK] AnalyticsService failed to initialize — missing SDKConfig.");
|
|
return;
|
|
}
|
|
|
|
// Resolve dependencies
|
|
_firebase = ServiceLocator.Get<IFirebaseService>();
|
|
_facebook = ServiceLocator.Get<IFacebookService>();
|
|
_appsflyer = ServiceLocator.Get<IAppsFlyerService>();
|
|
|
|
Debug.Log("[AnalyticsService] ✅ Analytics service ready. Sub-services initialized via registry.");
|
|
}
|
|
|
|
protected override void OnDispose()
|
|
{
|
|
_firebase = null;
|
|
_facebook = null;
|
|
_appsflyer = null;
|
|
Debug.Log("[AnalyticsService] 🔻 Disposed.");
|
|
}
|
|
|
|
// --------------------------------------------------------
|
|
// Parameter Mapping Helpers
|
|
// --------------------------------------------------------
|
|
private static Dictionary<string, object> MapParameters(
|
|
Dictionary<string, object> input,
|
|
Func<AnalyticsEventType, string> nameMapper)
|
|
{
|
|
if (input == null) return null;
|
|
var mapped = new Dictionary<string, object>();
|
|
|
|
foreach (var kvp in input)
|
|
if (Enum.TryParse(kvp.Key, true, out AnalyticsEventType key))
|
|
mapped[nameMapper(key)] = kvp.Value;
|
|
else
|
|
mapped[kvp.Key] = kvp.Value;
|
|
|
|
return mapped;
|
|
}
|
|
|
|
private static Dictionary<string, string> MapParametersToString(
|
|
Dictionary<string, object> input,
|
|
Func<AnalyticsEventType, string> nameMapper)
|
|
{
|
|
if (input == null) return null;
|
|
var mapped = new Dictionary<string, string>();
|
|
|
|
foreach (var kvp in input)
|
|
if (Enum.TryParse(kvp.Key, true, out AnalyticsEventType key))
|
|
mapped[nameMapper(key)] = kvp.Value?.ToString();
|
|
else
|
|
mapped[kvp.Key] = kvp.Value?.ToString();
|
|
|
|
return mapped;
|
|
}
|
|
}
|
|
#endif
|
|
} |