GoogleAnalytics.Blazor
1.0.3
dotnet add package GoogleAnalytics.Blazor --version 1.0.3
NuGet\Install-Package GoogleAnalytics.Blazor -Version 1.0.3
<PackageReference Include="GoogleAnalytics.Blazor" Version="1.0.3" />
<PackageVersion Include="GoogleAnalytics.Blazor" Version="1.0.3" />
<PackageReference Include="GoogleAnalytics.Blazor" />
paket add GoogleAnalytics.Blazor --version 1.0.3
#r "nuget: GoogleAnalytics.Blazor, 1.0.3"
#:package GoogleAnalytics.Blazor@1.0.3
#addin nuget:?package=GoogleAnalytics.Blazor&version=1.0.3
#tool nuget:?package=GoogleAnalytics.Blazor&version=1.0.3
GoogleAnalytics.Blazor
Blazor extensions for Google Analytics
Forked from Blazor-Analytics, itself MIT License, Copyright (c) 2019 Ivan Sanz Carasa.
Developed by Material-Blazor.
Configuration
All Projects
First, import the namespaces in _Imports.razor
+@using GoogleAnalytics.Blazor
Then, add the GBAnchor component to the end of App.razor.
+ <GBAnchor />
ServerSide Specific Configuration
Edit _Layout.cshtml:
<script src="_framework/blazor.server.js"></script>
+ <script src="_content/GoogleAnalytics.Blazor/googleanalytics.blazor.js"></script>
WebAssembly Specific Configuration
Edit index.html and apply the following change:
<script src="_framework/blazor.webassembly.js"></script>
+ <script src="_content/GoogleAnalytics.Blazor/googleanalytics.blazor.js"></script>
Setting up GoogleAnalytics
Inside Program.cs, call AddGBService. This will configure [YOUR_GTAG_ID] automatically. You can elect to
omit the tag id and call SetTargetId from the service injected into a component or service at a later stage; this
is helpful if for instance you need to run a back end query in order to determine the tag.
+ builder.Services.AddGBService("[YOUR_GTAG_ID]");
You may wish to set additional config info and/or
globally applicable event params (i.e. applied to every call to TrackEvent):
+ builder.Services.AddGBService(
+ "[YOUR_GTAG_ID]",
+ additionalConfigInfo: new Dictionary<string, object>()
+ {
+ { "user_id", "[YOUR_USER_ID]" }
+ },
+ globalEventParams: new Dictionary<string, object>()
+ {
+ { "user_id", "YOUR_USER_ID" }
+ });
You can set all three of these items using IGBAnalyticsManager.SetTrackingId(), IGBAnalyticsManager.SetAdditionalConfigInfo() and IGBAnalyticsManager.SetGlobalEventParams()
respectively, after having injected the IGBAnalyticsManager service into a component or another service (either scoped or transient).
How to trigger an Analytics Event
- Inject
IGBAnalyticsManagerwherever you want to trigger the event. - Call
IGBAnalyticsManager.TrackEventpassing theEventNameandEventData(an object containing the event data).
Or
Call IGBAnalyticsManager.TrackEvent passing the EventName, Value and Category (optional).
@using GoogleAnalytics.Blazor
@inject IGBAnalyticsManager AnalyticsManager
AnalyticsManager.TrackEvent("generate_lead", new {currency = "USD", value = 99.99});
How to disable tracking on any page
- Inject
IGBAnalyticsManagerinto a blazor component.
@using GoogleAnalytics.Blazor
@inject IGBAnalyticsManager AnalyticsManager
- Suppress tracking for just the current page.
protected override void OnInitialized()
{
AnalyticsManager.SuppressNextPageHitTracking();
}
- Disable page tracking for the whole application.
protected override void OnInitialized()
{
AnalyticsManager.DisableGlobalTracking();
}
- Re-enable page tracking for the whole application.
protected override void OnInitialized()
{
AnalyticsManager.EnableGlobalTracking();
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components (>= 7.0.2)
- Microsoft.AspNetCore.Components.Web (>= 7.0.2)
- Microsoft.AspNetCore.Hosting (>= 2.2.7)
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.3 | 21,163 | 1/22/2023 |
| 1.0.2 | 2,605 | 9/15/2022 |
| 1.0.1 | 528 | 9/15/2022 |
| 1.0.0 | 606 | 9/2/2022 |
| 1.0.0-preview.5 | 255 | 8/30/2022 |
| 1.0.0-preview.4 | 191 | 8/30/2022 |
| 1.0.0-preview.3 | 1,247 | 6/25/2022 |
| 1.0.0-preview.2 | 224 | 6/24/2022 |
| 1.0.0-preview.1 | 250 | 6/23/2022 |