Skip to content

Commit

Permalink
Abstract App Insights JS SDK
Browse files Browse the repository at this point in the history
* Making it a shared partial keeps the _layout.cshtml cleaner

* Adds missing configuration for click tracking and user identification

* Extract App Insights connection string into Options
  • Loading branch information
DrizzlyOwl committed Nov 28, 2024
1 parent 5372d77 commit 7efdfe8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Dfe.PrepareConversions.Configuration;

public class ApplicationInsightsOptions
{
public const string ConfigurationSection = "ApplicationInsights";
public string ConnectionString { get; set; }
public string InstrumentationKey { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@{
var authenticatedUserId = (User.Identity is not null && User.Identity.IsAuthenticated) ? User.Identity.Name ?? "Unknown" : "Anonymous";
}
<script type="text/javascript" integrity="sha384-g/ZkzetdQypWdY0NBZT5r2L3BR9/hURD8OBcd1rEaBpgX6QC7EaTL+o+mzWrBcXW" crossorigin="anonymous" src="https://js.monitor.azure.com/scripts/b/ext/ai.clck.2.8.18.min.js"></script>
<script type="text/javascript" asp-add-nonce>
window.appInsights = {
connectionString: '@ApplicationInsightsOptions.Value.ConnectionString',
instrumentationKey: '@ApplicationInsightsOptions.Value.InstrumentationKey',
authenticatedUserId: '@authenticatedUserId'
}
</script>
<script type="text/javascript" src="~/dist/application-insights.min.js" asp-add-nonce></script>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
var isTransfersPage = Context.Request.Path.Value.ToLower().Contains("/transfers");

var notificationBannerMessage = Configuration["notificationBannerMessage"] ?? string.Empty;

var authenticatedUserId = User.Identity is not null && User.Identity.IsAuthenticated ? User.Identity.Name ?? "Unknown" : "Anonymous";
}

<!DOCTYPE html>
Expand Down Expand Up @@ -64,16 +62,7 @@

@if (enableAppInsightsAnalytics)
{
<!-- Application insights -->
<script type="text/javascript" integrity="sha384-g/ZkzetdQypWdY0NBZT5r2L3BR9/hURD8OBcd1rEaBpgX6QC7EaTL+o+mzWrBcXW" crossorigin="anonymous" src="https://js.monitor.azure.com/scripts/b/ext/ai.clck.2.8.18.min.js"></script>
<script type="text/javascript" asp-add-nonce>
window.appInsights = {
connectionString: '@Configuration["ApplicationInsights:ConnectionString"]',
authenticatedUserId: '@authenticatedUserId'
}
</script>
<script type="text/javascript" src="~/dist/application-insights.min.js" asp-add-nonce></script>
<!-- End Application insights -->
<partial name="_AppInsights"/>
}

<script type="text/javascript" src="~/dist/accessible-autocomplete.min.js" asp-add-nonce></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
@using Dfe.PrepareConversions.Models
@using Dfe.PrepareConversions.ViewModels
@using Dfe.PrepareConversions.Extensions
@using Dfe.PrepareConversions.Configuration
@using Microsoft.ApplicationInsights.AspNetCore
@using Microsoft.Extensions.Options
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
@addTagHelper *, Dfe.PrepareConversions
@addTagHelper *, NetEscapades.AspNetCore.SecurityHeaders.TagHelpers
@addTagHelper *, NetEscapades.AspNetCore.SecurityHeaders.TagHelpers
@inject IOptions<ApplicationInsightsOptions> ApplicationInsightsOptions
3 changes: 2 additions & 1 deletion Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ public void ConfigureServices(IServiceCollection services)

services.Configure<ServiceLinkOptions>(GetConfigurationSectionFor<ServiceLinkOptions>());
services.Configure<AzureAdOptions>(GetConfigurationSectionFor<AzureAdOptions>());
services.Configure<ApplicationInsightsOptions>(GetConfigurationSectionFor<ApplicationInsightsOptions>());

services.AddScoped<ErrorService>();
services.AddScoped<IGetEstablishment, EstablishmentService>();
services.Decorate<IGetEstablishment, GetEstablishmentItemCacheDecorator>();
services.AddScoped<SchoolPerformanceService>();
services.AddScoped<SchoolOverviewService>();
services.AddScoped<KeyStagePerformanceService>();
services.AddScoped<ITrustsRepository, TrustsRepository>();
services.AddScoped<ITrustsRepository, TrustsRepository>();
services.AddScoped<IProjectGroupsRepository, ProjectGroupsRepository>();
services.AddScoped<IRoleCapablitiesRepository, RoleCapablitiesRepository>();
services.AddScoped<IAcademyConversionProjectRepository, AcademyConversionProjectRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ApplicationInsights } from '@microsoft/applicationinsights-web'
const clickPluginInstance = new Microsoft.ApplicationInsights.ClickAnalyticsPlugin();
const appInsights = new ApplicationInsights({ config: {
connectionString: window.appInsights.connectionString,
instrumentationKey: window.appInsights.instrumentationKey,
extensions: [ clickPluginInstance ],
extensionConfig: { [clickPluginInstance.identifier] : {
autoCapture : true,
Expand Down

0 comments on commit 7efdfe8

Please sign in to comment.