Skip to content

Commit

Permalink
feat: Add Google Analytics Attribute data-track-label with page url
Browse files Browse the repository at this point in the history
Merge pull request #342 from DFE-Digital/AddAnalyticsAttr
  • Loading branch information
MartinBelton-gov authored Nov 30, 2023
2 parents 3d7426a + a92598b commit 9e023ba
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
using Childrens_Social_Care_CPD.Contentful.Renderers;
using FluentAssertions;
using Microsoft.Extensions.WebEncoders.Testing;
using NSubstitute;
using NUnit.Framework;
using System.IO;

namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers;

public class ContentLinkRendererTests
{
private readonly IRendererWithOptions<ContentLink> _sut = new ContentLinkRenderer();
private readonly IRendererWithOptions<ContentLink> _sut;

public ContentLinkRendererTests()
{
var mockContentLinkContext = Substitute.For<IContentLinkContext>();
_sut = new ContentLinkRenderer(mockContentLinkContext);
}

[TestCase("http://foo", "http://foo")]
[TestCase("https://foo", "https://foo")]
Expand All @@ -31,7 +38,8 @@ public void ContentLink_Renders(string uri, string expectedUri)
var actual = stringWriter.ToString();

// assert
actual.Should().Be($"<a class=\"HtmlEncode[[govuk-link]]\" href=\"HtmlEncode[[{expectedUri}]]\">HtmlEncode[[Foo]]</a>");
actual.Should().Be($"<a class=\"HtmlEncode[[govuk-link]]\" data-track-label=\"\" href=\"HtmlEncode[[{expectedUri}]]\">HtmlEncode[[Foo]]</a>");

}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Contentful.Core.Models;
using FluentAssertions;
using Microsoft.Extensions.WebEncoders.Testing;
using NSubstitute;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
Expand All @@ -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()
{
var mockContentLinkContext = Substitute.For<IContentLinkContext>();
_sut = new HyperlinkRenderer(mockContentLinkContext);
}

[Test]
public void HyperlinkToHtml_Returns_Anchor()
Expand All @@ -34,7 +41,7 @@ public void HyperlinkToHtml_Returns_Anchor()
var actual = stringWriter.ToString();

// assert
actual.Should().Be("<a class=\"HtmlEncode[[govuk-link]]\" href=\"HtmlEncode[[Bar]]\">HtmlEncode[[Foo]]</a>");
actual.Should().Be("<a class=\"HtmlEncode[[govuk-link]]\" data-track-label=\"\" href=\"HtmlEncode[[Bar]]\">HtmlEncode[[Foo]]</a>");
}

[Test]
Expand All @@ -57,6 +64,6 @@ public void HyperlinkToHtml_Returns_Anchor_With_Empty_Text()
var actual = stringWriter.ToString();

// assert
actual.Should().Be("<a class=\"HtmlEncode[[govuk-link]]\" href=\"HtmlEncode[[Bar]]\"></a>");
actual.Should().Be("<a class=\"HtmlEncode[[govuk-link]]\" data-track-label=\"\" href=\"HtmlEncode[[Bar]]\"></a>");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

namespace Childrens_Social_Care_CPD.Contentful.Renderers;

interface IContentLinkContext
{
string Path { get; }
}

internal class ContentLinkRenderer : IRendererWithOptions<ContentLink>
{
private readonly IContentLinkContext _context;
public ContentLinkRenderer(IContentLinkContext contentLinkContext)
{
_context = contentLinkContext;
}
public IHtmlContent Render(ContentLink item, RendererOptions options = null)
{
var tagBuilder = new TagBuilder("a");
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ namespace Childrens_Social_Care_CPD.Contentful.Renderers;

internal class HyperlinkRenderer : IRenderer<Hyperlink>
{
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;
}
Expand Down
3 changes: 3 additions & 0 deletions Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,6 +53,8 @@ public static void AddDependencies(this WebApplicationBuilder builder)
return client;
});

builder.Services.AddScoped<IContentLinkContext, ContentLinkContext>();

// Register all the IRender<T> & IRenderWithOptions<T> implementations in the assembly
var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();

Expand Down

0 comments on commit 9e023ba

Please sign in to comment.