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

Aad 688 google analytics - MS Clarity #99

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics;
using SFA.DAS.Http.Configuration;

namespace SFA.DAS.ApprenticeApp.Pwa.Configuration;
Expand All @@ -19,6 +20,7 @@ public class ApplicationConfiguration
public string? StubAuth { get; set; }
public string? PushNotificationPublicKey { get; set; }
public bool UseGovSignIn { get; set; }
public GoogleAnalyticsConfiguration GoogleAnalytics { get; set; }
public string? WhiteListEmails { get; set; }
}

Expand Down
32 changes: 32 additions & 0 deletions src/SFA.DAS.ApprenticeApp.Pwa/Helpers/SecurityHeadersMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System.Threading.Tasks;

namespace SFA.DAS.ApprenticeApp.Pwa.Helpers
{
public class SecurityHeadersMiddleware
{
private readonly RequestDelegate _next;

public SecurityHeadersMiddleware(RequestDelegate next) => _next = next;

public async Task InvokeAsync(HttpContext context)
{
const string dasCdn = "das-at-frnt-end.azureedge.net das-pp-frnt-end.azureedge.net das-mo-frnt-end.azureedge.net das-test-frnt-end.azureedge.net das-test2-frnt-end.azureedge.net das-prd-frnt-end.azureedge.net";

if (!context.Response.Headers.ContainsKey("Content-Security-Policy"))
{
context.Response.Headers.Add("Content-Security-Policy",
new StringValues(
$"script-src 'self' 'unsafe-inline' 'unsafe-eval' {dasCdn} https://www.googletagmanager.com https://tagmanager.google.com https://www.google-analytics.com https://ssl.google-analytics.com ; " +
$"style-src 'self' 'unsafe-inline' {dasCdn} https://tagmanager.google.com https://fonts.googleapis.com ; " +
$"img-src 'self' {dasCdn} www.googletagmanager.com https://ssl.gstatic.com https://www.gstatic.com https://www.google-analytics.com ; " +
$"font-src 'self' {dasCdn} https://fonts.gstatic.com https://localhost:* ; " +
$"connect-src 'self' ws://localhost:* wss://localhost:* http://localhost:* https://localhost:* {dasCdn} https://www.google-analytics.com ; " +
$"frame-src https://www.googletagmanager.com"));
}

await _next(context);
}
}
}
13 changes: 13 additions & 0 deletions src/SFA.DAS.ApprenticeApp.Pwa/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.ApplicationInsights;
using SFA.DAS.ApprenticeApp.Pwa.AppStart;
using SFA.DAS.ApprenticeApp.Pwa.Configuration;
using SFA.DAS.ApprenticeApp.Pwa.Helpers;
using SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Text;
using WebEssentials.AspNetCore.Pwa;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -40,6 +45,12 @@
// Add the OpenTelemetry telemetry service to the application.
builder.Services.AddOpenTelemetryRegistration(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

// configure google analytics
builder.Services.Configure<MvcOptions>(options =>
options.Filters.Add(new EnableGoogleAnalyticsAttribute(
applicationConfiguration.GoogleAnalytics)
));

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -50,6 +61,8 @@
app.UseHsts();
}

// Add Security Headers Middleware
app.UseMiddleware<SecurityHeadersMiddleware>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseHealthChecks("/ping");
Expand Down
30 changes: 30 additions & 0 deletions src/SFA.DAS.ApprenticeApp.Pwa/Views/Shared/_GoogleAnalytics.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@using SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics

@if (ViewData.GoogleAnalyticsIsEnabled())
{
var id = ViewData.GetGoogleTagManagerId();

@if (Model == SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics.GoogleAnalyticsTag.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', '@id');</script>
<!-- End Google Tag Manager -->
}
else if (Model == SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics.GoogleAnalyticsTag.Body)
{
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=@id"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
}
}
15 changes: 14 additions & 1 deletion src/SFA.DAS.ApprenticeApp.Pwa/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
<!DOCTYPE html>
@using SFA.DAS.ApprenticePortal.SharedUi.GoogleAnalytics

<!DOCTYPE html>
<html lang="en" class="govuk-template">

<head>
<partial name="_GoogleAnalytics" model="GoogleAnalyticsTag.Head" />
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
<meta name="theme-color" content="#f7f7f7" />
<meta name="clarity-site-verification" content="ec87324f-d5ed-4a81-a723-dd5179c531eb" />
<title>@(ViewData["Title"] == null ? "Apprentice App" : ViewData["Title"])</title>
<link rel="icon" sizes="48x48" href="/assets/images/favicon.ico" />
<link rel="apple-touch-icon" href="/assets/images/apple-touch-icon.png" />
<link rel="stylesheet" href="~/css/app.css" asp-append-version="true" />
@*MS Clarity tag*@
<script type="text/javascript">
(function (c, l, a, r, i, t, y) {
c[a] = c[a] || function () { (c[a].q = c[a].q || []).push(arguments) };
t = l.createElement(r); t.async = 1; t.src = "https://www.clarity.ms/tag/" + i;
y = l.getElementsByTagName(r)[0]; y.parentNode.insertBefore(t, y);
})(window, document, "clarity", "script", "h8wk6eosjr");
</script>
</head>

<body class="govuk-template__body app-template__body" id="top">
<partial name="_GoogleAnalytics" model="GoogleAnalyticsTag.Head" />
<script>document.body.className += ' js-enabled' + ('noModule' in HTMLScriptElement.prototype ? ' govuk-frontend-supported' : '');</script>
<div class="app-container">
<main class="app-main">
Expand Down
2 changes: 1 addition & 1 deletion src/SFA.DAS.ApprenticeApp.Pwa/wwwroot/css/app.css

Large diffs are not rendered by default.

Loading