diff --git a/Childrens-Social-Care-CPD-Tests/Childrens-Social-Care-CPD-Tests.csproj b/Childrens-Social-Care-CPD-Tests/Childrens-Social-Care-CPD-Tests.csproj
index e030c85d..c925cfad 100644
--- a/Childrens-Social-Care-CPD-Tests/Childrens-Social-Care-CPD-Tests.csproj
+++ b/Childrens-Social-Care-CPD-Tests/Childrens-Social-Care-CPD-Tests.csproj
@@ -12,6 +12,7 @@
+
diff --git a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ContentLinkRendererTests.cs b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ContentLinkRendererTests.cs
index 93223f53..58d73a73 100644
--- a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ContentLinkRendererTests.cs
+++ b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ContentLinkRendererTests.cs
@@ -2,6 +2,7 @@
using Childrens_Social_Care_CPD.Contentful.Renderers;
using FluentAssertions;
using Microsoft.Extensions.WebEncoders.Testing;
+using Moq;
using NUnit.Framework;
using System.IO;
@@ -9,7 +10,13 @@ namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers;
public class ContentLinkRendererTests
{
- private readonly IRendererWithOptions _sut = new ContentLinkRenderer();
+ private readonly IRendererWithOptions _sut;
+
+ public ContentLinkRendererTests()
+ {
+ Mock mockContentLinkContext = new Mock();
+ _sut = new ContentLinkRenderer(mockContentLinkContext.Object);
+ }
[TestCase("http://foo", "http://foo")]
[TestCase("https://foo", "https://foo")]
@@ -31,7 +38,8 @@ public void ContentLink_Renders(string uri, string expectedUri)
var actual = stringWriter.ToString();
// assert
- actual.Should().Be($"HtmlEncode[[Foo]]");
+ actual.Should().Be($"HtmlEncode[[Foo]]");
+
}
[Test]
diff --git a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/HyperlinkRendererTests.cs b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/HyperlinkRendererTests.cs
index bf11c650..bccb2293 100644
--- a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/HyperlinkRendererTests.cs
+++ b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/HyperlinkRendererTests.cs
@@ -2,6 +2,7 @@
using Contentful.Core.Models;
using FluentAssertions;
using Microsoft.Extensions.WebEncoders.Testing;
+using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
@@ -10,7 +11,13 @@ namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers;
public class HyperlinkRendererTests
{
- private readonly HyperlinkRenderer _sut = new();
+ private readonly HyperlinkRenderer _sut;
+
+ public HyperlinkRendererTests()
+ {
+ Mock mockContentLinkContext = new Mock();
+ _sut = new HyperlinkRenderer(mockContentLinkContext.Object);
+ }
[Test]
public void HyperlinkToHtml_Returns_Anchor()
@@ -34,7 +41,7 @@ public void HyperlinkToHtml_Returns_Anchor()
var actual = stringWriter.ToString();
// assert
- actual.Should().Be("HtmlEncode[[Foo]]");
+ actual.Should().Be("HtmlEncode[[Foo]]");
}
[Test]
@@ -57,6 +64,6 @@ public void HyperlinkToHtml_Returns_Anchor_With_Empty_Text()
var actual = stringWriter.ToString();
// assert
- actual.Should().Be("");
+ actual.Should().Be("");
}
}
diff --git a/Childrens-Social-Care-CPD/Contentful/Contexts/ContentLinkContext.cs b/Childrens-Social-Care-CPD/Contentful/Contexts/ContentLinkContext.cs
new file mode 100644
index 00000000..ec4d4a19
--- /dev/null
+++ b/Childrens-Social-Care-CPD/Contentful/Contexts/ContentLinkContext.cs
@@ -0,0 +1,14 @@
+using Childrens_Social_Care_CPD.Contentful.Renderers;
+
+namespace Childrens_Social_Care_CPD.Contentful.Contexts
+{
+ public class ContentLinkContext : IContentLinkContext
+ {
+ private readonly IHttpContextAccessor _httpContextAccessor;
+ public ContentLinkContext(IHttpContextAccessor httpContextAccessor)
+ {
+ _httpContextAccessor = httpContextAccessor;
+ }
+ public string Path => _httpContextAccessor.HttpContext?.Request.Path;
+ }
+}
diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/ContentLinkRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/ContentLinkRenderer.cs
index 19b548d2..1d2eb344 100644
--- a/Childrens-Social-Care-CPD/Contentful/Renderers/ContentLinkRenderer.cs
+++ b/Childrens-Social-Care-CPD/Contentful/Renderers/ContentLinkRenderer.cs
@@ -4,8 +4,18 @@
namespace Childrens_Social_Care_CPD.Contentful.Renderers;
+interface IContentLinkContext
+{
+ string Path { get; }
+}
+
internal class ContentLinkRenderer : IRendererWithOptions
{
+ private readonly IContentLinkContext _context;
+ public ContentLinkRenderer(IContentLinkContext contentLinkContext)
+ {
+ _context = contentLinkContext;
+ }
public IHtmlContent Render(ContentLink item, RendererOptions options = null)
{
var tagBuilder = new TagBuilder("a");
@@ -23,6 +33,7 @@ string s when s.StartsWith('/') => s,
{
tagBuilder.AddCssClass(options.Css);
}
+ tagBuilder.Attributes.Add("data-track-label", _context.Path);
tagBuilder.InnerHtml.Append(item.Name);
return tagBuilder;
}
diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/HyperlinkRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/HyperlinkRenderer.cs
index 9e535aff..b57c694b 100644
--- a/Childrens-Social-Care-CPD/Contentful/Renderers/HyperlinkRenderer.cs
+++ b/Childrens-Social-Care-CPD/Contentful/Renderers/HyperlinkRenderer.cs
@@ -6,12 +6,18 @@ namespace Childrens_Social_Care_CPD.Contentful.Renderers;
internal class HyperlinkRenderer : IRenderer
{
+ private readonly IContentLinkContext _context;
+ public HyperlinkRenderer(IContentLinkContext contentLinkContext)
+ {
+ _context = contentLinkContext;
+ }
public IHtmlContent Render(Hyperlink item)
{
var linkText = (item.Content.FirstOrDefault() as Text)?.Value;
var anchor = new TagBuilder("a");
anchor.AddCssClass("govuk-link");
anchor.Attributes.Add("href", item.Data.Uri);
+ anchor.Attributes.Add("data-track-label", _context.Path);
anchor.InnerHtml.SetContent(linkText);
return anchor;
}
diff --git a/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs b/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
index 9d5ffb40..3dbccb4a 100644
--- a/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
+++ b/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
@@ -1,5 +1,6 @@
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Contentful;
+using Childrens_Social_Care_CPD.Contentful.Contexts;
using Childrens_Social_Care_CPD.Contentful.Renderers;
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.DataAccess;
@@ -52,6 +53,8 @@ public static void AddDependencies(this WebApplicationBuilder builder)
return client;
});
+ builder.Services.AddScoped();
+
// Register all the IRender & IRenderWithOptions implementations in the assembly
var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();