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

Added: Google Analytics Configurator #452

Merged
merged 2 commits into from
Jul 21, 2023
Merged
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
6 changes: 3 additions & 3 deletions CleanArchitecture.Blazor.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>CleanArchitecture.Blazor.Solution.Template</id>
<version>1.0.0-preview.5</version>
<version>1.0.0-preview.6</version>
<title>Clean Architecture Blazor Solution Template</title>
<authors>hl.z</authors>
<description>Clean Architecture Blazor Solution Template for .NET 7.</description>
<summary>
A Clean Architecture Blazor Server Solution Template for creating a Single-Page Application (SPA) with ASP.NET Core.
</summary>
<releaseNotes>
1.0.0-preview.5
- ♻️ recycle(Infrastructure): Switch events dispatcher to interceptor.
1.0.0-preview.6
- Made Google Analytics configurable.
</releaseNotes>

<projectUrl>https://github.com/neozhu/CleanArchitectureWithBlazorServer</projectUrl>
Expand Down
26 changes: 20 additions & 6 deletions src/Application/Common/Configurations/PrivacySettings.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
namespace CleanArchitecture.Blazor.Application.Common.Configurations;

/// <summary>
/// Configuration wrapper for the privacy section
/// Represents the privacy settings for the application.
/// </summary>
public class PrivacySettings
{
/// <summary>
/// Privacy key constraint
/// Gets the unique key to identify the PrivacySettings configuration.
/// </summary>
public const string Key = nameof(PrivacySettings);

/// <summary>
/// When enabled, the logs will include client IP addresses
/// Gets or sets a value indicating whether the client IP addresses should be logged.
/// </summary>
public bool LogClientIpAddresses { get; set; } = true;

/// <summary>
/// When enabled, the logs will include the client agents
/// Gets or sets a value indicating whether the client agents (user agents) should be logged.
/// </summary>
public bool LogClientAgents { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether Google Analytics should be used for tracking.
/// </summary>
public bool UseGoogleAnalytics { get; set; }

/// <summary>
/// Gets or sets the Google Analytics tracking key.
/// </summary>
/// <remarks>
/// If <see cref="UseGoogleAnalytics" /> is set to true, this property must contain a valid Google Analytics tracking
/// key.
/// </remarks>
public string? GoogleAnalyticsKey { get; set; }
}
57 changes: 33 additions & 24 deletions src/Blazor.Server.UI/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Blazor.Server.UI.Services.Notifications;
using Blazor.Server.UI.Services.UserPreferences;
using BlazorDownloadFile;
using CleanArchitecture.Blazor.Application.Common.Configurations;
using MudBlazor.Services;
using MudExtensions.Services;
using Toolbelt.Blazor.Extensions.DependencyInjection;
Expand All @@ -12,18 +13,18 @@ namespace Blazor.Server.UI;

public static class ConfigureServices
{
public static IServiceCollection AddBlazorUiServices(this IServiceCollection services)
public static WebApplicationBuilder AddBlazorUiServices(this WebApplicationBuilder builder)
{
services.AddRazorPages();
services.AddServerSideBlazor(
options =>
{
options.DetailedErrors = true;
options.DisconnectedCircuitMaxRetained = 100;
options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromMinutes(3);
options.JSInteropDefaultCallTimeout = TimeSpan.FromMinutes(1);
options.MaxBufferedUnacknowledgedRenderBatches = 10;
}
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(
options =>
{
options.DetailedErrors = true;
options.DisconnectedCircuitMaxRetained = 100;
options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromMinutes(3);
options.JSInteropDefaultCallTimeout = TimeSpan.FromMinutes(1);
options.MaxBufferedUnacknowledgedRenderBatches = 10;
}
).AddHubOptions(options =>
{
options.ClientTimeoutInterval = TimeSpan.FromSeconds(30);
Expand All @@ -35,9 +36,9 @@ public static IServiceCollection AddBlazorUiServices(this IServiceCollection ser
options.StreamBufferCapacity = 10;
})
.AddCircuitOptions(option => { option.DetailedErrors = true; });
services.AddMudBlazorDialog();
services.AddHotKeys2();
services.AddMudServices(config =>
builder.Services.AddMudBlazorDialog();
builder.Services.AddHotKeys2();
builder.Services.AddMudServices(config =>
{
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomRight;
config.SnackbarConfiguration.PreventDuplicates = false;
Expand All @@ -49,14 +50,22 @@ public static IServiceCollection AddBlazorUiServices(this IServiceCollection ser
config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
});

services.AddMudExtensions();
services.AddScoped<LayoutService>();
services.AddBlazorDownloadFile();
services.AddScoped<IUserPreferencesService, UserPreferencesService>();
services.AddScoped<IMenuService, MenuService>();
services.AddScoped<INotificationService, InMemoryNotificationService>();
services.AddGoogleAnalytics("G-PRYNCB61NV");
services.AddHealthChecks();
return services;
builder.Services.AddMudExtensions();
builder.Services.AddScoped<LayoutService>();
builder.Services.AddBlazorDownloadFile();
builder.Services.AddScoped<IUserPreferencesService, UserPreferencesService>();
builder.Services.AddScoped<IMenuService, MenuService>();
builder.Services.AddScoped<INotificationService, InMemoryNotificationService>();
builder.Services.AddHealthChecks();

var privacySettings = builder.Configuration.GetRequiredSection(PrivacySettings.Key).Get<PrivacySettings>();
if (privacySettings is not { UseGoogleAnalytics: true }) return builder;

if (privacySettings.GoogleAnalyticsKey is null or "")
throw new ArgumentNullException(nameof(privacySettings.GoogleAnalyticsKey));

builder.Services.AddGoogleAnalytics(privacySettings.GoogleAnalyticsKey);

return builder;
}
}
}
12 changes: 6 additions & 6 deletions src/Blazor.Server.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
using CleanArchitecture.Blazor.Infrastructure.Persistence;
using Microsoft.AspNetCore.Http.Connections;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(args);

builder.RegisterSerilog();
builder.Services.AddBlazorUiServices();
builder.AddBlazorUiServices();
builder.Services.AddInfrastructureServices(builder.Configuration)
.AddApplicationServices();

WebApplication app = builder.Build();
var app = builder.Build();

app.MapHealthChecks("/health");
app.UseExceptionHandler("/Error");
Expand All @@ -24,12 +24,12 @@
if (app.Environment.IsDevelopment())
{
// Initialise and seed database
using (IServiceScope scope = app.Services.CreateScope())
using (var scope = app.Services.CreateScope())
{
ApplicationDbContextInitializer initializer = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitializer>();
var initializer = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitializer>();
await initializer.InitialiseAsync();
await initializer.SeedAsync();
INotificationService? notificationService = scope.ServiceProvider.GetService<INotificationService>();
var notificationService = scope.ServiceProvider.GetService<INotificationService>();
if (notificationService is InMemoryNotificationService inMemoryNotificationService)
{
inMemoryNotificationService.Preload();
Expand Down
6 changes: 4 additions & 2 deletions src/Blazor.Server.UI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
"MailPickupDirectory": "",
"SocketOptions": null
},
"PrivacySettings" : {
"PrivacySettings": {
"LogClientIpAddresses": true,
"LogClientAgents" : true
"LogClientAgents": true,
"UseGoogleAnalytics": false,
"GoogleAnalyticsKey": ""
},
"Serilog": {
"MinimumLevel": {
Expand Down