From b0b108fa0f34f1955492612ae61a26203d8d982e Mon Sep 17 00:00:00 2001 From: jay1891 <14939333+jay1891@users.noreply.github.com> Date: Wed, 16 Mar 2022 16:59:42 +0100 Subject: [PATCH] Added UserId (#52) * Added UserId * Removed MId * Implemented globalConfigData & globalEventData * Removed MId * Enabled display in demo * Name cleanup * Applied new feature with user id in both samples --- .../DemoApp.Client/Shared/MainLayout.razor | 17 +++++++++ .../DemoApp.Server/Shared/MainLayout.razor | 21 ++++++++++- demo/DemoApp/DemoApp.Server/Startup.cs | 1 + .../Abstractions/IAnalytics.cs | 6 ++- .../GoogleAnalyticsStrategy.cs | 37 ++++++++++++++----- .../Resources/GoogleAnalyticsInterop.ts | 32 ++++++++++++++-- 6 files changed, 97 insertions(+), 17 deletions(-) diff --git a/demo/DemoApp/DemoApp.Client/Shared/MainLayout.razor b/demo/DemoApp/DemoApp.Client/Shared/MainLayout.razor index 0f4e22a..12121ed 100644 --- a/demo/DemoApp/DemoApp.Client/Shared/MainLayout.razor +++ b/demo/DemoApp/DemoApp.Client/Shared/MainLayout.razor @@ -13,3 +13,20 @@ @Body + +@code { + [Inject] + private Blazor.Analytics.IAnalytics Analytics { get; set; } + + protected override void OnInitialized() + { + var userId = $"userid{DateTime.Now.Ticks}"; + var globalConfigData = new Dictionary(); + globalConfigData["user_id"] = userId; + var globalEventData = new Dictionary(); + globalEventData["user_id"] = userId; + + this.Analytics.ConfigureGlobalConfigData(globalConfigData); + this.Analytics.ConfigureGlobalEventData(globalEventData); + } +} \ No newline at end of file diff --git a/demo/DemoApp/DemoApp.Server/Shared/MainLayout.razor b/demo/DemoApp/DemoApp.Server/Shared/MainLayout.razor index 9b5407d..1d37ed5 100644 --- a/demo/DemoApp/DemoApp.Server/Shared/MainLayout.razor +++ b/demo/DemoApp/DemoApp.Server/Shared/MainLayout.razor @@ -1,4 +1,5 @@ -@inherits LayoutComponentBase +@using System.Threading +@inherits LayoutComponentBase + +@code { + + [Inject] + private Blazor.Analytics.IAnalytics Analytics { get; set; } + + protected override void OnInitialized() + { + var userId = $"userid{DateTime.Now.Ticks}"; + var globalConfigData = new Dictionary(); + globalConfigData["user_id"] = userId; + var globalEventData = new Dictionary(); + globalEventData["user_id"] = userId; + + this.Analytics.ConfigureGlobalConfigData(globalConfigData); + this.Analytics.ConfigureGlobalEventData(globalEventData); + } +} \ No newline at end of file diff --git a/demo/DemoApp/DemoApp.Server/Startup.cs b/demo/DemoApp/DemoApp.Server/Startup.cs index 2ef6a4b..4487c2e 100644 --- a/demo/DemoApp/DemoApp.Server/Startup.cs +++ b/demo/DemoApp/DemoApp.Server/Startup.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; diff --git a/src/Blazor.Analytics/Abstractions/IAnalytics.cs b/src/Blazor.Analytics/Abstractions/IAnalytics.cs index bc347b2..f9e34e2 100644 --- a/src/Blazor.Analytics/Abstractions/IAnalytics.cs +++ b/src/Blazor.Analytics/Abstractions/IAnalytics.cs @@ -1,10 +1,12 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Blazor.Analytics { public interface IAnalytics { - Task Initialize(string trackingId); + Task ConfigureGlobalConfigData(Dictionary globalConfigData); + Task ConfigureGlobalEventData(Dictionary globalEventData); Task TrackNavigation(string uri); diff --git a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs index 0a1324b..ad2a17d 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs +++ b/src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Blazor.Analytics.Abstractions; using Blazor.Analytics.Constants; @@ -12,8 +13,10 @@ public sealed class GoogleAnalyticsStrategy : IAnalytics private bool _isGloballyEnabledTracking = true; private string _trackingId = null; - public bool _isInitialized = false; - public bool _debug = false; + private Dictionary _globalConfigData = new Dictionary(); + private Dictionary _globalEventData = new Dictionary(); + private bool _isInitialized = false; + private bool _debug = false; public GoogleAnalyticsStrategy( IJSRuntime jsRuntime @@ -28,20 +31,34 @@ public void Configure(string trackingId, bool debug) _debug = debug; } - public async Task Initialize(string trackingId) + private async Task Initialize() { - if (trackingId == null) + if (_trackingId == null) { throw new InvalidOperationException("Invalid TrackingId"); } await _jsRuntime.InvokeAsync( - GoogleAnalyticsInterop.Configure, trackingId, _debug); - - _trackingId = trackingId; + GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug); + _isInitialized = true; } + public async Task ConfigureGlobalConfigData(Dictionary globalConfigData) + { + if (!_isInitialized) + { + this._globalConfigData = globalConfigData; + + await Initialize(); + } + } + + public async Task ConfigureGlobalEventData(Dictionary globalEventData) + { + this._globalEventData = globalEventData; + } + public async Task TrackNavigation(string uri) { if (!_isGloballyEnabledTracking) @@ -51,7 +68,7 @@ public async Task TrackNavigation(string uri) if (!_isInitialized) { - await Initialize(_trackingId); + await Initialize(); } await _jsRuntime.InvokeAsync( @@ -86,12 +103,12 @@ public async Task TrackEvent(string eventName, object eventData) if (!_isInitialized) { - await Initialize(_trackingId); + await Initialize(); } await _jsRuntime.InvokeAsync( GoogleAnalyticsInterop.TrackEvent, - eventName, eventData); + eventName, eventData, _globalEventData); } public void Enable() => _isGloballyEnabledTracking = true; diff --git a/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts b/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts index f18e850..9f87d5e 100644 --- a/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts +++ b/src/Blazor.Analytics/GoogleAnalytics/Resources/GoogleAnalyticsInterop.ts @@ -5,6 +5,19 @@ interface Window gtag: (...args: any[]) => void; } +interface ObjectConstructor { + assign(...objects: Object[]): Object; +} + +interface ConfigObject { + [key: string]: any +} + +interface EventDataObject { + [key: string]: any +} + + // declare globals declare const dataLayer: any[]; declare const gtag: (...args: any[]) => void; @@ -18,16 +31,21 @@ gtag("js", new Date()); namespace GoogleAnalyticsInterop { - export function configure(trackingId: string, debug: boolean = false): void + export function configure(trackingId: string, globalConfigObject: ConfigObject, debug: boolean = false): void { this.debug = debug; + this.globalConfigObject = globalConfigObject; const script = document.createElement("script"); script.async = true; script.src = "https://www.googletagmanager.com/gtag/js?id=" + trackingId; document.head.appendChild(script); - gtag("config", trackingId, { 'send_page_view': false }); + let configObject: ConfigObject = {}; + configObject.send_page_view = false; + Object.assign(configObject, globalConfigObject) + + gtag("config", trackingId, configObject); if(this.debug){ console.log(`[GTAG][${trackingId}] Configured!`); @@ -36,15 +54,21 @@ namespace GoogleAnalyticsInterop export function navigate(trackingId: string, href: string): void { - gtag("config", trackingId, { page_location: href }); + let configObject: ConfigObject = {}; + + configObject.page_location = href; + Object.assign(configObject, this.globalConfigObject) + gtag("config", trackingId, configObject); if(this.debug){ console.log(`[GTAG][${trackingId}] Navigated: '${href}'`); } } - export function trackEvent(eventName: string, eventData: object) + export function trackEvent(eventName: string, eventData: EventDataObject, globalEventData: EventDataObject) { + Object.assign(eventData, globalEventData) + gtag("event", eventName, eventData); if (this.debug) { console.log(`[GTAG][Event triggered]: ${eventName}`);