diff --git a/TinyInsights.TestApp/Platforms/Android/AndroidManifest.xml b/TinyInsights.TestApp/Platforms/Android/AndroidManifest.xml index e9937ad..fdb13fa 100644 --- a/TinyInsights.TestApp/Platforms/Android/AndroidManifest.xml +++ b/TinyInsights.TestApp/Platforms/Android/AndroidManifest.xml @@ -1,5 +1,6 @@  + diff --git a/TinyInsights.TestApp/TinyInsights.TestApp.csproj b/TinyInsights.TestApp/TinyInsights.TestApp.csproj index e922ce6..f70d56c 100644 --- a/TinyInsights.TestApp/TinyInsights.TestApp.csproj +++ b/TinyInsights.TestApp/TinyInsights.TestApp.csproj @@ -1,7 +1,7 @@  - net9.0-android;net9.0-ios;net9.0-maccatalyst + net9.0-ios;net9.0-maccatalyst;net9.0-android35.0 $(TargetFrameworks);net9.0-windows10.0.19041.0 @@ -34,7 +34,7 @@ 11.0 13.1 - 21.0 + 30.0 10.0.17763.0 10.0.17763.0 6.5 diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index 76f2145..b3989a1 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -98,8 +98,12 @@ public void Initialize() { throw new NullReferenceException("Unable to configure `IsAutoTrackPageViewsEnabled` as `Application.Current` is null. You can either set `IsAutoTrackPageViewsEnabled` to false to ignore this issue, or check out this link for a possible reason - https://github.com/dhindrik/TinyInsights.Maui/issues/21"); } - WeakEventHandler weakHandler = new(OnAppearing); - Application.Current.PageAppearing += weakHandler.Handler; + + WeakEventHandler weakOnAppearingHandler = new(OnAppearing); + Application.Current.PageAppearing += weakOnAppearingHandler.Handler; + + WeakEventHandler weakOnDisappearingHandler = new(OnDisappearing); + Application.Current.PageDisappearing += weakOnDisappearingHandler.Handler; } Task.Run(SendCrashes); @@ -107,10 +111,19 @@ public void Initialize() IsInitialized = true; } + private static DateTime? _lastPageAppearing; + private static void OnAppearing(object? sender, Page e) { + _lastPageAppearing = DateTime.Now; + } + + private static void OnDisappearing(object? sender, Page e) + { + var duration = DateTime.Now - _lastPageAppearing; + var pageType = e.GetType(); - provider?.TrackPageViewAsync(pageType.FullName ?? pageType.Name, new Dictionary { { "DisplayName", pageType.Name } }); + provider?.TrackPageViewAsync(pageType.FullName ?? pageType.Name, new Dictionary { { "DisplayName", pageType.Name } }, duration); } readonly Dictionary _globalProperties = []; @@ -425,7 +438,7 @@ public async Task TrackEventAsync(string eventName, Dictionary? } } - public async Task TrackPageViewAsync(string viewName, Dictionary? properties = null) + public async Task TrackPageViewAsync(string viewName, Dictionary? properties = null, TimeSpan? duration = null) { try { @@ -437,7 +450,26 @@ public async Task TrackPageViewAsync(string viewName, Dictionary if (EnableConsoleLogging) Console.WriteLine($"TinyInsights: tracking page view {viewName}"); - Client.TrackPageView(viewName); + var pageView = new PageViewTelemetry(viewName) + { + Timestamp = new DateTimeOffset(_lastPageAppearing ?? DateTime.Now), + }; + + if (duration is not null) + { + pageView.Duration = duration.Value; + } + + if (properties is not null) + { + foreach (var property in properties) + { + pageView.Properties.Add(property.Key, property.Value); + } + } + + Client.TrackPageView(pageView); + await Client.FlushAsync(CancellationToken.None); } catch (Exception ex) diff --git a/TinyInsights/IInsights.cs b/TinyInsights/IInsights.cs index d26c206..d024d4f 100644 --- a/TinyInsights/IInsights.cs +++ b/TinyInsights/IInsights.cs @@ -10,7 +10,7 @@ public interface IInsights Task TrackErrorAsync(Exception ex, Dictionary? properties = null); - Task TrackPageViewAsync(string viewName, Dictionary? properties = null); + Task TrackPageViewAsync(string viewName, Dictionary? properties = null, TimeSpan? duration = null); Task TrackEventAsync(string eventName, Dictionary? properties = null); Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary? properties = null); diff --git a/TinyInsights/IInsightsProvider.cs b/TinyInsights/IInsightsProvider.cs index 3cfd4d4..7e8d1e3 100644 --- a/TinyInsights/IInsightsProvider.cs +++ b/TinyInsights/IInsightsProvider.cs @@ -18,7 +18,7 @@ public interface IInsightsProvider Task TrackErrorAsync(Exception ex, Dictionary? properties = null); - Task TrackPageViewAsync(string viewName, Dictionary? properties = null); + Task TrackPageViewAsync(string viewName, Dictionary? properties = null, TimeSpan? duration = null); Task TrackEventAsync(string eventName, Dictionary? properties = null); diff --git a/TinyInsights/Insights.cs b/TinyInsights/Insights.cs index 9137dd2..22e2c20 100644 --- a/TinyInsights/Insights.cs +++ b/TinyInsights/Insights.cs @@ -57,13 +57,13 @@ public Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary? properties = null) + public Task TrackPageViewAsync(string viewName, Dictionary? properties = null, TimeSpan? duration = null) { var tasks = new List(); foreach (var provider in insightsProviders.Where(x => x.IsTrackPageViewsEnabled)) { - var task = provider.TrackPageViewAsync(viewName, properties); + var task = provider.TrackPageViewAsync(viewName, properties, duration); tasks.Add(task); }