diff --git a/CommunityToolkit.App.Shared/App.xaml.cs b/CommunityToolkit.App.Shared/App.xaml.cs index ba5c5884..0b7fd28f 100644 --- a/CommunityToolkit.App.Shared/App.xaml.cs +++ b/CommunityToolkit.App.Shared/App.xaml.cs @@ -2,7 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Windows.UI; +using CommunityToolkit.App.Shared.Helpers; + +#if WINAPPSDK +using UnhandledExceptionEventArgs = Microsoft.UI.Xaml.UnhandledExceptionEventArgs; +#else +using UnhandledExceptionEventArgs = Windows.UI.Xaml.UnhandledExceptionEventArgs; +#endif namespace CommunityToolkit.App.Shared; @@ -26,6 +32,13 @@ public sealed partial class App : Application public App() { this.InitializeComponent(); + + UnhandledException += this.App_UnhandledException; + } + + private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + TrackingManager.TrackException(e.Exception); } /// diff --git a/CommunityToolkit.App.Shared/CommunityToolkit.App.Shared.projitems b/CommunityToolkit.App.Shared/CommunityToolkit.App.Shared.projitems index 732e956e..9cb1bc13 100644 --- a/CommunityToolkit.App.Shared/CommunityToolkit.App.Shared.projitems +++ b/CommunityToolkit.App.Shared/CommunityToolkit.App.Shared.projitems @@ -32,6 +32,7 @@ + GettingStartedPage.xaml diff --git a/CommunityToolkit.App.Shared/Helpers/TrackingManager.cs b/CommunityToolkit.App.Shared/Helpers/TrackingManager.cs new file mode 100644 index 00000000..b704e442 --- /dev/null +++ b/CommunityToolkit.App.Shared/Helpers/TrackingManager.cs @@ -0,0 +1,78 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +#if !DEBUG && WINDOWS_UWP && !HAS_UNO +// See https://learn.microsoft.com/windows/uwp/monetize/log-custom-events-for-dev-center +using Microsoft.Services.Store.Engagement; +#endif + +namespace CommunityToolkit.App.Shared.Helpers; + +#if DEBUG || !WINDOWS_UWP || HAS_UNO +// Stub for non-UWP platforms and DEBUG mode +internal class StoreServicesCustomEventLogger +{ + public static StoreServicesCustomEventLogger GetDefault() => new(); + + public void Log(string message) + { + Debug.WriteLine($"Log - {message}"); + } +} +#endif + +public static class TrackingManager +{ + private static StoreServicesCustomEventLogger? logger; + + static TrackingManager() + { + try + { + logger = StoreServicesCustomEventLogger.GetDefault(); + } + catch + { + // Ignoring error + } + } + + public static void TrackException(Exception ex) + { + try + { + logger?.Log($"exception - {ex.Message} - {ex.StackTrace}"); + } + catch + { + // Ignore error + } + } + + public static void TrackSample(string name) + { + try + { + logger?.Log($"sample - {name}"); + } + catch + { + // Ignore error + } + } + + public static void TrackPage(string pageName) + { + try + { + logger?.Log($"openpage - {pageName}"); + } + catch + { + // Ignore error + } + } +} diff --git a/CommunityToolkit.App.Shared/Pages/Shell.xaml.cs b/CommunityToolkit.App.Shared/Pages/Shell.xaml.cs index 2da7650a..535499f5 100644 --- a/CommunityToolkit.App.Shared/Pages/Shell.xaml.cs +++ b/CommunityToolkit.App.Shared/Pages/Shell.xaml.cs @@ -71,6 +71,7 @@ private void NavView_ItemInvoked(MUXC.NavigationView sender, MUXC.NavigationView if (NavigationFrame.CurrentSourcePageType != typeof(SettingsPage)) { NavigationFrame.Navigate(typeof(SettingsPage)); + TrackingManager.TrackPage("Settings"); } } @@ -78,6 +79,7 @@ private void NavView_ItemInvoked(MUXC.NavigationView sender, MUXC.NavigationView else if (selectedItem.Tag != null && selectedItem.Tag.GetType() == typeof(string)) { NavigationFrame.Navigate(typeof(GettingStartedPage), samplePages); + TrackingManager.TrackPage("GettingStarted"); } else { @@ -85,6 +87,7 @@ private void NavView_ItemInvoked(MUXC.NavigationView sender, MUXC.NavigationView if (selectedMetadata is null) return; NavigationFrame.Navigate(typeof(ToolkitDocumentationRenderer), selectedMetadata); + TrackingManager.TrackSample(selectedMetadata.Title!); } } @@ -93,10 +96,12 @@ public void NavigateToSample(ToolkitFrontMatter? sample) if (sample is null) { NavigationFrame.Navigate(typeof(GettingStartedPage), samplePages); + TrackingManager.TrackPage("GettingStarted"); } else { NavigationFrame.Navigate(typeof(ToolkitDocumentationRenderer), sample); + TrackingManager.TrackSample(sample.Title!); } EnsureNavigationSelection(sample?.FilePath); diff --git a/ProjectHeads/AllComponents/Uwp/CommunityToolkit.App.Uwp.csproj b/ProjectHeads/AllComponents/Uwp/CommunityToolkit.App.Uwp.csproj index ef273d6b..c1358b89 100644 --- a/ProjectHeads/AllComponents/Uwp/CommunityToolkit.App.Uwp.csproj +++ b/ProjectHeads/AllComponents/Uwp/CommunityToolkit.App.Uwp.csproj @@ -10,6 +10,8 @@ 2 true + Always + x86|x64|arm64 diff --git a/ProjectHeads/App.Head.Uwp.Dependencies.props b/ProjectHeads/App.Head.Uwp.Dependencies.props index 14bc2164..b8194c8f 100644 --- a/ProjectHeads/App.Head.Uwp.Dependencies.props +++ b/ProjectHeads/App.Head.Uwp.Dependencies.props @@ -6,4 +6,11 @@ + + + + + Microsoft Engagement Framework + +