Skip to content

Commit

Permalink
Add appcenter
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Aug 26, 2023
1 parent 3de2de7 commit cecdfb4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
19 changes: 18 additions & 1 deletion OnlyM/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
#if !DEBUG
#define USE_APP_CENTER
#endif

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
Expand Down Expand Up @@ -27,6 +31,8 @@
using OnlyM.Services.WebBrowser;
using OnlyM.ViewModel;
using Serilog;
using Microsoft.AppCenter;
using OnlyM.EventTracking;

namespace OnlyM
{
Expand Down Expand Up @@ -68,6 +74,8 @@ protected override void OnExit(ExitEventArgs e)

protected override void OnStartup(StartupEventArgs e)
{
ConfigureAppCenter();

if (AnotherInstanceRunning())
{
Shutdown();
Expand Down Expand Up @@ -186,6 +194,9 @@ private static bool InitCef()
private void CurrentDispatcherUnhandledException(object? sender, DispatcherUnhandledExceptionEventArgs e)
{
// unhandled exceptions thrown from UI thread

EventTracker.Error(e.Exception, "Unhandled exception");

e.Handled = true;
Log.Logger.Fatal(e.Exception, "Unhandled exception");
Current.Shutdown();
Expand All @@ -199,5 +210,11 @@ private static void AddEventLogEntry(string msg)
eventLog.WriteEntry(msg, EventLogEntryType.Error);
}
}

[Conditional("USE_APP_CENTER")]
private static void ConfigureAppCenter()
{
AppCenterInit.Execute();
}
}
}
24 changes: 24 additions & 0 deletions OnlyM/AppCenterInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Globalization;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;

namespace OnlyM;

internal static class AppCenterInit
{
// Please omit this token (or use your own) if you are building a fork
private static readonly string? TheToken = "78e43751-d0c9-4fc7-8137-0fcc95ebd0fe";

public static void Execute()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
AppCenter.Start(TheToken, typeof(Analytics), typeof(Crashes));
AppCenter.SetCountryCode(RegionInfo.CurrentRegion.TwoLetterISORegionName);
#pragma warning restore CA1416
}
}
}
7 changes: 7 additions & 0 deletions OnlyM/EventTracking/EventName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OnlyM.EventTracking;

internal enum EventName
{
Unknown,
StartMedia,
}
42 changes: 42 additions & 0 deletions OnlyM/EventTracking/EventTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using OnlyM.Core.Models;

namespace OnlyM.EventTracking;

#pragma warning disable CA1416 // Validate platform compatibility

internal static class EventTracker
{
public static void Track(EventName eventName, Dictionary<string, string>? properties = null)
{
Analytics.TrackEvent(eventName.ToString(), properties);
}

public static void TrackStartMedia(SupportedMediaType? mediaType)
{
var properties = new Dictionary<string, string>
{
{ "type", mediaType?.Classification.ToString() ?? "No Type" },
};

Track(EventName.StartMedia, properties);
}

public static void Error(Exception ex, string? context = null)
{
if (string.IsNullOrEmpty(context))
{
Crashes.TrackError(ex);
}
else
{
var properties = new Dictionary<string, string> { { "context", context } };
Crashes.TrackError(ex, properties);
}
}
}

#pragma warning restore CA1416 // Validate platform compatibility}
2 changes: 2 additions & 0 deletions OnlyM/OnlyM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="FFME.Windows" Version="4.4.350" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.2" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.2" />
<PackageReference Include="NAudio" Version="2.2.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
Expand Down
5 changes: 4 additions & 1 deletion OnlyM/Services/Pages/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using OnlyM.Core.Services.Options;
using OnlyM.CoreSys.Services.Snackbar;
using OnlyM.CoreSys.WindowsPositioning;
using OnlyM.EventTracking;
using OnlyM.MediaElementAdaption;
using OnlyM.Models;
using OnlyM.PubSubMessages;
Expand Down Expand Up @@ -180,7 +181,9 @@ public async Task StartMedia(
OpenMediaWindow(requiresVisibleWindow, mediaItemToStart.IsVideo);

CheckMediaWindow();


EventTracker.TrackStartMedia(mediaItemToStart.MediaType);

await _mediaWindow!.StartMedia(
mediaItemToStart,
currentMediaItems,
Expand Down

0 comments on commit cecdfb4

Please sign in to comment.