Skip to content

Commit 4e9d3ac

Browse files
authored
Add debug parameter which controls console logs (#26)
1 parent 85eda1a commit 4e9d3ac

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ namespace Blazor.Analytics
55
{
66
public static class GoogleAnalyticsExtensions
77
{
8-
public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services) => AddGoogleAnalytics(services, null);
8+
public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services) => AddGoogleAnalytics(services, null, false);
9+
public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services, string trackingId) => AddGoogleAnalytics(services, trackingId, false);
10+
public static IServiceCollection AddGoogleAnalytics(this IServiceCollection services, bool debug) => AddGoogleAnalytics(services, null, debug);
911

1012
public static IServiceCollection AddGoogleAnalytics(
1113
this IServiceCollection services,
12-
string trackingId)
14+
string trackingId,
15+
bool debug)
1316
{
1417
return services.AddScoped<IAnalytics>(p =>
1518
{
1619
var googleAnalytics = ActivatorUtilities.CreateInstance<GoogleAnalyticsStrategy>(p);
1720

1821
if (trackingId != null)
1922
{
20-
googleAnalytics.Configure(trackingId);
23+
googleAnalytics.Configure(trackingId, debug);
2124
}
2225

2326
return googleAnalytics;

src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics
1111

1212
private string _trackingId = null;
1313
public bool _isInitialized = false;
14+
public bool _debug = false;
1415

1516
public GoogleAnalyticsStrategy(
1617
IJSRuntime jsRuntime)
1718
{
1819
_jsRuntime = jsRuntime;
1920
}
2021

21-
public void Configure(string trackingId)
22+
public void Configure(string trackingId, bool debug)
2223
{
2324
_trackingId = trackingId;
25+
_debug = debug;
2426
}
2527

2628
public async Task Initialize(string trackingId)
@@ -31,7 +33,7 @@ public async Task Initialize(string trackingId)
3133
}
3234

3335
await _jsRuntime.InvokeAsync<string>(
34-
GoogleAnalyticsInterop.Configure, trackingId);
36+
GoogleAnalyticsInterop.Configure, trackingId, _debug);
3537

3638
_trackingId = trackingId;
3739
_isInitialized = true;

src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ gtag("js", new Date());
1818

1919
namespace GoogleAnalyticsInterop
2020
{
21-
export function configure(trackingId: string): void
21+
export function configure(trackingId: string, debug: boolean = false): void
2222
{
23+
this.debug = debug;
2324
const script = document.createElement("script");
2425
script.async = true;
2526
script.src = "https://www.googletagmanager.com/gtag/js?id=" + trackingId;
@@ -28,20 +29,25 @@ namespace GoogleAnalyticsInterop
2829

2930
gtag("config", trackingId);
3031

31-
console.log(`[GTAG][${trackingId}] Configured!`);
32+
if(this.debug){
33+
console.log(`[GTAG][${trackingId}] Configured!`);
34+
}
3235
}
3336

3437
export function navigate(trackingId: string, href: string): void
3538
{
3639
gtag("config", trackingId, { page_location: href });
3740

38-
console.log(`[GTAG][${trackingId}] Navigated: '${href}'`);
41+
if(this.debug){
42+
console.log(`[GTAG][${trackingId}] Navigated: '${href}'`);
43+
}
3944
}
4045

4146
export function trackEvent(event: string, eventCategory: string, eventLabel: string, eventValue: string)
4247
{
4348
gtag("event", event, { event_category: eventCategory, event_label: eventLabel, value: eventValue });
44-
45-
console.log(`[GTAG][Event trigered]: ${event}`);
49+
if(this.debug){
50+
console.log(`[GTAG][Event trigered]: ${event}`);
51+
}
4652
}
4753
}

0 commit comments

Comments
 (0)