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 +
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 +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 @@