Skip to content

Commit

Permalink
Add client_id to analytics events (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Oct 11, 2023
1 parent a2ec2dd commit c21ccfb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Dfe.Analytics.AspNetCore;

namespace TeacherIdentity.AuthServer.Infrastructure.Middleware;

public class AppendAuthorizationInfoToAnalyticsEventsMiddleware
{
private readonly RequestDelegate _next;

public AppendAuthorizationInfoToAnalyticsEventsMiddleware(RequestDelegate next)
{
_next = next;
}

public Task Invoke(HttpContext context)
{
var analyticsEvent = context.GetWebRequestEvent();
var clientRedirectInfo = context.GetClientRedirectInfo();

if (context.TryGetAuthenticationState(out var authenticationState))
{
if (!string.IsNullOrEmpty(authenticationState.SessionId))
{
analyticsEvent.AddData("session_id", authenticationState.SessionId);
}

if (authenticationState.TryGetOAuthState(out var oAuthState))
{
analyticsEvent.AddData("client_id", oAuthState.ClientId);
}
}
else if (clientRedirectInfo is not null)
{
analyticsEvent.AddData("client_id", clientRedirectInfo.ClientId);
}

return _next(context);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public static async Task Main(string[] args)
a =>
{
a.UseDfeAnalytics();
a.UseMiddleware<Infrastructure.Middleware.AppendSessionIdToAnalyticsEventsMiddleware>();
a.UseMiddleware<Infrastructure.Middleware.AppendAuthorizationInfoToAnalyticsEventsMiddleware>();
});
}

Expand Down

0 comments on commit c21ccfb

Please sign in to comment.