From a21b5359378ff5e06f60190740ffe33a1583d97e Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Sat, 27 Sep 2014 12:39:29 -0700 Subject: [PATCH 1/4] New: added self hosted analytics to help improve nzbdrone. Can be turned off in Setting>General --- src/NzbDrone.Api/Config/HostConfigResource.cs | 3 ++- .../Frontend/Mappers/IndexHtmlMapper.cs | 3 +++ .../Configuration/ConfigFileProvider.cs | 9 +++++++ .../Settings/General/GeneralViewTemplate.hbs | 26 ++++++++++++++++++- src/UI/Shared/NzbDroneController.js | 13 ++++++++++ src/UI/index.html | 4 ++- src/UI/piwik.js | 14 ++++++++++ 7 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/UI/piwik.js diff --git a/src/NzbDrone.Api/Config/HostConfigResource.cs b/src/NzbDrone.Api/Config/HostConfigResource.cs index cc52c01c4..f8dde71a7 100644 --- a/src/NzbDrone.Api/Config/HostConfigResource.cs +++ b/src/NzbDrone.Api/Config/HostConfigResource.cs @@ -10,7 +10,8 @@ namespace NzbDrone.Api.Config public Int32 SslPort { get; set; } public Boolean EnableSsl { get; set; } public Boolean LaunchBrowser { get; set; } - public Boolean AuthenticationEnabled { get; set; } + public bool AuthenticationEnabled { get; set; } + public Boolean AnalyticsEnabled { get; set; } public String Username { get; set; } public String Password { get; set; } public String LogLevel { get; set; } diff --git a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs index e59fd0e44..3b2b30cb9 100644 --- a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs +++ b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs @@ -13,6 +13,7 @@ namespace NzbDrone.Api.Frontend.Mappers public class IndexHtmlMapper : StaticResourceMapperBase { private readonly IDiskProvider _diskProvider; + private readonly IConfigFileProvider _configFileProvider; private readonly Func _cacheBreakProviderFactory; private readonly string _indexPath; private static readonly Regex ReplaceRegex = new Regex("(?<=(?:href|src|data-main)=\").*?(?=\")", RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -30,6 +31,7 @@ namespace NzbDrone.Api.Frontend.Mappers : base(diskProvider, logger) { _diskProvider = diskProvider; + _configFileProvider = configFileProvider; _cacheBreakProviderFactory = cacheBreakProviderFactory; _indexPath = Path.Combine(appFolderInfo.StartUpFolder, "UI", "index.html"); @@ -87,6 +89,7 @@ namespace NzbDrone.Api.Frontend.Mappers text = text.Replace("API_ROOT", URL_BASE + "/api"); text = text.Replace("API_KEY", API_KEY); text = text.Replace("APP_VERSION", BuildInfo.Version.ToString()); + text = text.Replace("APP_ANALYTICS", _configFileProvider.AnalyticsEnabled.ToString().ToLowerInvariant()); _generatedContent = text; diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index ecb23d0a2..1240529f8 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -27,6 +27,7 @@ namespace NzbDrone.Core.Configuration bool EnableSsl { get; } bool LaunchBrowser { get; } bool AuthenticationEnabled { get; } + bool AnalyticsEnabled { get; } string Username { get; } string Password { get; } string LogLevel { get; } @@ -139,6 +140,14 @@ namespace NzbDrone.Core.Configuration get { return GetValueBoolean("AuthenticationEnabled", false); } } + public bool AnalyticsEnabled + { + get + { + return GetValueBoolean("AnalyticsEnabled", true, persist: false); + } + } + public string Branch { get { return GetValue("Branch", "master").ToLowerInvariant(); } diff --git a/src/UI/Settings/General/GeneralViewTemplate.hbs b/src/UI/Settings/General/GeneralViewTemplate.hbs index 60e42c462..69c452aac 100644 --- a/src/UI/Settings/General/GeneralViewTemplate.hbs +++ b/src/UI/Settings/General/GeneralViewTemplate.hbs @@ -173,9 +173,33 @@ +
+ Analytics + +
+ + +
+
+
+
+
- Updating + Updates
diff --git a/src/UI/Shared/NzbDroneController.js b/src/UI/Shared/NzbDroneController.js index c08e14bea..f0d52a9ba 100644 --- a/src/UI/Shared/NzbDroneController.js +++ b/src/UI/Shared/NzbDroneController.js @@ -25,6 +25,19 @@ define( else { document.title = title + ' - nzbdrone'; } + + if(window.Piwik){ + try { + var piwik = window.Piwik.getTracker('http://piwik.nzbdrone.com/piwik.php', 1); + piwik.setReferrerUrl(''); + piwik.setCustomUrl('http://local' + window.location.pathname); + piwik.setCustomVariable(1, 'version', window.NzbDrone.version, 'page'); + piwik.trackPageView(title); + } + catch (e){ + console.error(e); + } + } }, _onServerUpdated: function () { diff --git a/src/UI/index.html b/src/UI/index.html index 9477f7adf..3f2f5a199 100644 --- a/src/UI/index.html +++ b/src/UI/index.html @@ -75,10 +75,12 @@ window.NzbDrone = { ApiRoot: 'API_ROOT', ApiKey : 'API_KEY', - Version: 'APP_VERSION' + Version: 'APP_VERSION', + Analytics: APP_ANALYTICS }; + diff --git a/src/UI/piwik.js b/src/UI/piwik.js new file mode 100644 index 000000000..f56b4af31 --- /dev/null +++ b/src/UI/piwik.js @@ -0,0 +1,14 @@ +'use strict'; + +(function () { +if(window.NzbDrone.Analytics) { + var d = document; + var g = d.createElement('script'); + var s = d.getElementsByTagName('script')[0]; + g.type = 'text/javascript'; + g.async = true; + g.defer = true; + g.src = 'http://piwik.nzbdrone.com/piwik.js'; + s.parentNode.insertBefore(g, s); +} +})(); \ No newline at end of file From f716903a304fb4f99e41a4b98f9da82654b7fc79 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Sat, 27 Sep 2014 12:46:23 -0700 Subject: [PATCH 2/4] minor cleanup --- src/UI/Shared/NzbDroneController.js | 2 +- src/UI/piwik.js | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/UI/Shared/NzbDroneController.js b/src/UI/Shared/NzbDroneController.js index f0d52a9ba..c4584e3c7 100644 --- a/src/UI/Shared/NzbDroneController.js +++ b/src/UI/Shared/NzbDroneController.js @@ -26,7 +26,7 @@ define( document.title = title + ' - nzbdrone'; } - if(window.Piwik){ + if(window.NzbDrone.Analytics && window.Piwik){ try { var piwik = window.Piwik.getTracker('http://piwik.nzbdrone.com/piwik.php', 1); piwik.setReferrerUrl(''); diff --git a/src/UI/piwik.js b/src/UI/piwik.js index f56b4af31..782ef13c4 100644 --- a/src/UI/piwik.js +++ b/src/UI/piwik.js @@ -1,6 +1,4 @@ 'use strict'; - -(function () { if(window.NzbDrone.Analytics) { var d = document; var g = d.createElement('script'); @@ -10,5 +8,4 @@ if(window.NzbDrone.Analytics) { g.defer = true; g.src = 'http://piwik.nzbdrone.com/piwik.js'; s.parentNode.insertBefore(g, s); -} -})(); \ No newline at end of file +} \ No newline at end of file From c4b210c05362ee4152c6810495f691175ec16576 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Sat, 27 Sep 2014 13:38:31 -0700 Subject: [PATCH 3/4] disable analytics in dev --- .../Frontend/Mappers/IndexHtmlMapper.cs | 7 ++++- .../Analytics/AnalyticsService.cs | 28 +++++++++++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core/Analytics/AnalyticsService.cs diff --git a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs index 3b2b30cb9..a8172845a 100644 --- a/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs +++ b/src/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs @@ -6,6 +6,7 @@ using Nancy; using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Analytics; using NzbDrone.Core.Configuration; namespace NzbDrone.Api.Frontend.Mappers @@ -14,6 +15,7 @@ namespace NzbDrone.Api.Frontend.Mappers { private readonly IDiskProvider _diskProvider; private readonly IConfigFileProvider _configFileProvider; + private readonly IAnalyticsService _analyticsService; private readonly Func _cacheBreakProviderFactory; private readonly string _indexPath; private static readonly Regex ReplaceRegex = new Regex("(?<=(?:href|src|data-main)=\").*?(?=\")", RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -26,12 +28,14 @@ namespace NzbDrone.Api.Frontend.Mappers public IndexHtmlMapper(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider, IConfigFileProvider configFileProvider, + IAnalyticsService analyticsService, Func cacheBreakProviderFactory, Logger logger) : base(diskProvider, logger) { _diskProvider = diskProvider; _configFileProvider = configFileProvider; + _analyticsService = analyticsService; _cacheBreakProviderFactory = cacheBreakProviderFactory; _indexPath = Path.Combine(appFolderInfo.StartUpFolder, "UI", "index.html"); @@ -89,7 +93,8 @@ namespace NzbDrone.Api.Frontend.Mappers text = text.Replace("API_ROOT", URL_BASE + "/api"); text = text.Replace("API_KEY", API_KEY); text = text.Replace("APP_VERSION", BuildInfo.Version.ToString()); - text = text.Replace("APP_ANALYTICS", _configFileProvider.AnalyticsEnabled.ToString().ToLowerInvariant()); + + text = text.Replace("APP_ANALYTICS", _analyticsService.IsEnabled.ToString().ToLowerInvariant()); _generatedContent = text; diff --git a/src/NzbDrone.Core/Analytics/AnalyticsService.cs b/src/NzbDrone.Core/Analytics/AnalyticsService.cs new file mode 100644 index 000000000..8901817a9 --- /dev/null +++ b/src/NzbDrone.Core/Analytics/AnalyticsService.cs @@ -0,0 +1,28 @@ +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Configuration; + +namespace NzbDrone.Core.Analytics +{ + public interface IAnalyticsService + { + bool IsEnabled { get; } + } + + public class AnalyticsService : IAnalyticsService + { + private readonly IConfigFileProvider _configFileProvider; + + public AnalyticsService(IConfigFileProvider configFileProvider) + { + _configFileProvider = configFileProvider; + } + + public bool IsEnabled + { + get + { + return _configFileProvider.AnalyticsEnabled && RuntimeInfoBase.IsProduction; + } + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 459a5b9f8..e6140d5a2 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -109,6 +109,7 @@ Properties\SharedAssemblyInfo.cs + From 9bae44003bce51490e833173b5584dffc05767cf Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Sat, 27 Sep 2014 18:36:15 -0700 Subject: [PATCH 4/4] fixed grammar --- src/UI/Settings/General/GeneralViewTemplate.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UI/Settings/General/GeneralViewTemplate.hbs b/src/UI/Settings/General/GeneralViewTemplate.hbs index 69c452aac..2028508bd 100644 --- a/src/UI/Settings/General/GeneralViewTemplate.hbs +++ b/src/UI/Settings/General/GeneralViewTemplate.hbs @@ -191,7 +191,7 @@ - +