Small bit of analytics

This commit is contained in:
Jamie.Rees 2016-12-08 14:22:28 +00:00
parent 121cf90ba7
commit ad796a47bd
5 changed files with 27 additions and 11 deletions

View file

@ -54,7 +54,7 @@ namespace PlexRequests.Helpers.Analytics
Track(HitType.@event, username, cat, act, label, clientId, value); 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 cat = category.ToString();
var act = action.ToString(); var act = action.ToString();
@ -146,7 +146,7 @@ namespace PlexRequests.Helpers.Analytics
request.Method = RequestMethod; request.Method = RequestMethod;
// set the Content-Length header to the correct value // set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
#if !DEBUG
// write the request body to the request // write the request body to the request
using (var writer = new StreamWriter(request.GetRequestStream())) using (var writer = new StreamWriter(request.GetRequestStream()))
{ {
@ -165,6 +165,7 @@ namespace PlexRequests.Helpers.Analytics
{ {
Log.Error(ex, "Analytics tracking failed"); Log.Error(ex, "Analytics tracking failed");
} }
#endif
} }
private async Task SendRequestAsync(string postDataString) private async Task SendRequestAsync(string postDataString)
{ {
@ -173,6 +174,7 @@ namespace PlexRequests.Helpers.Analytics
// set the Content-Length header to the correct value // set the Content-Length header to the correct value
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString); request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
#if !DEBUG
// write the request body to the request // write the request body to the request
using (var writer = new StreamWriter(await request.GetRequestStreamAsync())) using (var writer = new StreamWriter(await request.GetRequestStreamAsync()))
{ {
@ -191,6 +193,7 @@ namespace PlexRequests.Helpers.Analytics
{ {
Log.Error(ex, "Analytics tracking failed"); Log.Error(ex, "Analytics tracking failed");
} }
#endif
} }
private Dictionary<string, string> BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal) private Dictionary<string, string> BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal)

View file

@ -38,6 +38,7 @@ namespace PlexRequests.Helpers.Analytics
Issues, Issues,
UserLogin, UserLogin,
Services, Services,
Navbar Navbar,
UserManagement
} }
} }

View file

@ -51,7 +51,7 @@ namespace PlexRequests.Helpers.Analytics
/// <param name="clientId">The client identifier.</param> /// <param name="clientId">The client identifier.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// Tracks the page view. /// Tracks the page view.

View file

