Skip to content

Commit

Permalink
Catch interop undefined error instead of having application crash (#61)
Browse files Browse the repository at this point in the history
* Fix application breaking on js interop error

Instead, it now catches the exception and stops tracking the connection.

* Change method signature for async method without await

* Remove unnecessary calls to base class initialization

* Remove unsubscription from initialization method

* Implement IDisposable and have unsubscribe occur there

* Change from OnInitializedAsync to OnIntialized
  • Loading branch information
scott-parkhill authored Mar 30, 2023
1 parent b0b108f commit 19eb521
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/Blazor.Analytics/Components/NavigationTracker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
@using Microsoft.AspNetCore.Components.Routing
@using Blazor.Analytics.Abstractions

@implements IDisposable

@code
{
[Inject]
Expand All @@ -14,18 +16,13 @@
[Inject]
protected ITrackingNavigationState TrackingNavigationState { get; set; } = null;

protected override async Task OnInitializedAsync()
protected override void OnInitialized()
{
await base.OnInitializedAsync();

NavigationManager.LocationChanged -= OnLocationChanged;
NavigationManager.LocationChanged += OnLocationChanged;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
// Track initial navigation
Expand Down
61 changes: 51 additions & 10 deletions src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics
private Dictionary<string, object> _globalEventData = new Dictionary<string, object>();
private bool _isInitialized = false;
private bool _debug = false;

private bool _interopError = false;

public GoogleAnalyticsStrategy(
IJSRuntime jsRuntime
)
Expand All @@ -38,8 +39,16 @@ private async Task Initialize()
throw new InvalidOperationException("Invalid TrackingId");
}

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug);
try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug);
}
catch (JSException)
{
_interopError = true;
Disable();
}

_isInitialized = true;
}
Expand All @@ -54,7 +63,7 @@ public async Task ConfigureGlobalConfigData(Dictionary<string, object> globalCon
}
}

public async Task ConfigureGlobalEventData(Dictionary<string, object> globalEventData)
public void ConfigureGlobalEventData(Dictionary<string, object> globalEventData)
{
this._globalEventData = globalEventData;
}
Expand All @@ -71,8 +80,21 @@ public async Task TrackNavigation(string uri)
await Initialize();
}

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Navigate, _trackingId, uri);
if (_interopError)
{
return;
}

try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Navigate, _trackingId, uri);
}
catch (JSException)
{
_interopError = true;
Disable();
}
}

public async Task TrackEvent(
Expand Down Expand Up @@ -106,12 +128,31 @@ public async Task TrackEvent(string eventName, object eventData)
await Initialize();
}

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventData, _globalEventData);
if (_interopError)
{
return;
}

try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventData, _globalEventData);
}
catch (JSException)
{
_interopError = true;
Disable();
}
}

public void Enable() => _isGloballyEnabledTracking = true;
public void Enable()
{
if (_interopError)
return;

_isGloballyEnabledTracking = true;
}

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

0 comments on commit 19eb521

Please sign in to comment.