diff --git a/README.md b/README.md index dbf3def..06034dc 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ https://nuget.org/packages/Blazor-Analytics First, import the namespaces in `_Imports.razor` -``` +```csharp @using Blazor.Analytics @using Blazor.Analytics.Components ``` @@ -18,7 +18,7 @@ First, import the namespaces in `_Imports.razor` Then, add the `NavigationTracker` component below your Router in `App.razor`.
The tracker listens to every navigation change while it's rendered on a page. -```diff +```csharp diff + ``` @@ -27,7 +27,7 @@ The tracker listens to every navigation change while it's rendered on a page. Edit `_Host.cshtml` and apply the following change: -```diff +```html diff + ``` @@ -36,7 +36,7 @@ Edit `_Host.cshtml` and apply the following change: Edit `index.html` and apply the following change: -```diff +```html diff + ``` @@ -45,7 +45,7 @@ Edit `index.html` and apply the following change: Inside your main `Startup`/`Program`, call `AddGoogleAnalytics`. This will configure your GTAG_ID automatically. -```diff +```csharp diff + services.AddGoogleAnalytics("YOUR_GTAG_ID"); ``` @@ -56,7 +56,7 @@ Inside your main `Startup`/`Program`, call `AddGoogleAnalytics`. This will confi
Or
Call `IAnalytics.TrackEvent` passing the `EventName`, `Value` and `Category` (optional). -``` +```csharp @inject Blazor.Analytics.IAnalytics Analytics Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99}); @@ -66,20 +66,33 @@ Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99}); 1.- Inject ITrackingState on blazor component -``` +```csharp @using Blazor.Analytics.Abstractions -@inject ITrackingState DisableNavigation +@using Blazor.Analytics + +@inject ITrackingNavigationState TrackingNavigationState +@inject IAnalytics GlobalTracking ``` 2.- Disable tracking on initialized -``` +2.1 For current page +```csharp protected override void OnInitialized() { DisableNavigation.DisableTracking(); } ``` +2.2 For whole application +```csharp +protected override void OnInitialized() +{ + GlobalTracking.Disable(); +} +``` # Changelog +### v3.7.1 +- Support for globally enable/disable tracking for the whole application ### v3.7.0 - Support for disable tracking on any page ### v3.1.0 diff --git a/demo/DemoApp/DemoApp.Client/Pages/Counter.razor b/demo/DemoApp/DemoApp.Client/Pages/Counter.razor index 8f239b5..dea4963 100644 --- a/demo/DemoApp/DemoApp.Client/Pages/Counter.razor +++ b/demo/DemoApp/DemoApp.Client/Pages/Counter.razor @@ -1,7 +1,9 @@ @page "/counter" @using Blazor.Analytics.Abstractions +@using Blazor.Analytics @inject ITrackingNavigationState TrackingNavigationState +@inject IAnalytics GlobalTracking

Counter

@@ -18,6 +20,7 @@ protected override void OnInitialized() { TrackingNavigationState.DisableTracking(); + GlobalTracking.Disable(); } private void IncrementCount() diff --git a/demo/DemoApp/DemoApp.Server/Pages/Counter.razor b/demo/DemoApp/DemoApp.Server/Pages/Counter.razor index 6e4fef3..6995964 100644 --- a/demo/DemoApp/DemoApp.Server/Pages/Counter.razor +++ b/demo/DemoApp/DemoApp.Server/Pages/Counter.razor @@ -1,7 +1,9 @@ @page "/counter" @using Blazor.Analytics.Abstractions +@using Blazor.Analytics @inject ITrackingNavigationState TrackingNavigationState +@inject IAnalytics GlobalTracking

Counter

@@ -18,12 +20,13 @@ protected override void OnInitialized() { TrackingNavigationState.DisableTracking(); + GlobalTracking.Disable(); } 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 = currentCount }); diff --git a/src/Blazor.Analytics/Abstractions/IAnalytics.cs b/src/Blazor.Analytics/Abstractions/IAnalytics.cs index 635e926..bc347b2 100644 --- a/src/Blazor.Analytics/Abstractions/IAnalytics.cs +++ b/src/Blazor.Analytics/Abstractions/IAnalytics.cs @@ -11,5 +11,8 @@ public interface IAnalytics Task TrackEvent(string eventName, string eventCategory = null, string eventLabel = null, int? eventValue = null); Task TrackEvent(string eventName, int eventValue, string eventCategory = null, string eventLabel = null); Task TrackEvent(string eventName, object eventData); + + void Enable(); + void Disable(); } } diff --git a/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs b/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs index 87eaf32..460e906 100644 --- a/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs +++ b/src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs @@ -7,9 +7,7 @@ namespace Blazor.Analytics.Abstractions public interface ITrackingNavigationState { void EnableTracking(); - void DisableTracking(); - bool IsTrackingEnabled(); } } diff --git a/src/Blazor.Analytics/Components/TrackingNavigationState.cs b/src/Blazor.Analytics/Components/TrackingNavigationState.cs index f33dfb6..a3f4618 100644 --- a/src/Blazor.Analytics/Components/TrackingNavigationState.cs +++ b/src/Blazor.Analytics/Components/TrackingNavigationState.cs @@ -7,12 +7,12 @@ namespace Blazor.Analytics.GoogleAnalytics { public class TrackingNavigationState : ITrackingNavigationState { - private bool _isEnableTracking = true; + private bool _isTrackingEnabled = true; - public void DisableTracking() => _isEnableTracking = false; + public void DisableTracking() => _isTrackingEnabled = false; - public void EnableTracking() => _isEnableTracking = true; + public void EnableTracking() => _isTrackingEnabled = true; - public bool IsTrackingEnabled() => _isEnableTracking; + public bool IsTrackingEnabled() => _isTrackingEnabled; } } diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs index 8de9f80..0a1324b 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Blazor.Analytics.Abstractions; using Blazor.Analytics.Constants; using Microsoft.JSInterop; @@ -8,13 +9,15 @@ namespace Blazor.Analytics.GoogleAnalytics public sealed class GoogleAnalyticsStrategy : IAnalytics { private readonly IJSRuntime _jsRuntime; + private bool _isGloballyEnabledTracking = true; private string _trackingId = null; public bool _isInitialized = false; public bool _debug = false; public GoogleAnalyticsStrategy( - IJSRuntime jsRuntime) + IJSRuntime jsRuntime + ) { _jsRuntime = jsRuntime; } @@ -41,6 +44,11 @@ await _jsRuntime.InvokeAsync( public async Task TrackNavigation(string uri) { + if (!_isGloballyEnabledTracking) + { + return; + } + if (!_isInitialized) { await Initialize(_trackingId); @@ -71,6 +79,11 @@ public Task TrackEvent(string eventName, int eventValue, string eventCategory = public async Task TrackEvent(string eventName, object eventData) { + if (!_isGloballyEnabledTracking) + { + return; + } + if (!_isInitialized) { await Initialize(_trackingId); @@ -80,5 +93,9 @@ await _jsRuntime.InvokeAsync( GoogleAnalyticsInterop.TrackEvent, eventName, eventData); } + + public void Enable() => _isGloballyEnabledTracking = true; + + public void Disable() => _isGloballyEnabledTracking = false; } }