Blazor.GoogleTagManager 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Blazor.GoogleTagManager --version 1.0.1                
NuGet\Install-Package Blazor.GoogleTagManager -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blazor.GoogleTagManager" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.GoogleTagManager --version 1.0.1                
#r "nuget: Blazor.GoogleTagManager, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Blazor.GoogleTagManager as a Cake Addin
#addin nuget:?package=Blazor.GoogleTagManager&version=1.0.1

// Install Blazor.GoogleTagManager as a Cake Tool
#tool nuget:?package=Blazor.GoogleTagManager&version=1.0.1                

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.

Nuget GitHub

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 />

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

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 scrip you will see following

<script async src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXXX" data-consent-category="google"></script>

Not Supported Scenarios

There is no support for the Content Security Policy out of the box as that would require additional JavaScript moddification.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
3.0.0 2,454 8/26/2024
2.0.0 5,470 5/27/2024
1.1.1 15,696 6/19/2022
1.1.0 404 5/18/2022
1.0.2 409 5/18/2022
1.0.1 413 5/17/2022