using Microsoft.AppCenter; using Microsoft.AppCenter.Analytics; using Microsoft.AppCenter.Crashes; using System; using System.Linq; using System.Collections.Generic; using System.Diagnostics; using Windows.ApplicationModel; using Windows.Storage; namespace FoxTube.Utils { public static class Metrics { static readonly ApplicationDataContainer storage = ApplicationData.Current.RoamingSettings; static readonly Stopwatch sw = new Stopwatch(); public static TimeSpan Uptime { get => (TimeSpan?)storage.Values["Metrics.SpentTime"] ?? TimeSpan.FromSeconds(0); set => storage.Values["Metrics.SpentTime"] = value; } public static string CurrentVersion { get { PackageVersion v = Package.Current.Id.Version; return $"{v.Major}.{v.Minor}.{v.Revision}.{v.Build}"; } } static Metrics() { sw.Start(); if (!Settings.AllowAnalytics) return; AppCenter.Start("45774462-9ea7-438a-96fc-03982666f39e", typeof(Analytics), typeof(Crashes)); AppCenter.SetCountryCode(Settings.Region); AppCenter.LogLevel = LogLevel.Verbose; } public static void EndSession() { sw.Stop(); Uptime += sw.Elapsed; AddEvent("Session closed", ("Duration", sw.Elapsed.ToString()), ("Spend time total", Uptime.ToString())); } public static void AddEvent(string eventName, params (string key, string value)[] details) => Analytics.TrackEvent(eventName, details.Length < 1 ? null : details.Select(i => new KeyValuePair(i.key, i.value)) as Dictionary); public static void SendReport(Exception exception, ErrorAttachmentLog[] logs = null, params (string key, string value)[] details) { Crashes.TrackError(exception, details.Length < 1 ? null : details.Select(i => new KeyValuePair(i.key, i.value)) as Dictionary, logs); } } }