Skip to content

Commit

Permalink
Added UserId (#52)
Browse files Browse the repository at this point in the history
* Added UserId

* Removed MId

* Implemented globalConfigData & globalEventData

* Removed MId

* Enabled display in demo

* Name cleanup

* Applied new feature with user id in both samples
  • Loading branch information
jay1891 authored Mar 16, 2022
1 parent 3df2b0c commit b0b108f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 17 deletions.
17 changes: 17 additions & 0 deletions demo/DemoApp/DemoApp.Client/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@
@Body
</div>
</div>

@code {
[Inject]
private Blazor.Analytics.IAnalytics Analytics { get; set; }

protected override void OnInitialized()
{
var userId = $"userid{DateTime.Now.Ticks}";
var globalConfigData = new Dictionary<string, object>();
globalConfigData["user_id"] = userId;
var globalEventData = new Dictionary<string, object>();
globalEventData["user_id"] = userId;

this.Analytics.ConfigureGlobalConfigData(globalConfigData);
this.Analytics.ConfigureGlobalEventData(globalEventData);
}
}
21 changes: 20 additions & 1 deletion demo/DemoApp/DemoApp.Server/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inherits LayoutComponentBase
@using System.Threading
@inherits LayoutComponentBase

<div class="sidebar">
<NavMenu />
Expand All @@ -13,3 +14,21 @@
@Body
</div>
</div>

@code {

[Inject]
private Blazor.Analytics.IAnalytics Analytics { get; set; }

protected override void OnInitialized()
{
var userId = $"userid{DateTime.Now.Ticks}";
var globalConfigData = new Dictionary<string, object>();
globalConfigData["user_id"] = userId;
var globalEventData = new Dictionary<string, object>();
globalEventData["user_id"] = userId;

this.Analytics.ConfigureGlobalConfigData(globalConfigData);
this.Analytics.ConfigureGlobalEventData(globalEventData);
}
}
1 change: 1 addition & 0 deletions demo/DemoApp/DemoApp.Server/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand Down
6 changes: 4 additions & 2 deletions src/Blazor.Analytics/Abstractions/IAnalytics.cs
Original file line number Diff line number Diff line change
@@ -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<string, object> globalConfigData);
Task ConfigureGlobalEventData(Dictionary<string, object> globalEventData);

Task TrackNavigation(string uri);

Expand Down
37 changes: 27 additions & 10 deletions src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Blazor.Analytics.Abstractions;
using Blazor.Analytics.Constants;
Expand All @@ -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<string, object> _globalConfigData = new Dictionary<string, object>();
private Dictionary<string, object> _globalEventData = new Dictionary<string, object>();
private bool _isInitialized = false;
private bool _debug = false;

public GoogleAnalyticsStrategy(
IJSRuntime jsRuntime
Expand All @@ -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<string>(
GoogleAnalyticsInterop.Configure, trackingId, _debug);

_trackingId = trackingId;
GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug);

_isInitialized = true;
}

public async Task ConfigureGlobalConfigData(Dictionary<string, object> globalConfigData)
{
if (!_isInitialized)
{
this._globalConfigData = globalConfigData;

await Initialize();
}
}

public async Task ConfigureGlobalEventData(Dictionary<string, object> globalEventData)
{
this._globalEventData = globalEventData;
}

public async Task TrackNavigation(string uri)
{
if (!_isGloballyEnabledTracking)
Expand All @@ -51,7 +68,7 @@ public async Task TrackNavigation(string uri)

if (!_isInitialized)
{
await Initialize(_trackingId);
await Initialize();
}

await _jsRuntime.InvokeAsync<string>(
Expand Down Expand Up @@ -86,12 +103,12 @@ public async Task TrackEvent(string eventName, object eventData)

if (!_isInitialized)
{
await Initialize(_trackingId);
await Initialize();
}

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventData);
eventName, eventData, _globalEventData);
}

public void Enable() => _isGloballyEnabledTracking = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!`);
Expand All @@ -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}`);
Expand Down

0 comments on commit b0b108f

Please sign in to comment.