Skip to content

Commit

Permalink
Add debug parameter which controls console logs (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger authored Nov 3, 2020
1 parent 85eda1a commit 4e9d3ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ 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<IAnalytics>(p =>
{
var googleAnalytics = ActivatorUtilities.CreateInstance<GoogleAnalyticsStrategy>(p);

if (trackingId != null)
{
googleAnalytics.Configure(trackingId);
googleAnalytics.Configure(trackingId, debug);
}

return googleAnalytics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics

private string _trackingId = null;
public bool _isInitialized = false;
public bool _debug = false;

public GoogleAnalyticsStrategy(
IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

public void Configure(string trackingId)
public void Configure(string trackingId, bool debug)
{
_trackingId = trackingId;
_debug = debug;
}

public async Task Initialize(string trackingId)
Expand All @@ -31,7 +33,7 @@ public async Task Initialize(string trackingId)
}

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Configure, trackingId);
GoogleAnalyticsInterop.Configure, trackingId, _debug);

_trackingId = trackingId;
_isInitialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
}
}
}

0 comments on commit 4e9d3ac

Please sign in to comment.