From d22322ee261cab3ce3837351e1f2b315269d5d8b Mon Sep 17 00:00:00 2001 From: huyufei Date: Wed, 7 Aug 2024 22:09:42 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BC=98=E5=8C=96=E4=BA=86=20AdjustS?= =?UTF-8?q?ervice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huyufei --- Runtime/GuruAdjust/AdjustService.cs | 81 ++++++++++------------------- 1 file changed, 27 insertions(+), 54 deletions(-) diff --git a/Runtime/GuruAdjust/AdjustService.cs b/Runtime/GuruAdjust/AdjustService.cs index 259fc7c..9eb692e 100644 --- a/Runtime/GuruAdjust/AdjustService.cs +++ b/Runtime/GuruAdjust/AdjustService.cs @@ -14,14 +14,11 @@ namespace Guru private const string Version = "1.6.1"; private const string AdjustVersion = "4.38.0"; // Adjust SDK Version private const string LOG_TAG = "[ADJUST]"; - private const double delayTime = 1; // 延迟启动时间(s) + private const double delayTime = 5; // 延迟启动时间(s) private const string K_IAP_PURCHASE = "iap_purchase"; // 固定点位事件 private const string K_SUB_PURCHASE = "sub_purchase"; // 固定点位事件 - - private static Action _onInitComplete; - private static Action _onGetGoogleAdidHandler; - + private string _googleAdId = ""; public string GoogleAdId // GPS = Google Play Service { @@ -45,8 +42,8 @@ namespace Guru } } - private static bool _isReady = false; - public static bool IsReady => _isReady; + private bool _isReady = false; + public bool IsReady => _isReady; private static AdjustService _instance; @@ -73,7 +70,7 @@ namespace Guru /// /// /// - public void StartService(string appToken, string fbAppId = "", string firebaseId = "", string deviceId = "", + public async void Start(string appToken, string fbAppId = "", string firebaseId = "", string deviceId = "", Action onInitComplete = null, Action onDeeplinkCallback = null, Action onGetGoogleAdIdCallback = null, @@ -85,16 +82,24 @@ namespace Guru return; } - _onInitComplete = onInitComplete; - _onGetGoogleAdidHandler = onGetGoogleAdIdCallback; - - InstallEvent(firebaseId, deviceId); // 需要在 Adjust.start 前设置 <安装归因参数> + // 需要在 Adjust.start 前设置 <安装归因参数> + if (!string.IsNullOrEmpty(firebaseId)) + { + Adjust.addSessionCallbackParameter("user_pseudo_id", firebaseId); + } + if (!string.IsNullOrEmpty(deviceId)) + { + Adjust.addSessionCallbackParameter("device_id", deviceId); + } + + // 初始化启动 Config AdjustEnvironment environment = GetAdjustEnvironment(); AdjustConfig config = new AdjustConfig(appToken, environment); config.setPreinstallTrackingEnabled(true); // Adjust Preinstall config.setLogLevel(GetLogLevel(showLogs)); config.setDelayStart(delayTime); // 延迟 1s 启动 Adjust,保证 <安装归因参数> 成功注入 + #if UNITY_ANDROID if (!string.IsNullOrEmpty(fbAppId)) config.setFbAppId(fbAppId); // 注入 MIR ID #endif @@ -116,31 +121,29 @@ namespace Guru Adjust.start(config); // 启动服务 // 异步加载AdId - FetchGoogleAdIdAsync(); - + FetchGoogleAdIdAsync(onGetGoogleAdIdCallback); LogI(LOG_TAG, $"----- Start AdjustService[{Version}] AdjustVer:{AdjustVersion} -----"); + // 异步等待延时初始化执行成功 - WaitInitCompleteAsync(delayTime * 2); + // TODO: 应该在此处类似 Firebase 的continueWithMainThread 的能力的一个函数 + // TODO: 应该让以下的任务推迟 Ns 在主线程执行 + await Task.Delay(TimeSpan.FromMilliseconds((delayTime + 0.1) * 1000)); + _isReady = true; + onInitComplete?.Invoke(Adjust.getAdid(), Adjust.getIdfv(), Adjust.getIdfa()); } - private async void WaitInitCompleteAsync(double delaySeconds) - { - await Task.Delay(TimeSpan.FromMilliseconds(delaySeconds * 1000)); - _isReady = true; - _onInitComplete?.Invoke(Adjust.getAdid(), Adjust.getIdfv(), Adjust.getIdfa()); - } /// /// 异步拉取 Google Ad Id /// - private void FetchGoogleAdIdAsync() + private void FetchGoogleAdIdAsync(Action onGetGoogleAdIdCallback = null) { Adjust.getGoogleAdId(gid => { if (!string.IsNullOrEmpty(gid)) { _googleAdId = gid; // 获取Google AD ID - _onGetGoogleAdidHandler?.Invoke(_googleAdId); // 返回 GoogleAdid + onGetGoogleAdIdCallback?.Invoke(_googleAdId); // 返回 GoogleAdid } }); } @@ -162,37 +165,7 @@ namespace Guru } #endregion - - #region 关键属性上报 - - /// - /// 安装事件上报 - /// 该事件只有第一次上报有效 - /// pseudoId 为空时不上报 - /// deviceId 为空时也不上报 (一般不会为空) - /// - /// - /// - private 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}"); - Adjust.addSessionCallbackParameter("user_pseudo_id", pseudoId); - Adjust.addSessionCallbackParameter("device_id", deviceId); - } - - - #endregion + #region 事件回调函数 /*