-
Notifications
You must be signed in to change notification settings - Fork 39
/
GoogleAnalyticsComponent.cs
43 lines (34 loc) · 1.17 KB
/
GoogleAnalyticsComponent.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
using System;
using System.Threading.Tasks;
using Blazor.Analytics.Constants;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Blazor.Analytics.GoogleAnalytics.Components
{
public class GoogleAnalyticsComponent : ComponentBase, IDisposable
{
[Parameter]
protected string TrackingId { get; set; } = null;
[Inject]
protected IUriHelper UriHelper { get; set; } = null;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null;
protected override async Task OnInitAsync()
{
base.OnInit();
UriHelper.OnLocationChanged += OnLocationChanged;
await JSRuntime.InvokeAsync<string>(GoogleAnalyticsInterop.Configure,
TrackingId);
}
public void Dispose()
{
UriHelper.OnLocationChanged -= OnLocationChanged;
}
private async void OnLocationChanged(object sender, string absoluteUri)
{
var relativeUri = new Uri(absoluteUri).PathAndQuery;
await JSRuntime.InvokeAsync<string>(GoogleAnalyticsInterop.Navigate,
TrackingId, relativeUri);
}
}
}