-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25aeb4b
commit d317086
Showing
35 changed files
with
1,188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34607.119 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorAnalytics", "BlazorAnalytics\BlazorAnalytics.csproj", "{E732E1A4-7E87-461D-B13C-E95020AA746A}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorAnalyticsDemo.Client", "BlazorAnalyticsDemo\BlazorAnalyticsDemo.Client\BlazorAnalyticsDemo.Client.csproj", "{760E6CC2-CA76-48F4-91F9-56B05D8E736F}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorAnalyticsDemo.Server", "BlazorAnalyticsDemo\BlazorAnalyticsDemo.Server\BlazorAnalyticsDemo.Server.csproj", "{6508CA61-A356-41E1-8B78-E5C0A8DC2114}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D3BBE991-424C-4954-9D72-520452A426A5}" | ||
ProjectSection(SolutionItems) = preProject | ||
README.md = README.md | ||
EndProjectSection | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E732E1A4-7E87-461D-B13C-E95020AA746A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E732E1A4-7E87-461D-B13C-E95020AA746A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E732E1A4-7E87-461D-B13C-E95020AA746A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E732E1A4-7E87-461D-B13C-E95020AA746A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{760E6CC2-CA76-48F4-91F9-56B05D8E736F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{760E6CC2-CA76-48F4-91F9-56B05D8E736F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{760E6CC2-CA76-48F4-91F9-56B05D8E736F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{760E6CC2-CA76-48F4-91F9-56B05D8E736F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{6508CA61-A356-41E1-8B78-E5C0A8DC2114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{6508CA61-A356-41E1-8B78-E5C0A8DC2114}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{6508CA61-A356-41E1-8B78-E5C0A8DC2114}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{6508CA61-A356-41E1-8B78-E5C0A8DC2114}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {703C7089-1327-420A-84C8-9FCEA078822C} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
@using System.Threading.Tasks | ||
@using Microsoft.AspNetCore.Components | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@rendermode InteractiveServer | ||
|
||
@code{ | ||
protected bool IsFirstRequest = true; | ||
|
||
[Inject] | ||
protected IBlazorAnalytics Analytics { get; set; } = null; | ||
|
||
[Inject] | ||
protected NavigationManager NavigationManager { get; set; } = null; | ||
|
||
private async void OnLocationChanged(object sender, LocationChangedEventArgs args) => await OnLocationChanged(args.Location); | ||
|
||
private async Task OnLocationChanged(string location) | ||
{ | ||
if (!IsFirstRequest) | ||
{ | ||
await Analytics.TrackNavigation(location); | ||
} | ||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
await Analytics.Initialize(); | ||
|
||
await base.OnAfterRenderAsync(firstRender); | ||
|
||
if (firstRender) | ||
{ | ||
await OnLocationChanged(NavigationManager.Uri); | ||
IsFirstRequest = false; | ||
} | ||
} | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
|
||
NavigationManager.LocationChanged -= OnLocationChanged; | ||
NavigationManager.LocationChanged += OnLocationChanged; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
NavigationManager.LocationChanged -= OnLocationChanged; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using BlazorAnalytics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class BAExtensions | ||
{ | ||
public static IServiceCollection AddBlazorAnalytics(this IServiceCollection services) => AddBlazorAnalytics(services, null, null, null); | ||
|
||
//public static IServiceCollection AddBlazorAnalytics( | ||
// this IServiceCollection services, | ||
// IConfigurationSection settings) | ||
//{ | ||
// settings.Value | ||
// BlazorAnalyticsSettings opts = settings.GetSection("BlazorAnalytics"); | ||
// return AddBlazorAnalytics(services, opts.GAID, opts.FBPID, opts.GTMID); | ||
//} | ||
|
||
public static IServiceCollection AddBlazorAnalytics(this IServiceCollection services, string GAID) => AddBlazorAnalytics(services, GAID, null, null); | ||
|
||
public static IServiceCollection AddBlazorAnalytics(this IServiceCollection services, string GAID, string FBPID) => AddBlazorAnalytics(services, GAID, FBPID, null); | ||
|
||
/// <summary> | ||
/// Adds the Blazor Analytics Service. | ||
/// </summary> | ||
/// <param name="services">The services.</param> | ||
/// <param name="GAID">The GA4 Tracking Id.</param> | ||
/// <param name="FBPID">The FaceBook Pixel Tracking Id.</param> | ||
/// <param name="GTMID">The Google Tag Measurement Id.</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddBlazorAnalytics( | ||
this IServiceCollection services, | ||
string GAID, | ||
string FBPID, | ||
string GTMID) | ||
{ | ||
return services.AddScoped<IBlazorAnalytics>(p => | ||
{ | ||
var BA = ActivatorUtilities.CreateInstance<BlazorAnalyticsClient>(p); | ||
|
||
BA.Configure(GAID, FBPID, GTMID); | ||
|
||
return BA; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
|
||
|
||
<ItemGroup> | ||
<None Include="wwwroot\BlazorAnalytics.js" /> | ||
</ItemGroup> | ||
|
||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using Microsoft.JSInterop; | ||
|
||
/// <summary> | ||
/// Blazor Analytics Client | ||
/// </summary> | ||
public sealed class BlazorAnalyticsClient(IJSRuntime jsRuntime) : IBlazorAnalytics | ||
{ | ||
private readonly IJSRuntime _jsRuntime = jsRuntime; | ||
|
||
private string _FBPID = null; | ||
|
||
private string _GAID = null; | ||
|
||
private string _GTMID = null; | ||
|
||
private Task<IJSObjectReference> _module; | ||
|
||
public bool _isInitialized = false; | ||
|
||
private Task<IJSObjectReference> Module => _module ??= | ||
_jsRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/BlazorAnalytics/BlazorAnalytics.js").AsTask(); | ||
|
||
private bool HasToStartFacebookPixel() | ||
{ | ||
return !String.IsNullOrEmpty(_FBPID); | ||
} | ||
|
||
private bool HasToStartGoogleAnalytics() | ||
{ | ||
return !String.IsNullOrEmpty(_GAID); | ||
} | ||
|
||
private bool HasToStartGTM() | ||
{ | ||
return !String.IsNullOrEmpty(_GTMID); | ||
} | ||
|
||
/// <summary> | ||
/// Configures the specified Tracking Services. | ||
/// </summary> | ||
/// <param name="GAID">A GA4 Tracking Id.</param> | ||
/// <param name="FBPID">FaceBook Pixel Tracking Id.</param> | ||
/// <param name="GTMID">A Google Tag Measurement Id.</param> | ||
public void Configure(string GAID, string FBPID, string GTMID) | ||
{ | ||
_GAID = GAID; | ||
_FBPID = FBPID; | ||
_GTMID = GTMID; | ||
} | ||
|
||
public async Task Initialize() | ||
{ | ||
var module = await Module; | ||
|
||
await module.InvokeAsync<string>("InitGlobals"); | ||
|
||
if (HasToStartFacebookPixel()) | ||
{ | ||
await module.InvokeAsync<string>("InitializeFacebookPixel", _FBPID); | ||
} | ||
|
||
if (HasToStartGoogleAnalytics()) | ||
{ | ||
await module.InvokeAsync<string>("InitializeGoogleAnalytics", _GAID); | ||
} | ||
|
||
if (HasToStartGTM()) | ||
{ | ||
await module.InvokeAsync<string>("InitializeGTM", _GTMID); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Just writes some data to the console for testing. | ||
/// </summary> | ||
/// <param name="eventName">Name of the event.</param> | ||
/// <param name="eventValue">The event value.</param> | ||
public async Task LogEvent(string eventName, string eventValue) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("LogEvent", eventName, eventValue); | ||
} | ||
|
||
public async Task TrackEventGA( | ||
string eventName, | ||
string eventValue, | ||
string eventCategory = null, | ||
string eventLabel = null) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("TrackEventsGA", eventName, eventValue, eventCategory, eventLabel); | ||
} | ||
|
||
public async Task TrackEventGA( | ||
string eventName, | ||
object objectValue = null) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("TrackEventsGAByObjectValue", eventName, objectValue); | ||
} | ||
|
||
public async Task TrackEventGTM( | ||
string eventName, | ||
object objectValue = null) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("TrackEventsGTM", eventName, objectValue); | ||
} | ||
|
||
public async Task TrackEventPixel( | ||
string eventName, | ||
object objectValue = null) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("TrackEventsPixel", eventName, objectValue); | ||
} | ||
|
||
/// <summary> | ||
/// Tracks the SPA navigation, so that page views are tracked. | ||
/// </summary> | ||
/// <param name="uri">The URI.</param> | ||
public async Task TrackNavigation(string uri) | ||
{ | ||
var module = await Module; | ||
await module.InvokeAsync<string>("TrackNavigation", uri, _GAID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace BlazorAnalytics; | ||
|
||
/// <summary> | ||
/// Blazor Analytics Settings | ||
/// </summary> | ||
public class BlazorAnalyticsSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the FaceBook Pixel Tracking Id. | ||
/// </summary> | ||
/// <value> | ||
/// The FaceBook Pixel Tracking Id. | ||
/// </value> | ||
public string FBPID { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the GA4 Tracking Id. | ||
/// </summary> | ||
/// <value> | ||
/// The GA4 Tracking Id. | ||
/// </value> | ||
public string GAID { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the Google Tag Measurement Id. | ||
/// </summary> | ||
/// <value> | ||
/// The Google Tag Measurement Id. | ||
/// </value> | ||
public string GTMID { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// <summary> | ||
/// Blazor Analytics Interface | ||
/// </summary> | ||
public interface IBlazorAnalytics | ||
{ | ||
Task Initialize(); | ||
|
||
/// <summary> | ||
/// Log an event, for testing the tracking processes. | ||
/// </summary> | ||
/// <param name="eventName">Name of the event.</param> | ||
/// <param name="eventValue">The event value.</param> | ||
/// <returns></returns> | ||
Task LogEvent(string eventName, string eventValue); | ||
|
||
Task TrackEventGA(string eventName, string eventValue, string eventCategory = null, string eventLabel = null); | ||
|
||
Task TrackEventGA(string eventName, object objectValue = null); | ||
|
||
Task TrackEventGTM(string eventName, object objectValue = null); | ||
|
||
Task TrackEventPixel(string eventName, object objectValue = null); | ||
|
||
Task TrackNavigation(string uri); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using static Microsoft.AspNetCore.Components.Web.RenderMode |
Oops, something went wrong.