From 8febc9416c88d429064fb6d762b6da94d251597b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20L=C3=B3pez?= Date: Thu, 29 Jul 2021 15:09:00 +0200 Subject: [PATCH] Added ability to disable tracking on certain pages (#38) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomás López --- .../DemoApp/DemoApp.Client/Pages/Counter.razor | 12 ++++++++++-- .../DemoApp/DemoApp.Server/Pages/Counter.razor | 10 +++++++++- .../Abstractions/ITrackingNavigationState.cs | 15 +++++++++++++++ src/Blazor.Analytics/Blazor.Analytics.csproj | 2 +- .../Components/NavigationTracker.razor | 11 ++++++++++- .../Components/TrackingNavigationState.cs | 18 ++++++++++++++++++ .../GoogleAnalyticsExtensions.cs | 4 +++- 7 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs create mode 100644 src/Blazor.Analytics/Components/TrackingNavigationState.cs diff --git a/demo/DemoApp/DemoApp.Client/Pages/Counter.razor b/demo/DemoApp/DemoApp.Client/Pages/Counter.razor index 0d5550c..8f239b5 100644 --- a/demo/DemoApp/DemoApp.Client/Pages/Counter.razor +++ b/demo/DemoApp/DemoApp.Client/Pages/Counter.razor @@ -1,5 +1,8 @@ @page "/counter" +@using Blazor.Analytics.Abstractions +@inject ITrackingNavigationState TrackingNavigationState +

Counter

Current count: @currentCount

@@ -12,12 +15,17 @@ [Inject] private Blazor.Analytics.IAnalytics Analytics { get; set; } + protected override void OnInitialized() + { + TrackingNavigationState.DisableTracking(); + } + private void IncrementCount() { currentCount++; - Analytics.TrackEvent("Increment", currentCount, "CountPage"); + Analytics.TrackEvent("Increment", currentCount, "CountPage"); //Example of how to track a generic event (see also https://developers.google.com/gtagjs/reference/ga4-events) - Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99}); + Analytics.TrackEvent("generate_lead", new { currency = "USD", value = 99.99 }); } } diff --git a/demo/DemoApp/DemoApp.Server/Pages/Counter.razor b/demo/DemoApp/DemoApp.Server/Pages/Counter.razor index 099cf9e..6e4fef3 100644 --- a/demo/DemoApp/DemoApp.Server/Pages/Counter.razor +++ b/demo/DemoApp/DemoApp.Server/Pages/Counter.razor @@ -1,5 +1,8 @@ @page "/counter" +@using Blazor.Analytics.Abstractions +@inject ITrackingNavigationState TrackingNavigationState +

Counter

Current count: @currentCount

@@ -12,12 +15,17 @@ [Inject] private Blazor.Analytics.IAnalytics Analytics { get; set; } + protected override void OnInitialized() + { + TrackingNavigationState.DisableTracking(); + } + private void IncrementCount() { currentCount++; //Analytics.TrackEvent("Increment", currentCount, "CountPage"); //Example of how to track a generic event (see also https://developers.google.com/gtagjs/reference/ga4-events) - Analytics.TrackEvent("generate_lead", new {currency = "USD", value = currentCount}); + Analytics.TrackEvent("generate_lead", new { currency = "USD", value = currentCount }); } } diff --git a/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs b/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs new file mode 100644 index 0000000..87eaf32 --- /dev/null +++ b/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Blazor.Analytics.Abstractions +{ + public interface ITrackingNavigationState + { + void EnableTracking(); + + void DisableTracking(); + + bool IsTrackingEnabled(); + } +} diff --git a/src/Blazor.Analytics/Blazor.Analytics.csproj b/src/Blazor.Analytics/Blazor.Analytics.csproj index 7cfccee..4d70dbc 100644 --- a/src/Blazor.Analytics/Blazor.Analytics.csproj +++ b/src/Blazor.Analytics/Blazor.Analytics.csproj @@ -38,7 +38,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Blazor.Analytics/Components/NavigationTracker.razor b/src/Blazor.Analytics/Components/NavigationTracker.razor index c19d7c3..430dbc6 100644 --- a/src/Blazor.Analytics/Components/NavigationTracker.razor +++ b/src/Blazor.Analytics/Components/NavigationTracker.razor @@ -1,6 +1,7 @@ @using System.Threading.Tasks @using Microsoft.AspNetCore.Components @using Microsoft.AspNetCore.Components.Routing +@using Blazor.Analytics.Abstractions @code { @@ -10,6 +11,9 @@ [Inject] protected NavigationManager NavigationManager { get; set; } = null; + [Inject] + protected ITrackingNavigationState TrackingNavigationState { get; set; } = null; + protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); @@ -38,6 +42,11 @@ private async Task OnLocationChanged(string location) { - await Analytics.TrackNavigation(location); + if (TrackingNavigationState.IsTrackingEnabled()) + { + await Analytics.TrackNavigation(location); + } + + TrackingNavigationState.EnableTracking(); } } diff --git a/src/Blazor.Analytics/Components/TrackingNavigationState.cs b/src/Blazor.Analytics/Components/TrackingNavigationState.cs new file mode 100644 index 0000000..f33dfb6 --- /dev/null +++ b/src/Blazor.Analytics/Components/TrackingNavigationState.cs @@ -0,0 +1,18 @@ +using Blazor.Analytics.Abstractions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Blazor.Analytics.GoogleAnalytics +{ + public class TrackingNavigationState : ITrackingNavigationState + { + private bool _isEnableTracking = true; + + public void DisableTracking() => _isEnableTracking = false; + + public void EnableTracking() => _isEnableTracking = true; + + public bool IsTrackingEnabled() => _isEnableTracking; + } +} diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs index c40c174..7159e62 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs @@ -1,4 +1,5 @@ -using Blazor.Analytics.GoogleAnalytics; +using Blazor.Analytics.Abstractions; +using Blazor.Analytics.GoogleAnalytics; using Microsoft.Extensions.DependencyInjection; namespace Blazor.Analytics @@ -14,6 +15,7 @@ public static IServiceCollection AddGoogleAnalytics( string trackingId, bool debug) { + services.AddScoped(); return services.AddScoped(p => { var googleAnalytics = ActivatorUtilities.CreateInstance(p);