From d1d705dd856e0c2bd9553dbec9b73a627275cbdd Mon Sep 17 00:00:00 2001 From: James Griffiths Date: Tue, 12 Nov 2024 18:01:49 +0000 Subject: [PATCH] EHD-1432: Reduce use of interfaces: IWebTracker --- .../TestHelpers/UiTestHelpers.cs | 4 ++-- .../GenderPayGap.WebUI.Tests/appsettings.json | 3 ++- .../Classes/GoogleAnalyticsTracker.cs | 24 ++++++++----------- .../Controllers/DownloadController.cs | 4 ++-- .../Controllers/RedirectController.cs | 4 ++-- GenderPayGap.WebUI/Startup.cs | 6 ++--- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/TestHelpers/UiTestHelpers.cs b/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/TestHelpers/UiTestHelpers.cs index f4dc53b0b..ea30ce929 100644 --- a/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/TestHelpers/UiTestHelpers.cs +++ b/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/TestHelpers/UiTestHelpers.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -214,7 +214,7 @@ public static IContainer BuildContainerIoC(params object[] dbObjects) //Register WebTracker - builder.Register(c => Mock.Of()).As().InstancePerLifetimeScope(); + builder.RegisterType().As().InstancePerLifetimeScope(); //Register all controllers - this is required to ensure KeyFilter is resolved in constructors builder.RegisterType().InstancePerLifetimeScope(); diff --git a/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/appsettings.json b/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/appsettings.json index 8d87a3fcd..1a27d82f5 100644 --- a/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/appsettings.json +++ b/GenderPayGap.UnitTests/GenderPayGap.WebUI.Tests/appsettings.json @@ -1,4 +1,5 @@ { "DefaultEncryptionKey": "", // some unit tests require this key to be empty - "OffsetCurrentDateTimeForSite": "917" + "OffsetCurrentDateTimeForSite": "917", + "GoogleAnalyticsAccountId": null } \ No newline at end of file diff --git a/GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs b/GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs index 6754b4c96..124b83c03 100644 --- a/GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs +++ b/GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; @@ -10,17 +11,11 @@ namespace GenderPayGap.WebUI.Classes { - public interface IWebTracker - { - void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null); - } - - /// /// Uses open HttpClient see /// https://www.codeproject.com/Articles/1194406/Using-HttpClient-as-it-was-intended-because-you-re /// - public class GoogleAnalyticsTracker : IWebTracker, IDisposable + public class GoogleAnalyticsTracker : IDisposable { public static Uri BaseUri = new Uri("https://www.google-analytics.com"); @@ -28,14 +23,12 @@ public class GoogleAnalyticsTracker : IWebTracker, IDisposable private readonly HttpClient _httpClient; - private readonly IHttpContextAccessor _httpContextAccessor; private readonly string googleTrackingId; // UA-XXXXXXXXX-XX private readonly string googleVersion = "1"; - public GoogleAnalyticsTracker(IHttpContextAccessor httpContextAccessor, HttpClient httpClient, string trackingId) + public GoogleAnalyticsTracker(HttpClient httpClient, string trackingId) { - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); _httpClient = httpClient; googleTrackingId = trackingId; } @@ -46,8 +39,7 @@ public void Dispose() { _httpClient?.Dispose(); } - - + public void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null) { if (string.IsNullOrWhiteSpace(pageTitle)) @@ -71,10 +63,14 @@ public void TrackPageView(Controller controller, string pageTitle = null, string SendPageViewTracking(pageTitle, pageUrl); } - - + private async void SendPageViewTracking(string title, string url) { + if (googleTrackingId == null) + { + return; + } + if (string.IsNullOrWhiteSpace(title)) { throw new ArgumentNullException(nameof(title)); diff --git a/GenderPayGap.WebUI/Controllers/DownloadController.cs b/GenderPayGap.WebUI/Controllers/DownloadController.cs index fde0ed690..6fed75838 100644 --- a/GenderPayGap.WebUI/Controllers/DownloadController.cs +++ b/GenderPayGap.WebUI/Controllers/DownloadController.cs @@ -16,11 +16,11 @@ public class DownloadController : Controller { private readonly IFileRepository fileRepository; - private readonly IWebTracker webTracker; + private readonly GoogleAnalyticsTracker webTracker; public DownloadController( IFileRepository fileRepository, - IWebTracker webTracker) + GoogleAnalyticsTracker webTracker) { this.fileRepository = fileRepository; this.webTracker = webTracker; diff --git a/GenderPayGap.WebUI/Controllers/RedirectController.cs b/GenderPayGap.WebUI/Controllers/RedirectController.cs index 6fe8fe465..8c9516e45 100644 --- a/GenderPayGap.WebUI/Controllers/RedirectController.cs +++ b/GenderPayGap.WebUI/Controllers/RedirectController.cs @@ -17,10 +17,10 @@ public class RedirectController : Controller // This class contains redirects for outdated URLs // Some of these URLs might appear in emails / printed letters, so we want to redirect to an appropriate page, rather than showing a 404 - private readonly IWebTracker webTracker; + private readonly GoogleAnalyticsTracker webTracker; private readonly IDataRepository dataRepository; - public RedirectController(IWebTracker webTracker, IDataRepository dataRepository) + public RedirectController(GoogleAnalyticsTracker webTracker, IDataRepository dataRepository) { this.webTracker = webTracker; this.dataRepository = dataRepository; diff --git a/GenderPayGap.WebUI/Startup.cs b/GenderPayGap.WebUI/Startup.cs index 48f43a474..0baa8d3ea 100644 --- a/GenderPayGap.WebUI/Startup.cs +++ b/GenderPayGap.WebUI/Startup.cs @@ -63,7 +63,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services) }); //Add a dedicated httpclient for Google Analytics tracking with exponential retry policy - services.AddHttpClient(nameof(IWebTracker), GoogleAnalyticsTracker.SetupHttpClient) + services.AddHttpClient(nameof(GoogleAnalyticsTracker), GoogleAnalyticsTracker.SetupHttpClient) .SetHandlerLifetime(TimeSpan.FromMinutes(10)) .AddPolicyHandler(GoogleAnalyticsTracker.GetRetryPolicy()); @@ -228,11 +228,11 @@ public IContainer BuildContainerIoC(IServiceCollection services) //Register WebTracker builder.RegisterType() - .As() + .As() .SingleInstance() .WithParameter( (p, ctx) => p.ParameterType == typeof(HttpClient), - (p, ctx) => ctx.Resolve().CreateClient(nameof(IWebTracker))) + (p, ctx) => ctx.Resolve().CreateClient(nameof(GoogleAnalyticsTracker))) .WithParameter("trackingId", Global.GoogleAnalyticsAccountId); //TOD: Implement AutoFac modules