From 19eb521b37b2736dc45a1255affe01821d0dd7f5 Mon Sep 17 00:00:00 2001 From: Scott Parkhill Date: Thu, 30 Mar 2023 04:22:34 -0400 Subject: [PATCH] Catch interop undefined error instead of having application crash (#61) * 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 --- .../Components/NavigationTracker.razor | 9 +-- .../GoogleAnalyticsStrategy.cs | 61 ++++++++++++++++--- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/Blazor.Analytics/Components/NavigationTracker.razor b/src/Blazor.Analytics/Components/NavigationTracker.razor index 430dbc6..73bd328 100644 --- a/src/Blazor.Analytics/Components/NavigationTracker.razor +++ b/src/Blazor.Analytics/Components/NavigationTracker.razor @@ -3,6 +3,8 @@ @using Microsoft.AspNetCore.Components.Routing @using Blazor.Analytics.Abstractions +@implements IDisposable + @code { [Inject] @@ -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 diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs index ad2a17d..adab086 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs @@ -17,7 +17,8 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics private Dictionary _globalEventData = new Dictionary(); private bool _isInitialized = false; private bool _debug = false; - + private bool _interopError = false; + public GoogleAnalyticsStrategy( IJSRuntime jsRuntime ) @@ -38,8 +39,16 @@ private async Task Initialize() throw new InvalidOperationException("Invalid TrackingId"); } - await _jsRuntime.InvokeAsync( - GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug); + try + { + await _jsRuntime.InvokeAsync( + GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug); + } + catch (JSException) + { + _interopError = true; + Disable(); + } _isInitialized = true; } @@ -54,7 +63,7 @@ public async Task ConfigureGlobalConfigData(Dictionary globalCon } } - public async Task ConfigureGlobalEventData(Dictionary globalEventData) + public void ConfigureGlobalEventData(Dictionary globalEventData) { this._globalEventData = globalEventData; } @@ -71,8 +80,21 @@ public async Task TrackNavigation(string uri) await Initialize(); } - await _jsRuntime.InvokeAsync( - GoogleAnalyticsInterop.Navigate, _trackingId, uri); + if (_interopError) + { + return; + } + + try + { + await _jsRuntime.InvokeAsync( + GoogleAnalyticsInterop.Navigate, _trackingId, uri); + } + catch (JSException) + { + _interopError = true; + Disable(); + } } public async Task TrackEvent( @@ -106,12 +128,31 @@ public async Task TrackEvent(string eventName, object eventData) await Initialize(); } - await _jsRuntime.InvokeAsync( - GoogleAnalyticsInterop.TrackEvent, - eventName, eventData, _globalEventData); + if (_interopError) + { + return; + } + + try + { + await _jsRuntime.InvokeAsync( + 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; }