diff --git a/source/plugin/Assets/GoogleMobileAds/Common/InsightTimer.cs b/source/plugin/Assets/GoogleMobileAds/Common/InsightTimer.cs new file mode 100644 index 000000000..7d7af84d8 --- /dev/null +++ b/source/plugin/Assets/GoogleMobileAds/Common/InsightTimer.cs @@ -0,0 +1,46 @@ +using System; +using System.Diagnostics; +using UnityEngine; + +namespace GoogleMobileAds.Common +{ + /// + /// A helper class to time an operation and emit an insight with the duration. + /// + public class InsightTimer + { + private Insight _insight; + private Stopwatch _stopwatch; + + /// + /// Creates an instance of InsightTimer and starts timing. + /// + /// The insight to emit when End() is called. + public InsightTimer(Insight insight) + { + _insight = insight; + if (_insight.Tracing == null) + { + _insight.Tracing = new Insight.TracingActivity(); + } + _insight.Tracing.HasEnded = false; + _stopwatch = new Stopwatch(); + _stopwatch.Start(); + } + + /// + /// Stops the timer, calculates the duration, and emits the insight. + /// + public void End() + { + if (_insight == null || _insight.Tracing == null || _insight.Tracing.HasEnded) + { + return; + } + _stopwatch.Stop(); + _insight.Tracing.DurationMillis = _stopwatch.ElapsedMilliseconds; + _insight.Tracing.HasEnded = true; + InsightsEmitter.Instance.Emit(_insight); + } + } +} diff --git a/source/plugin/Assets/GoogleMobileAds/Platforms/Android/MobileAdsClient.cs b/source/plugin/Assets/GoogleMobileAds/Platforms/Android/MobileAdsClient.cs index d5b177a92..056bb848a 100644 --- a/source/plugin/Assets/GoogleMobileAds/Platforms/Android/MobileAdsClient.cs +++ b/source/plugin/Assets/GoogleMobileAds/Platforms/Android/MobileAdsClient.cs @@ -32,6 +32,7 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient private readonly IInsightsEmitter _insightsEmitter = InsightsEmitter.Instance; private readonly ITracer _tracer; private Action _initCompleteAction; + private InsightTimer _initializationInsightTimer; private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) { _mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName); @@ -51,6 +52,10 @@ public void Initialize(Action initCompleteAction) using (_tracer.StartTrace("MobileAdsClient.Initialize")) { _initCompleteAction = initCompleteAction; + _initializationInsightTimer = new InsightTimer(new Insight() + { + Name = Insight.CuiName.SdkInitialized + }); Task.Run(() => { using (_tracer.StartTrace("AttachCurrentThread")) @@ -71,10 +76,6 @@ public void Initialize(Action initCompleteAction) } }); } - _insightsEmitter.Emit(new Insight() - { - Name = Insight.CuiName.SdkInitialized - }); } public void SetApplicationVolume(float volume) @@ -175,6 +176,10 @@ public Version GetSDKVersion() public void onInitializationComplete(AndroidJavaObject initStatus) { + if (_initializationInsightTimer != null) + { + _initializationInsightTimer.End(); + } if (_initCompleteAction != null) { IInitializationStatusClient statusClient = new InitializationStatusClient(initStatus); _initCompleteAction(statusClient);