-
Notifications
You must be signed in to change notification settings - Fork 39
/
GoogleAnalyticsStrategy.cs
159 lines (136 loc) · 4.18 KB
/
GoogleAnalyticsStrategy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Blazor.Analytics.Abstractions;
using Blazor.Analytics.Constants;
using Microsoft.JSInterop;
namespace Blazor.Analytics.GoogleAnalytics
{
public sealed class GoogleAnalyticsStrategy : IAnalytics
{
private readonly IJSRuntime _jsRuntime;
private bool _isGloballyEnabledTracking = true;
private string _trackingId = null;
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;
private bool _interopError = false;
public GoogleAnalyticsStrategy(
IJSRuntime jsRuntime
)
{
_jsRuntime = jsRuntime;
}
public void Configure(string trackingId, bool debug)
{
_trackingId = trackingId;
_debug = debug;
}
private async Task Initialize()
{
if (_trackingId == null)
{
throw new InvalidOperationException("Invalid TrackingId");
}
try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Configure, _trackingId, _globalConfigData, _debug);
}
catch (JSException)
{
_interopError = true;
Disable();
}
_isInitialized = true;
}
public async Task ConfigureGlobalConfigData(Dictionary<string, object> globalConfigData)
{
if (!_isInitialized)
{
this._globalConfigData = globalConfigData;
await Initialize();
}
}
public void ConfigureGlobalEventData(Dictionary<string, object> globalEventData)
{
this._globalEventData = globalEventData;
}
public async Task TrackNavigation(string uri)
{
if (!_isGloballyEnabledTracking)
{
return;
}
if (!_isInitialized)
{
await Initialize();
}
if (_interopError)
{
return;
}
try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.Navigate, _trackingId, uri);
}
catch (JSException)
{
_interopError = true;
Disable();
}
}
public async Task TrackEvent(
string eventName,
string eventCategory = null,
string eventLabel = null,
int? eventValue = null)
{
await TrackEvent(eventName, new
{
event_category = eventCategory,
event_label = eventLabel,
value = eventValue
});
}
public Task TrackEvent(string eventName, int eventValue, string eventCategory = null, string eventLabel = null)
{
return TrackEvent (eventName, eventCategory, eventLabel, eventValue);
}
public async Task TrackEvent(string eventName, object eventData)
{
if (!_isGloballyEnabledTracking)
{
return;
}
if (!_isInitialized)
{
await Initialize();
}
if (_interopError)
{
return;
}
try
{
await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventData, _globalEventData);
}
catch (JSException)
{
_interopError = true;
Disable();
}
}
public void Enable()
{
if (_interopError)
return;
_isGloballyEnabledTracking = true;
}
public void Disable() => _isGloballyEnabledTracking = false;
}
}