From ad796a47bd119fd6b068c4af31b196e53809f344 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Thu, 8 Dec 2016 14:22:28 +0000 Subject: [PATCH] Small bit of analytics --- PlexRequests.Helpers/Analytics/Analytics.cs | 7 +++++-- PlexRequests.Helpers/Analytics/Category.cs | 3 ++- PlexRequests.Helpers/Analytics/IAnalytics.cs | 2 +- .../Modules/Admin/SystemStatusModule.cs | 10 ++++++++-- PlexRequests.UI/Modules/UserManagementModule.cs | 16 +++++++++++----- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/PlexRequests.Helpers/Analytics/Analytics.cs b/PlexRequests.Helpers/Analytics/Analytics.cs index 764295fa3..b38daca12 100644 --- a/PlexRequests.Helpers/Analytics/Analytics.cs +++ b/PlexRequests.Helpers/Analytics/Analytics.cs @@ -54,7 +54,7 @@ namespace PlexRequests.Helpers.Analytics Track(HitType.@event, username, cat, act, label, clientId, value); } - public async Task TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null) + public async void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null) { var cat = category.ToString(); var act = action.ToString(); @@ -146,7 +146,7 @@ namespace PlexRequests.Helpers.Analytics request.Method = RequestMethod; // set the Content-Length header to the correct value request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); - +#if !DEBUG // write the request body to the request using (var writer = new StreamWriter(request.GetRequestStream())) { @@ -165,6 +165,7 @@ namespace PlexRequests.Helpers.Analytics { Log.Error(ex, "Analytics tracking failed"); } +#endif } private async Task SendRequestAsync(string postDataString) { @@ -173,6 +174,7 @@ namespace PlexRequests.Helpers.Analytics // set the Content-Length header to the correct value request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); +#if !DEBUG // write the request body to the request using (var writer = new StreamWriter(await request.GetRequestStreamAsync())) { @@ -191,6 +193,7 @@ namespace PlexRequests.Helpers.Analytics { Log.Error(ex, "Analytics tracking failed"); } +#endif } private Dictionary BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal) diff --git a/PlexRequests.Helpers/Analytics/Category.cs b/PlexRequests.Helpers/Analytics/Category.cs index 33d0cecf1..e15173672 100644 --- a/PlexRequests.Helpers/Analytics/Category.cs +++ b/PlexRequests.Helpers/Analytics/Category.cs @@ -38,6 +38,7 @@ namespace PlexRequests.Helpers.Analytics Issues, UserLogin, Services, - Navbar + Navbar, + UserManagement } } \ No newline at end of file diff --git a/PlexRequests.Helpers/Analytics/IAnalytics.cs b/PlexRequests.Helpers/Analytics/IAnalytics.cs index d21dd18d1..b808c392a 100644 --- a/PlexRequests.Helpers/Analytics/IAnalytics.cs +++ b/PlexRequests.Helpers/Analytics/IAnalytics.cs @@ -51,7 +51,7 @@ namespace PlexRequests.Helpers.Analytics /// The client identifier. /// The value. /// - Task TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null); + void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null); /// /// Tracks the page view. diff --git a/PlexRequests.UI/Modules/Admin/SystemStatusModule.cs b/PlexRequests.UI/Modules/Admin/SystemStatusModule.cs index 9218df1f7..3f5b33947 100644 --- a/PlexRequests.UI/Modules/Admin/SystemStatusModule.cs +++ b/PlexRequests.UI/Modules/Admin/SystemStatusModule.cs @@ -37,8 +37,8 @@ using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Core.StatusChecker; using PlexRequests.Helpers; +using PlexRequests.Helpers.Analytics; using PlexRequests.Helpers.Permissions; -using PlexRequests.UI.Helpers; using PlexRequests.UI.Models; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; @@ -46,10 +46,11 @@ namespace PlexRequests.UI.Modules.Admin { public class SystemStatusModule : BaseModule { - public SystemStatusModule(ISettingsService settingsService, ICacheProvider cache, ISettingsService ss, ISecurityExtensions security) : base("admin", settingsService, security) + public SystemStatusModule(ISettingsService settingsService, ICacheProvider cache, ISettingsService ss, ISecurityExtensions security, IAnalytics a) : base("admin", settingsService, security) { Cache = cache; SystemSettings = ss; + Analytics = a; Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); @@ -61,6 +62,7 @@ namespace PlexRequests.UI.Modules.Admin private ICacheProvider Cache { get; } private ISettingsService SystemSettings { get; } + private IAnalytics Analytics { get; } private async Task Status() { @@ -99,8 +101,10 @@ namespace PlexRequests.UI.Modules.Admin private async Task Save() { + var settings = this.Bind(); + Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, $"Updated Branch {EnumHelper.GetDisplayValue(settings.Branch)}", Username, CookieHelper.GetAnalyticClientId(Cookies)); await SystemSettings.SaveSettingsAsync(settings); // Clear the cache @@ -111,6 +115,8 @@ namespace PlexRequests.UI.Modules.Admin private Response AutoUpdate() { + Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, "AutoUpdate", Username, CookieHelper.GetAnalyticClientId(Cookies)); + var url = Request.Form["url"]; var startInfo = Type.GetType("Mono.Runtime") != null diff --git a/PlexRequests.UI/Modules/UserManagementModule.cs b/PlexRequests.UI/Modules/UserManagementModule.cs index b870536b0..4fc7b8d9d 100644 --- a/PlexRequests.UI/Modules/UserManagementModule.cs +++ b/PlexRequests.UI/Modules/UserManagementModule.cs @@ -13,12 +13,13 @@ using PlexRequests.Core; using PlexRequests.Core.Models; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; +using PlexRequests.Helpers.Analytics; using PlexRequests.Helpers.Permissions; using PlexRequests.Store; using PlexRequests.Store.Models; using PlexRequests.Store.Repository; using PlexRequests.UI.Models; - +using Action = PlexRequests.Helpers.Analytics.Action; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; namespace PlexRequests.UI.Modules @@ -26,7 +27,7 @@ namespace PlexRequests.UI.Modules public class UserManagementModule : BaseModule { public UserManagementModule(ISettingsService pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService plex, IRepository userLogins, IPlexUserRepository plexRepo - , ISecurityExtensions security, IRequestService req) : base("usermanagement", pr, security) + , ISecurityExtensions security, IRequestService req, IAnalytics ana) : base("usermanagement", pr, security) { #if !DEBUG Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); @@ -38,11 +39,12 @@ namespace PlexRequests.UI.Modules PlexUsersRepository = plexRepo; PlexRequestSettings = pr; RequestService = req; + Analytics = ana; Get["/"] = x => Load(); Get["/users", true] = async (x, ct) => await LoadUsers(); - Post["/createuser"] = x => CreateUser(); + Post["/createuser", true] = async (x,ct) => await CreateUser(); Get["/local/{id}"] = x => LocalDetails((Guid)x.id); Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id); Get["/permissions"] = x => GetEnum(); @@ -58,6 +60,7 @@ namespace PlexRequests.UI.Modules private IPlexUserRepository PlexUsersRepository { get; } private ISettingsService PlexRequestSettings { get; } private IRequestService RequestService { get; } + private IAnalytics Analytics { get; } private Negotiator Load() { @@ -103,8 +106,9 @@ namespace PlexRequests.UI.Modules return Response.AsJson(model); } - private Response CreateUser() + private async Task CreateUser() { + Analytics.TrackEventAsync(Category.UserManagement, Action.Create, "Created User", Username, CookieHelper.GetAnalyticClientId(Cookies)); var body = Request.Body.AsString(); if (string.IsNullOrEmpty(body)) { @@ -122,7 +126,7 @@ namespace PlexRequests.UI.Modules }); } - var users = UserMapper.GetUsers(); + var users = await UserMapper.GetUsersAsync(); if (users.Any(x => x.UserName.Equals(model.Username, StringComparison.CurrentCultureIgnoreCase))) { return Response.AsJson(new JsonResponseModel @@ -158,6 +162,7 @@ namespace PlexRequests.UI.Modules private async Task UpdateUser() { + Analytics.TrackEventAsync(Category.UserManagement, Action.Update, "Updated User", Username, CookieHelper.GetAnalyticClientId(Cookies)); var body = Request.Body.AsString(); if (string.IsNullOrEmpty(body)) { @@ -276,6 +281,7 @@ namespace PlexRequests.UI.Modules private Response DeleteUser() { + Analytics.TrackEventAsync(Category.UserManagement, Action.Delete, "Deleted User", Username, CookieHelper.GetAnalyticClientId(Cookies)); var body = Request.Body.AsString(); if (string.IsNullOrEmpty(body)) {