Playvoi/Assets/ShiroginSDK/Runtime/Services/Implementations/AppsFlyerServiceNS/AppsFlyerService.cs
2025-10-30 22:48:16 +03:00

117 lines
No EOL
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using AppsFlyerSDK;
using ShiroginSDK.Runtime.Core.SDK;
using ShiroginSDK.Runtime.Services.Base;
using ShiroginSDK.Runtime.Services.Interfaces;
using UnityEngine;
// artık çakışma yok
namespace ShiroginSDK.Runtime.Services.Implementations.AppsFlyerServiceNS
{
/// <summary>
/// Handles AppsFlyer initialization and event tracking for analytics and attribution.
/// </summary>
public class AppsFlyerService : ServiceBase, IAppsFlyerService, IAppsFlyerConversionData
{
private ShiroginConfig _config;
// ----------------------------------------------------
// Conversion & Attribution Callbacks
// ----------------------------------------------------
public void onConversionDataSuccess(string conversionData)
{
Debug.Log($"[AppsFlyerService] 📈 Conversion data received: {conversionData}");
}
public void onConversionDataFail(string error)
{
Debug.LogError($"[AppsFlyerService] ❌ Conversion data failed: {error}");
}
public void onAppOpenAttribution(string attributionData)
{
Debug.Log($"[AppsFlyerService] 🔗 App open attribution: {attributionData}");
}
public void onAppOpenAttributionFailure(string error)
{
Debug.LogError($"[AppsFlyerService] ❌ Attribution failed: {error}");
}
public bool IsInitialized { get; private set; }
// ----------------------------------------------------
// Event Tracking
// ----------------------------------------------------
public void TrackPurchase(string productId, string currency, double price, int quantity = 1)
{
if (!IsInitialized)
{
Debug.LogWarning("[AppsFlyerService] ⚠️ TrackPurchase called before initialization.");
return;
}
var eventValues = new Dictionary<string, string>
{
{ AFInAppEvents.CONTENT_ID, productId },
{ AFInAppEvents.CURRENCY, currency },
{ AFInAppEvents.REVENUE, price.ToString("F2") },
{ AFInAppEvents.QUANTITY, quantity.ToString() }
};
AppsFlyer.sendEvent(AFInAppEvents.PURCHASE, eventValues);
Debug.Log($"[AppsFlyerService] 💰 Purchase tracked: {productId} ({price} {currency})");
}
public void TrackCustomEvent(string eventName, Dictionary<string, string> eventValues = null)
{
if (!IsInitialized)
{
Debug.LogWarning("[AppsFlyerService] ⚠️ TrackCustomEvent called before initialization.");
return;
}
AppsFlyer.sendEvent(eventName, eventValues ?? new Dictionary<string, string>());
Debug.Log($"[AppsFlyerService] 🧭 Custom event sent: {eventName}");
}
// ----------------------------------------------------
// Initialization
// ----------------------------------------------------
protected override void OnInitialize()
{
_config = ShiroginConfig.Load();
if (_config == null)
{
Debug.LogError("[AppsFlyerService] ❌ SDKConfig not found.");
return;
}
if (string.IsNullOrEmpty(_config.appsFlyerDevKey) || string.IsNullOrEmpty(_config.appsFlyerAppId))
{
Debug.LogError("[AppsFlyerService] ❌ AppsFlyer DevKey or AppId missing in SDKConfig.");
return;
}
#if UNITY_IOS
AppsFlyer.initSDK(_config.appsFlyerDevKey, _config.appsFlyerAppId);
#elif UNITY_ANDROID
AppsFlyer.initSDK(_config.appsFlyerDevKey, _config.appsFlyerAppId);
#else
Debug.LogWarning("[AppsFlyerService] ⚠️ Unsupported platform for AppsFlyer.");
return;
#endif
AppsFlyer.startSDK();
IsInitialized = true;
Debug.Log("[AppsFlyerService] ✅ AppsFlyer initialized successfully.");
}
protected override void OnDispose()
{
IsInitialized = false;
}
}
}