Blazor.GoogleTagManager
3.0.0
dotnet add package Blazor.GoogleTagManager --version 3.0.0
NuGet\Install-Package Blazor.GoogleTagManager -Version 3.0.0
<PackageReference Include="Blazor.GoogleTagManager" Version="3.0.0" />
paket add Blazor.GoogleTagManager --version 3.0.0
#r "nuget: Blazor.GoogleTagManager, 3.0.0"
// Install Blazor.GoogleTagManager as a Cake Addin #addin nuget:?package=Blazor.GoogleTagManager&version=3.0.0 // Install Blazor.GoogleTagManager as a Cake Tool #tool nuget:?package=Blazor.GoogleTagManager&version=3.0.0
Blazor.GoogleTagManager
This is a fork of Havit.Blazor.GoogleTagManager but without Havit.Core, since for Blazor WASM every byte counts. This library is trim friendly.
🎉 Release Notes
Changelog
🚀Getting Started
Register Services
Blazor ServerSide or WASM
builder.Services.AddGoogleTagManager(options =>
{
options.GtmId = "GTM-XXXXXXX";
});
Add Imports
After the package is added, you need to add the following in your _Imports.razor
@using Blazor.GoogleTagManager;
Add Components
Add the following component to your MainLayout.razor
<GoogleTagManagerPageViewTracker />
NB! There is no need to add _content/Blazor.GoogleTagManager/GoogleTagManager.js
in indedx.html / _Host.cshtml, the script is imported automatically.
đź“śSample Usage
For general use case, please refer to google tutorials or any other learning materials.
Manual push
Only if you need to trigger custom events from the code.
In the razor component
@inject IGoogleTagManager GoogleTagManager;
<button @onclick="OnButtonClick">Click</button>
@code {
private async Task OnButtonClick(){
await GoogleTagManager.PushAsync(new { @event = "button_click_sample_event" });
}
}
⚙️Additional Settings
Attributes
You can add attributes, this can be useful for cookie consent
builder.Services.AddGoogleTagManager(options =>
{
options.GtmId = "GTM-XXXXXXX";
options.Attributes = new Dictionary<string, string>
{
{ "data-consent-category", "google" }
};
});
then in your script you will see following
<script async src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXXX" data-consent-category="google"></script>
Other useful links for consent settings: link1 link2
Debug to console
You can enable debugging to the browser console. This helps to see whatever the library is initialized properly.
Keep in mind that this will only output the events that is done by this library, it will not show the triggers that were configured in the Google Tag Manager Dashboard. For the rest, please, use this debug section.
builder.Services.AddGoogleTagManager(options =>
{
options.GtmId = "GTM-XXXXXXX";
options.DebugToConsole = true;
});
Example output
[GTM]: Configured with GtmId = GTM-XXXXXXX
[GTM]:{"pageUrl":"https://localhost:5001/","event":"virtualPageView"}
[GTM]:{"isNavigationIntercepted":"True","pageUrl":"https://localhost:5001/counter","event":"virtualPageView","gtm.uniqueEventId":14}
[GTM]:{"event":"button_click_sample_event","gtm.uniqueEventId":16}
[GTM]:{"event":"button_click_sample_event","gtm.uniqueEventId":17}
⚠️NB! Do not use this option in production.
📢Troubleshooting
If nothing happens, even a simple pageview
event, and you are sure you configured the library and Google Tag Manager correctly, then check if adblocker/firewall doesn't block the Google Tag Manager script. For example, AdGuard by default can remove tracking scripts.
Try to use console and other tools to make sure that the script(https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXXX
) is present on the page and that you have access to dataLayer object.
Debugging your Google Tag Manager
There is debug / tag assistant feature for Google Tag Manager that will always help you to debug your triggers and show that your GTM is hooked up properly.
đź“ŚLimitations / Not Supported Scenarios
There is no support for the Content Security Policy out of the box, as that would require additional JavaScript modification. There is also no support for renaming the dataLayer object for the Google Tag Manager.
However, starting from version 3.0.0, you can disable automatic script import by setting ImportJsAutomatically = false
via GoogleTagManagerOptions
:
builder.Services.AddGoogleTagManager(options => { options.GtmId = "GTM-XXXXXXX"; options.ImportJsAutomatically = false; });
And manually import your own script using a <script>
tag in index.html
or App.razor
. This allows you to add a nonce for CSP, rename the dataLayer, and modify the script however you want.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. 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 is compatible. 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 6.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.