How to call Google Tag Manager from Blazor Server

Viewed 334

I need to call a google GTM script from my Blazor Server site. I understand that by architecture I can’t have a script call in the HTML portion of Index.Razor. I had already called in the same project a Javascript tracking function in the override of OnInitialized in my Index.razor and that worked OK, but when I tried the same solution on the Google GTM it was not been fired by the website.

I found a closed issued 16218 in GitHub (closed by Steve Sanderson), while trying to implement a suggestion by BrainSlugs83.

The solution proposed on the closed issue was to create a shared component which was temporarily suppressing error BL999. In the solution he mentions that then we could add the script in the HTML. I've tried and still gives me the same error, maybe because I did not invoked it correctly.

The "pixel" tracking Javascript code is just Javascript code, the GTM is a Javascript function.

I need to know how to call the component proposed on the closed issue from my OnInitialized

 JS.InvokeVoidAsync("pixel");  // this worked
//JS.InvokeVoidAsync("GTM");  // this did not work
////////////////////////////////////// ??NavManager.NavigateTo(GTM);
var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
//     userData.ParseUri(uri);
base.OnInitialized();

If anyone from the community knows how to call the GTM script from Blazor Server, please help.

Thanks in advance.

0 Answers
Related