Skip to content

Commit

Permalink
Merge pull request #108 from DFE-Digital/feature/tracking
Browse files Browse the repository at this point in the history
Add gtm and clarity tracking
  • Loading branch information
jack-coggin authored Jul 10, 2024
2 parents b9ea2ef + 4a3aa3a commit 57569ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Dfe.ContentSupport.Web/Configuration/TrackingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dfe.ContentSupport.Web.Configuration;

public class TrackingOptions
{
public string Gtm { get; set; }
public string Clarity { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using Dfe.ContentSupport.Web.Http;
using Dfe.ContentSupport.Web.Models.Mapped;
using Dfe.ContentSupport.Web.Services;
using Microsoft.Extensions.Options;

namespace Dfe.ContentSupport.Web.Extensions;

public static class WebApplicationBuilderExtensions
{
public static void InitDependencyInjection(this WebApplicationBuilder app)
{
var contentfulOptions = new CsContentfulOptions();
app.Configuration.GetSection("Contentful").Bind(contentfulOptions);
app.Services.AddSingleton(contentfulOptions);


app.Services.Configure<CsContentfulOptions>(app.Configuration.GetSection("Contentful"))
.AddSingleton(sp => sp.GetRequiredService<IOptions<CsContentfulOptions>>().Value);

app.Services.Configure<TrackingOptions>(app.Configuration.GetSection("tracking"))
.AddSingleton(sp => sp.GetRequiredService<IOptions<TrackingOptions>>().Value);

app.Services
.AddTransient<ICacheService<List<CsPage>>, CsPagesCacheService>();
Expand All @@ -30,4 +30,4 @@ public static void InitDependencyInjection(this WebApplicationBuilder app)
app.Services.AddTransient<IHttpContentfulClient, HttpContentfulClient>();
}
}
}
}
25 changes: 23 additions & 2 deletions src/Dfe.ContentSupport.Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
@model CsPage;
@using Dfe.ContentSupport.Web.Configuration
@using Microsoft.Extensions.Configuration
@inject TrackingOptions trackingOptions
@model CsPage;

@{
Layout = "_GovUkPageTemplate";
ViewData["Title"] = Model.Heading.Title;
ViewData["containerClasses"] = "dfe-width-container";
}

@section Head {
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', '@(trackingOptions.Gtm)');</script>
<!-- End Google Tag Manager -->
}

@section Header {
<link rel="stylesheet" as="style" href="~/css/application.css">
<link rel="stylesheet" as="style" href="~/css/site.css">
Expand All @@ -19,6 +35,11 @@
@section BeforeContent {
<script defer>document.body.className += ' js-enabled' + ('noModule' in HTMLScriptElement.prototype ? ' govuk-frontend-supported' : '');</script>

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src=@($"https://www.googletagmanager.com/ns.html?id={trackingOptions.Gtm}") height="0" width="0"
style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

@RenderSection("BeforeContent", false)
}

Expand All @@ -35,4 +56,4 @@
</script>

@RenderSection("BodyEnd", false)
}
}

0 comments on commit 57569ac

Please sign in to comment.