com.guru.unity.sdk.core/Runtime/GuruAdjust/AdjustService.cs

441 lines
11 KiB
C#
Raw Permalink Normal View History

2023-12-26 04:22:19 +00:00
namespace Guru
{
using UnityEngine;
using com.adjust.sdk;
using System;
using System.Collections;
2023-12-26 04:22:19 +00:00
public class AdjustService
2023-12-26 04:22:19 +00:00
{
public const string Version = "1.6.1";
public const string AdjustVersion = "4.38.0"; // Adjust SDK Version
2023-12-26 04:22:19 +00:00
public static readonly string LOG_TAG = "Adjust";
public static readonly float DelayTime = 1f; // 延迟启动时间
public const string K_IAP_PURCHASE = "iap_purchase"; // 固定点位事件
public const string K_SUB_PURCHASE = "sub_purchase"; // 固定点位事件
private static Action<string> _onSessionSuccessCallback;
private static string _adId = "";
2024-01-08 12:32:47 +00:00
public static string AdId
{
get
{
if(string.IsNullOrEmpty(_adId)) FetchGoogleAdId();
return _adId; // Google AdId
}
}
public static string IDFA => Adjust.getIdfa();
2024-01-10 05:34:45 +00:00
private static string _adjustId = "";
2024-01-08 12:32:47 +00:00
public static string AdjustId
{
get
{
2024-01-10 05:34:45 +00:00
if (string.IsNullOrEmpty(_adjustId)) _adjustId = Adjust.getAdid();
return _adjustId; // Adjust AdId;
2024-01-08 12:32:47 +00:00
}
}
2023-12-26 04:22:19 +00:00
#region 启动服务
2024-01-06 05:46:25 +00:00
/// <summary>
/// Adjust启动服务
/// </summary>
2024-01-10 05:34:45 +00:00
/// <param name="appToken"></param>
2024-01-06 05:46:25 +00:00
/// <param name="fbAppId">MIR 追踪 AppID</param>
public static void StartService(string appToken, string fbAppId = "", Action<string> onSessionSuccess = null)
2023-12-26 04:22:19 +00:00
{
if (string.IsNullOrEmpty(appToken))
{
LogE(LOG_TAG, "Adjust没有设置token无法进行初始化");
2023-12-26 04:22:19 +00:00
return;
}
2024-01-06 05:46:25 +00:00
_onSessionSuccessCallback = onSessionSuccess;
2023-12-26 04:22:19 +00:00
InstallEvent(IPMConfig.FIREBASE_ID, IPMConfig.IPM_DEVICE_ID); // 注入启动参数
AdjustEnvironment environment = GetAdjustEnvironment();
AdjustConfig config = new AdjustConfig(appToken, environment);
config.setLogLevel(GetAdjustLogLevel());
config.setDelayStart(DelayTime);
config.setPreinstallTrackingEnabled(true); // Adjust Preinstall
config.setSessionSuccessDelegate(OnSessionSuccessCallback); // SessionSuccess
2023-12-26 04:22:19 +00:00
2024-01-06 05:46:25 +00:00
#if UNITY_ANDROID
2024-01-10 05:34:45 +00:00
if (!string.IsNullOrEmpty(fbAppId)) config.setFbAppId(fbAppId); // 注入 MIR ID
2024-01-06 05:46:25 +00:00
#endif
2023-12-26 04:22:19 +00:00
#if UNITY_EDITOR || DEBUG
config.setLogDelegate(log => LogI(LOG_TAG, log));
2023-12-26 04:22:19 +00:00
config.setEventSuccessDelegate(OnEventSuccessCallback);
config.setEventFailureDelegate(OnEventFailureCallback);
2023-12-26 04:22:19 +00:00
config.setSessionFailureDelegate(OnSessionFailureCallback);
config.setAttributionChangedDelegate(OnAttributionChangedCallback);
#endif
// 检查场景实例
SetupInstance();
Adjust.start(config);
// 缓存标准属性
2024-01-10 05:34:45 +00:00
_adjustId = Adjust.getAdid(); // 获取AdjustID
// 异步加载AdId
FetchGoogleAdId();
LogI(LOG_TAG, $"----- Start AdjustService[{Version}] AdjustVer:{AdjustVersion} -----");
}
2024-01-08 12:32:47 +00:00
public static void FetchGoogleAdId()
{
Adjust.getGoogleAdId(gid =>
2023-12-26 04:22:19 +00:00
{
if (!string.IsNullOrEmpty(gid))
{
_adId = gid; // 获取Google AD ID
}
2023-12-26 04:22:19 +00:00
});
}
/// <summary>
/// 确保 Adjust 实例在场景中
/// </summary>
private static void SetupInstance()
{
var go = UnityEngine.GameObject.Find(nameof(Adjust));
if (go == null)
{
go = new GameObject(nameof(Adjust));
var ins = go.AddComponent<Adjust>();
ins.startManually = true;
ins.launchDeferredDeeplink = true;
ins.sendInBackground = true;
}
}
2024-01-10 05:34:45 +00:00
2023-12-26 04:22:19 +00:00
#endregion
#region 关键属性上报
/// <summary>
/// 安装事件上报
/// 该事件只有第一次上报有效
/// pseudoId 为空时不上报
/// deviceId 为空时也不上报 (一般不会为空)
/// </summary>
/// <param name="pseudoId"></param>
/// <param name="deviceId"></param>
public static void InstallEvent(string pseudoId, string deviceId)
{
if (string.IsNullOrEmpty(pseudoId))
{
Debug.LogWarning($">> Pseudo ID is Empty, skip install event reporting");
return;
}
if (string.IsNullOrEmpty(deviceId))
{
Debug.LogWarning($">> Device ID is Empty, skip install event reporting");
return;
}
Debug.LogWarning($"{LOG_TAG} --- addSessionCallbackParameter: user_pseudo_id:{pseudoId}, device_id:{deviceId}");
2023-12-26 04:22:19 +00:00
Adjust.addSessionCallbackParameter("user_pseudo_id", pseudoId);
Adjust.addSessionCallbackParameter("device_id", deviceId);
}
#endregion
#region 事件回调函数
private static void OnAttributionChangedCallback(AdjustAttribution attributionData)
{
LogI(LOG_TAG, "Attribution changed!");
2023-12-26 04:22:19 +00:00
if (attributionData.trackerName != null)
{
LogI(LOG_TAG, "Tracker name: " + attributionData.trackerName);
2023-12-26 04:22:19 +00:00
}
if (attributionData.trackerToken != null)
{
LogI(LOG_TAG, "Tracker token: " + attributionData.trackerToken);
2023-12-26 04:22:19 +00:00
}
if (attributionData.network != null)
{
LogI(LOG_TAG, "Network: " + attributionData.network);
2023-12-26 04:22:19 +00:00
}
if (attributionData.campaign != null)
{
LogI(LOG_TAG, "Campaign: " + attributionData.campaign);
2023-12-26 04:22:19 +00:00
}
if (attributionData.adgroup != null)
{
LogI(LOG_TAG, "Adgroup: " + attributionData.adgroup);
2023-12-26 04:22:19 +00:00
}
if (attributionData.creative != null)
{
LogI(LOG_TAG, "Creative: " + attributionData.creative);
2023-12-26 04:22:19 +00:00
}
if (attributionData.clickLabel != null)
{
LogI(LOG_TAG , "Click label: " + attributionData.clickLabel);
2023-12-26 04:22:19 +00:00
}
if (attributionData.adid != null)
{
LogI(LOG_TAG, "ADID: " + attributionData.adid);
2023-12-26 04:22:19 +00:00
}
}
private static void OnEventSuccessCallback(AdjustEventSuccess eventSuccessData)
{
LogI(LOG_TAG, "Event tracked successfully!");
2023-12-26 04:22:19 +00:00
if (eventSuccessData.Message != null)
{
LogI(LOG_TAG, "Message: " + eventSuccessData.Message);
2023-12-26 04:22:19 +00:00
}
if (eventSuccessData.Timestamp != null)
{
LogI(LOG_TAG, "Timestamp: " + eventSuccessData.Timestamp);
2023-12-26 04:22:19 +00:00
}
if (eventSuccessData.Adid != null)
{
LogI(LOG_TAG, "Adid: " + eventSuccessData.Adid);
2023-12-26 04:22:19 +00:00
}
if (eventSuccessData.EventToken != null)
{
LogI(LOG_TAG, "EventToken: " + eventSuccessData.EventToken);
2023-12-26 04:22:19 +00:00
}
if (eventSuccessData.CallbackId != null)
{
LogI(LOG_TAG, "CallbackId: " + eventSuccessData.CallbackId);
2023-12-26 04:22:19 +00:00
}
if (eventSuccessData.JsonResponse != null)
{
LogI(LOG_TAG, "JsonResponse: " + eventSuccessData.GetJsonResponse());
2023-12-26 04:22:19 +00:00
}
}
private static void OnEventFailureCallback(AdjustEventFailure eventFailureData)
{
LogI(LOG_TAG, "Event tracking failed!");
2023-12-26 04:22:19 +00:00
if (eventFailureData.Message != null)
{
LogI(LOG_TAG, "Message: " + eventFailureData.Message);
2023-12-26 04:22:19 +00:00
}
if (eventFailureData.Timestamp != null)
{
LogI(LOG_TAG, "Timestamp: " + eventFailureData.Timestamp);
2023-12-26 04:22:19 +00:00
}
if (eventFailureData.Adid != null)
{
LogI(LOG_TAG, "Adid: " + eventFailureData.Adid);
2023-12-26 04:22:19 +00:00
}
if (eventFailureData.EventToken != null)
{
LogI(LOG_TAG, "EventToken: " + eventFailureData.EventToken);
2023-12-26 04:22:19 +00:00
}
if (eventFailureData.CallbackId != null)
{
LogI(LOG_TAG, "CallbackId: " + eventFailureData.CallbackId);
2023-12-26 04:22:19 +00:00
}
if (eventFailureData.JsonResponse != null)
{
LogI(LOG_TAG, "JsonResponse: " + eventFailureData.GetJsonResponse());
2023-12-26 04:22:19 +00:00
}
LogI(LOG_TAG, "WillRetry: " + eventFailureData.WillRetry.ToString());
2023-12-26 04:22:19 +00:00
}
private static void OnSessionSuccessCallback(AdjustSessionSuccess sessionSuccessData)
{
LogI(LOG_TAG,$"{LOG_TAG} --- Session tracked successfully!");
2023-12-26 04:22:19 +00:00
var adid = sessionSuccessData.Adid;
_onSessionSuccessCallback?.Invoke(adid);
2023-12-26 04:22:19 +00:00
}
private static void OnSessionFailureCallback(AdjustSessionFailure sessionFailureData)
{
LogI(LOG_TAG,"Session tracking failed!");
2023-12-26 04:22:19 +00:00
if (sessionFailureData.Message != null)
{
LogI(LOG_TAG,"Message: " + sessionFailureData.Message);
2023-12-26 04:22:19 +00:00
}
if (sessionFailureData.Timestamp != null)
{
LogI(LOG_TAG,"Timestamp: " + sessionFailureData.Timestamp);
2023-12-26 04:22:19 +00:00
}
if (sessionFailureData.Adid != null)
{
LogI(LOG_TAG,"Adid: " + sessionFailureData.Adid);
2023-12-26 04:22:19 +00:00
}
if (sessionFailureData.JsonResponse != null)
{
LogI(LOG_TAG,"JsonResponse: " + sessionFailureData.GetJsonResponse());
2023-12-26 04:22:19 +00:00
}
LogI(LOG_TAG,"WillRetry: " + sessionFailureData.WillRetry.ToString());
2023-12-26 04:22:19 +00:00
}
#endregion
#region IAP收入上报
/// <summary>
/// IAP支付事件上报
/// </summary>
/// <param name="revenue"></param>
/// <param name="productID"></param>
public static void TrackIAPPurchase(double revenue, string productID)
{
string tokenID = Analytics.GetAdjustEventToken(K_IAP_PURCHASE);
2023-12-26 04:22:19 +00:00
if (string.IsNullOrEmpty(tokenID))
return;
AdjustEvent adjustEvent = new AdjustEvent(tokenID);
adjustEvent.setRevenue(revenue,"USD");
adjustEvent.AddEventParameter("platform", Analytics.IAPPlatform);
adjustEvent.AddEventParameter("product_id", productID);
adjustEvent.AddEventParameter("value", $"{revenue}");
Adjust.trackEvent(adjustEvent);
}
/// <summary>
/// IAP订阅支付事件上报
/// </summary>
/// <param name="revenue"></param>
/// <param name="productID"></param>
public static void TrackSubPurchase(double revenue, string productID)
{
string tokenID = Analytics.GetAdjustEventToken(K_SUB_PURCHASE);
if (string.IsNullOrEmpty(tokenID))
return;
AdjustEvent adjustEvent = new AdjustEvent(tokenID);
adjustEvent.setRevenue(revenue,"USD");
adjustEvent.AddEventParameter("platform", Analytics.IAPPlatform);
2023-12-26 04:22:19 +00:00
adjustEvent.AddEventParameter("product_id", productID);
adjustEvent.AddEventParameter("value", $"{revenue}");
Adjust.trackEvent(adjustEvent);
}
2024-01-10 05:34:45 +00:00
/// <summary>
/// 广告收入上报 (Adjust 特有的接口)
/// </summary>
/// <param name="adInfo"></param>
public static void TrackADRevenue(MaxSdkBase.AdInfo adInfo)
{
if (adInfo == null)
return;
var adRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceAppLovinMAX);
adRevenue.setRevenue(adInfo.Revenue, "USD");
adRevenue.setAdRevenueNetwork(adInfo.NetworkName);
adRevenue.setAdRevenueUnit(adInfo.AdUnitIdentifier);
adRevenue.setAdRevenuePlacement(adInfo.Placement);
Adjust.trackAdRevenue(adRevenue);
}
2023-12-26 04:22:19 +00:00
2023-12-26 04:22:19 +00:00
#endregion
#region 关键属性上报
/// <summary>
/// 上报PseudoId
/// </summary>
/// <param name="firebaseId"></param>
public static void ReportPseudoID(string firebaseId)
{
if (!string.IsNullOrEmpty(firebaseId))
{
Adjust.addSessionCallbackParameter("user_pseudo_id", firebaseId); // 关联 user_pseudo_id
}
}
/// <summary>
/// 上报DeviceId
/// </summary>
/// <param name="deviceId"></param>
public static void ReportDeviceId(string deviceId)
{
if (!string.IsNullOrEmpty(deviceId))
{
Adjust.addSessionCallbackParameter("device_id", deviceId); // 关联 user_pseudo_id
}
}
#endregion
2024-01-10 05:34:45 +00:00
#region 工具接口
2023-12-26 04:22:19 +00:00
private static AdjustEnvironment GetAdjustEnvironment()
{
#if UNITY_EDITOR || DEBUG
return AdjustEnvironment.Sandbox;
#else
return AdjustEnvironment.Production;
#endif
}
private static AdjustLogLevel GetAdjustLogLevel()
{
#if UNITY_EDITOR || DEBUG
return AdjustLogLevel.Verbose;
#else
return AdjustLogLevel.Suppress;
#endif
}
public static void LogI(string tag, object conent)
{
Debug.Log($"{tag} {conent}");
}
2023-12-26 04:22:19 +00:00
public static void LogE(string tag, object conent)
{
Debug.LogError($"{tag} {conent}");
}
2024-01-10 05:34:45 +00:00
#endregion
2023-12-26 04:22:19 +00:00
}
}