Skip to content

Commit

Permalink
EHD-1432: Reduce use of interfaces: IWebTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgriff committed Nov 20, 2024
1 parent 928b952 commit d1d705d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -214,7 +214,7 @@ public static IContainer BuildContainerIoC(params object[] dbObjects)


//Register WebTracker
builder.Register(c => Mock.Of<IWebTracker>()).As<IWebTracker>().InstancePerLifetimeScope();
builder.RegisterType<GoogleAnalyticsTracker>().As<GoogleAnalyticsTracker>().InstancePerLifetimeScope();

//Register all controllers - this is required to ensure KeyFilter is resolved in constructors
builder.RegisterType<ErrorController>().InstancePerLifetimeScope();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"DefaultEncryptionKey": "", // some unit tests require this key to be empty
"OffsetCurrentDateTimeForSite": "917"
"OffsetCurrentDateTimeForSite": "917",
"GoogleAnalyticsAccountId": null
}
24 changes: 10 additions & 14 deletions GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,32 +11,24 @@

namespace GenderPayGap.WebUI.Classes
{
public interface IWebTracker
{
void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null);
}


/// <summary>
/// Uses open HttpClient see
/// https://www.codeproject.com/Articles/1194406/Using-HttpClient-as-it-was-intended-because-you-re
/// </summary>
public class GoogleAnalyticsTracker : IWebTracker, IDisposable
public class GoogleAnalyticsTracker : IDisposable
{

public static Uri BaseUri = new Uri("https://www.google-analytics.com");
private static readonly Uri endpointUri = new Uri(BaseUri, "/collect");

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;
}
Expand All @@ -46,8 +39,7 @@ public void Dispose()
{
_httpClient?.Dispose();
}



public void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null)
{
if (string.IsNullOrWhiteSpace(pageTitle))
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions GenderPayGap.WebUI/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions GenderPayGap.WebUI/Controllers/RedirectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions GenderPayGap.WebUI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
});

//Add a dedicated httpclient for Google Analytics tracking with exponential retry policy
services.AddHttpClient<IWebTracker, GoogleAnalyticsTracker>(nameof(IWebTracker), GoogleAnalyticsTracker.SetupHttpClient)
services.AddHttpClient<GoogleAnalyticsTracker, GoogleAnalyticsTracker>(nameof(GoogleAnalyticsTracker), GoogleAnalyticsTracker.SetupHttpClient)
.SetHandlerLifetime(TimeSpan.FromMinutes(10))
.AddPolicyHandler(GoogleAnalyticsTracker.GetRetryPolicy());

Expand Down Expand Up @@ -228,11 +228,11 @@ public IContainer BuildContainerIoC(IServiceCollection services)

//Register WebTracker
builder.RegisterType<GoogleAnalyticsTracker>()
.As<IWebTracker>()
.As<GoogleAnalyticsTracker>()
.SingleInstance()
.WithParameter(
(p, ctx) => p.ParameterType == typeof(HttpClient),
(p, ctx) => ctx.Resolve<IHttpClientFactory>().CreateClient(nameof(IWebTracker)))
(p, ctx) => ctx.Resolve<IHttpClientFactory>().CreateClient(nameof(GoogleAnalyticsTracker)))
.WithParameter("trackingId", Global.GoogleAnalyticsAccountId);

//TOD: Implement AutoFac modules
Expand Down

0 comments on commit d1d705d

Please sign in to comment.