Skip to content

Commit

Permalink
Added ability to disable tracking on certain pages (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomás López <tlopez@facephi.com>
  • Loading branch information
CrahunGit and Tomás López authored Jul 29, 2021
1 parent 796d07d commit 8febc94
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
12 changes: 10 additions & 2 deletions demo/DemoApp/DemoApp.Client/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@page "/counter"

@using Blazor.Analytics.Abstractions
@inject ITrackingNavigationState TrackingNavigationState

<h1>Counter</h1>

<p>Current count: @currentCount</p>
Expand All @@ -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 });
}
}
10 changes: 9 additions & 1 deletion demo/DemoApp/DemoApp.Server/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@page "/counter"

@using Blazor.Analytics.Abstractions
@inject ITrackingNavigationState TrackingNavigationState

<h1>Counter</h1>

<p>Current count: @currentCount</p>
Expand All @@ -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 });
}
}
15 changes: 15 additions & 0 deletions src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion src/Blazor.Analytics/Blazor.Analytics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<!-- TypeScript Build -->
<ItemGroup>
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.2.4">
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.3.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
11 changes: 10 additions & 1 deletion src/Blazor.Analytics/Components/NavigationTracker.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using System.Threading.Tasks
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Routing
@using Blazor.Analytics.Abstractions

@code
{
Expand All @@ -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();
Expand Down Expand Up @@ -38,6 +42,11 @@

private async Task OnLocationChanged(string location)
{
await Analytics.TrackNavigation(location);
if (TrackingNavigationState.IsTrackingEnabled())
{
await Analytics.TrackNavigation(location);
}

TrackingNavigationState.EnableTracking();
}
}
18 changes: 18 additions & 0 deletions src/Blazor.Analytics/Components/TrackingNavigationState.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blazor.Analytics.GoogleAnalytics;
using Blazor.Analytics.Abstractions;
using Blazor.Analytics.GoogleAnalytics;
using Microsoft.Extensions.DependencyInjection;

namespace Blazor.Analytics
Expand All @@ -14,6 +15,7 @@ public static IServiceCollection AddGoogleAnalytics(
string trackingId,
bool debug)
{
services.AddScoped<ITrackingNavigationState, TrackingNavigationState>();
return services.AddScoped<IAnalytics>(p =>
{
var googleAnalytics = ActivatorUtilities.CreateInstance<GoogleAnalyticsStrategy>(p);
Expand Down

0 comments on commit 8febc94

Please sign in to comment.