Skip to content

Commit

Permalink
Feature/globally disabled tracking (#40)
Browse files Browse the repository at this point in the history
* Added ability to disable tracking on certain pages

* Fixed method names for tracking dependency

* Fixed dependency scoped for tracking state

* Fixed renamed bug

* - Globally disable tracking (not renabled)

* Added ocumentation to readme

* Seggregate disable behaviour into IAnalytics(globally) and ITrackingNavigationState (current page)

* Improved global disable toggle

Co-authored-by: Tomás López <tlopez@facephi.com>
  • Loading branch information
CrahunGit and Tomás López authored Aug 6, 2021
1 parent dca3e04 commit 7b8dbf4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ https://nuget.org/packages/Blazor-Analytics

First, import the namespaces in `_Imports.razor`

```
```csharp
@using Blazor.Analytics
@using Blazor.Analytics.Components
```

Then, add the `NavigationTracker` component below your Router in `App.razor`.<br/>
The tracker listens to every navigation change while it's rendered on a page.

```diff
```csharp diff
<Router ... />
+ <NavigationTracker />
```
Expand All @@ -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
<script src="_framework/blazor.server.js"></script>
+ <script src="_content/Blazor-Analytics/blazor-analytics.js"></script>
```
Expand All @@ -36,7 +36,7 @@ Edit `_Host.cshtml` and apply the following change:

Edit `index.html` and apply the following change:

```diff
```html diff
<script src="_framework/blazor.webassembly.js"></script>
+ <script src="_content/Blazor-Analytics/blazor-analytics.js"></script>
```
Expand All @@ -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");
```

Expand All @@ -56,7 +56,7 @@ Inside your main `Startup`/`Program`, call `AddGoogleAnalytics`. This will confi
<br>Or<br>
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});
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions demo/DemoApp/DemoApp.Client/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@page "/counter"

@using Blazor.Analytics.Abstractions
@using Blazor.Analytics
@inject ITrackingNavigationState TrackingNavigationState
@inject IAnalytics GlobalTracking

<h1>Counter</h1>

Expand All @@ -18,6 +20,7 @@
protected override void OnInitialized()
{
TrackingNavigationState.DisableTracking();
GlobalTracking.Disable();
}

private void IncrementCount()
Expand Down
5 changes: 4 additions & 1 deletion demo/DemoApp/DemoApp.Server/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@page "/counter"

@using Blazor.Analytics.Abstractions
@using Blazor.Analytics
@inject ITrackingNavigationState TrackingNavigationState
@inject IAnalytics GlobalTracking

<h1>Counter</h1>

Expand All @@ -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 });
Expand Down
3 changes: 3 additions & 0 deletions src/Blazor.Analytics/Abstractions/IAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 0 additions & 2 deletions src/Blazor.Analytics/Abstractions/ITrackingNavigationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace Blazor.Analytics.Abstractions
public interface ITrackingNavigationState
{
void EnableTracking();

void DisableTracking();

bool IsTrackingEnabled();
}
}
8 changes: 4 additions & 4 deletions src/Blazor.Analytics/Components/TrackingNavigationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 18 additions & 1 deletion src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Blazor.Analytics.Abstractions;
using Blazor.Analytics.Constants;
using Microsoft.JSInterop;

Expand All @@ -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;
}
Expand All @@ -41,6 +44,11 @@ await _jsRuntime.InvokeAsync<string>(

public async Task TrackNavigation(string uri)
{
if (!_isGloballyEnabledTracking)
{
return;
}

if (!_isInitialized)
{
await Initialize(_trackingId);
Expand Down Expand Up @@ -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);
Expand All @@ -80,5 +93,9 @@ await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventData);
}

public void Enable() => _isGloballyEnabledTracking = true;

public void Disable() => _isGloballyEnabledTracking = false;
}
}

0 comments on commit 7b8dbf4

Please sign in to comment.