From 4e9d3ac5da3d9bddd7d04d6943162b91a15e2ab6 Mon Sep 17 00:00:00 2001 From: Niels Swimberghe Date: Mon, 2 Nov 2020 23:58:07 -0500 Subject: [PATCH] Add debug parameter which controls console logs (#26) --- .../GoogleAnalytics/GoogleAnalyticsExtensions.cs | 9 ++++++--- .../GoogleAnalytics/GoogleAnalyticsStrategy.cs | 6 ++++-- .../Resources/GoogleAnalyticsInterop.ts | 16 +++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs index a23b3b1..c40c174 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs @@ -5,11 +5,14 @@ namespace Blazor.Analytics { public static class GoogleAnalyticsExtensions { - public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services) => AddGoogleAnalytics(services, null); + public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services) => AddGoogleAnalytics(services, null, false); + public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services, string trackingId) => AddGoogleAnalytics(services, trackingId, false); + public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services, bool debug) => AddGoogleAnalytics(services, null, debug); public static IServiceCollection AddGoogleAnalytics( this IServiceCollection services, - string trackingId) + string trackingId, + bool debug) { return services.AddScoped(p => { @@ -17,7 +20,7 @@ public static IServiceCollection AddGoogleAnalytics( if (trackingId != null) { - googleAnalytics.Configure(trackingId); + googleAnalytics.Configure(trackingId, debug); } return googleAnalytics; diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs index 6221be4..2e5b9b0 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs @@ -11,6 +11,7 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics private string _trackingId = null; public bool _isInitialized = false; + public bool _debug = false; public GoogleAnalyticsStrategy( IJSRuntime jsRuntime) @@ -18,9 +19,10 @@ public GoogleAnalyticsStrategy( _jsRuntime = jsRuntime; } - public void Configure(string trackingId) + public void Configure(string trackingId, bool debug) { _trackingId = trackingId; + _debug = debug; } public async Task Initialize(string trackingId) @@ -31,7 +33,7 @@ public async Task Initialize(string trackingId) } await _jsRuntime.InvokeAsync( - GoogleAnalyticsInterop.Configure, trackingId); + GoogleAnalyticsInterop.Configure, trackingId, _debug); _trackingId = trackingId; _isInitialized = true; diff --git a/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts b/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts index 9581944..4e2896c 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts +++ b/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts @@ -18,8 +18,9 @@ gtag("js", new Date()); namespace GoogleAnalyticsInterop { - export function configure(trackingId: string): void + export function configure(trackingId: string, debug: boolean = false): void { + this.debug = debug; const script = document.createElement("script"); script.async = true; script.src = "https://www.googletagmanager.com/gtag/js?id=" + trackingId; @@ -28,20 +29,25 @@ namespace GoogleAnalyticsInterop gtag("config", trackingId); - console.log(`[GTAG][${trackingId}] Configured!`); + if(this.debug){ + console.log(`[GTAG][${trackingId}] Configured!`); + } } export function navigate(trackingId: string, href: string): void { gtag("config", trackingId, { page_location: href }); - console.log(`[GTAG][${trackingId}] Navigated: '${href}'`); + if(this.debug){ + console.log(`[GTAG][${trackingId}] Navigated: '${href}'`); + } } export function trackEvent(event: string, eventCategory: string, eventLabel: string, eventValue: string) { gtag("event", event, { event_category: eventCategory, event_label: eventLabel, value: eventValue }); - - console.log(`[GTAG][Event trigered]: ${event}`); + if(this.debug){ + console.log(`[GTAG][Event trigered]: ${event}`); + } } }