Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Google Analytics Attribute data-track-label with page url #342

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
MartinBelton-gov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.16" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
Expand Down
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 Moq;
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()
{
Mock<IContentLinkContext> mockContentLinkContext = new Mock<IContentLinkContext>();
_sut = new ContentLinkRenderer(mockContentLinkContext.Object);
}

[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 Moq;
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()
{
Mock<IContentLinkContext> mockContentLinkContext = new Mock<IContentLinkContext>();
_sut = new HyperlinkRenderer(mockContentLinkContext.Object);
}

[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
Loading