@ -37,8 +37,8 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Core.StatusChecker; using PlexRequests.Core.StatusChecker;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Analytics;
using PlexRequests.Helpers.Permissions; using PlexRequests.Helpers.Permissions;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions;
@ -46,10 +46,11 @@ namespace PlexRequests.UI.Modules.Admin
{ {
public class SystemStatusModule : BaseModule public class SystemStatusModule : BaseModule
{ {
public SystemStatusModule(ISettingsService<PlexRequestSettings> settingsService, ICacheProvider cache, ISettingsService<SystemSettings> ss, ISecurityExtensions security) : base("admin", settingsService, security) public SystemStatusModule(ISettingsService<PlexRequestSettings> settingsService, ICacheProvider cache, ISettingsService<SystemSettings> ss, ISecurityExtensions security, IAnalytics a) : base("admin", settingsService, security)
{ {
Cache = cache; Cache = cache;
SystemSettings = ss; SystemSettings = ss;
Analytics = a;
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
@ -61,6 +62,7 @@ namespace PlexRequests.UI.Modules.Admin
private ICacheProvider Cache { get; } private ICacheProvider Cache { get; }
private ISettingsService<SystemSettings> SystemSettings { get; } private ISettingsService<SystemSettings> SystemSettings { get; }
private IAnalytics Analytics { get; }
private async Task<Negotiator> Status() private async Task<Negotiator> Status()
{ {
@ -99,8 +101,10 @@ namespace PlexRequests.UI.Modules.Admin
private async Task<Response> Save() private async Task<Response> Save()
{ {
var settings = this.Bind<SystemSettings>(); var settings = this.Bind<SystemSettings>();
Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, $"Updated Branch {EnumHelper<Branches>.GetDisplayValue(settings.Branch)}", Username, CookieHelper.GetAnalyticClientId(Cookies));
await SystemSettings.SaveSettingsAsync(settings); await SystemSettings.SaveSettingsAsync(settings);
// Clear the cache // Clear the cache
@ -111,6 +115,8 @@ namespace PlexRequests.UI.Modules.Admin
private Response AutoUpdate() private Response AutoUpdate()
{ {
Analytics.TrackEventAsync(Category.Admin, PlexRequests.Helpers.Analytics.Action.Update, "AutoUpdate", Username, CookieHelper.GetAnalyticClientId(Cookies));
var url = Request.Form["url"]; var url = Request.Form["url"];
var startInfo = Type.GetType("Mono.Runtime") != null var startInfo = Type.GetType("Mono.Runtime") != null

View file

@ -13,12 +13,13 @@ using PlexRequests.Core;
using PlexRequests.Core.Models; using PlexRequests.Core.Models;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Analytics;
using PlexRequests.Helpers.Permissions; using PlexRequests.Helpers.Permissions;
using PlexRequests.Store; using PlexRequests.Store;
using PlexRequests.Store.Models; using PlexRequests.Store.Models;
using PlexRequests.Store.Repository; using PlexRequests.Store.Repository;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
using Action = PlexRequests.Helpers.Analytics.Action;
using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions; using ISecurityExtensions = PlexRequests.Core.ISecurityExtensions;
namespace PlexRequests.UI.Modules namespace PlexRequests.UI.Modules
@ -26,7 +27,7 @@ namespace PlexRequests.UI.Modules
public class UserManagementModule : BaseModule public class UserManagementModule : BaseModule
{ {
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins, IPlexUserRepository plexRepo public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins, IPlexUserRepository plexRepo
, ISecurityExtensions security, IRequestService req) : base("usermanagement", pr, security) , ISecurityExtensions security, IRequestService req, IAnalytics ana) : base("usermanagement", pr, security)
{ {
#if !DEBUG #if !DEBUG
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx); Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
@ -38,11 +39,12 @@ namespace PlexRequests.UI.Modules
PlexUsersRepository = plexRepo; PlexUsersRepository = plexRepo;
PlexRequestSettings = pr; PlexRequestSettings = pr;
RequestService = req; RequestService = req;
Analytics = ana;
Get["/"] = x => Load(); Get["/"] = x => Load();
Get["/users", true] = async (x, ct) => await LoadUsers(); 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["/local/{id}"] = x => LocalDetails((Guid)x.id);
Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id); Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id);
Get["/permissions"] = x => GetEnum<Permissions>(); Get["/permissions"] = x => GetEnum<Permissions>();
@ -58,6 +60,7 @@ namespace PlexRequests.UI.Modules
private IPlexUserRepository PlexUsersRepository { get; } private IPlexUserRepository PlexUsersRepository { get; }
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; } private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
private IRequestService RequestService { get; } private IRequestService RequestService { get; }
private IAnalytics Analytics { get; }
private Negotiator Load() private Negotiator Load()
{ {
@ -103,8 +106,9 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(model); return Response.AsJson(model);
} }
private Response CreateUser() private async Task<Response> CreateUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Create, "Created User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) 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))) if (users.Any(x => x.UserName.Equals(model.Username, StringComparison.CurrentCultureIgnoreCase)))
{ {
return Response.AsJson(new JsonResponseModel return Response.AsJson(new JsonResponseModel
@ -158,6 +162,7 @@ namespace PlexRequests.UI.Modules
private async Task<Response> UpdateUser() private async Task<Response> UpdateUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Update, "Updated User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) if (string.IsNullOrEmpty(body))
{ {
@ -276,6 +281,7 @@ namespace PlexRequests.UI.Modules
private Response DeleteUser() private Response DeleteUser()
{ {
Analytics.TrackEventAsync(Category.UserManagement, Action.Delete, "Deleted User", Username, CookieHelper.GetAnalyticClientId(Cookies));
var body = Request.Body.AsString(); var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body)) if (string.IsNullOrEmpty(body))
{ {