Blazor with Google Analytics


This is not a post about how to use Google analytics. There are hundreds of books and thousands of articles on that subject. I am going to explain my experience of implementing Google analytics into a Blazor Server application and a Blazor Web Assembly application. The applications are remarkably similar, but there are slight differences that I will point out.

I have implemented Google analytics with HTML/CSS/JavaScript applications in the past. It is straight forward implementation; you place a script on your pages to trigger the events you are interested in tracking. On the main page of the application, you add a script and your Google key to associate the events to your account.

Since a Blazor application is a SPA, Single Page Application, it is a little more involved to set the trigger points in order to log the analytics. Lucky for use there is a NuGet package that will help us with the heavy lifting.


NuGet Package

The package is Blazor-Analytics. It is popular. It adds a component in the application router to manage the trigger requests. In Program.cs you instantiate the service with your Google Analytics Id.

    services.AddGoogleAnalytics("YOUR_GTAG_ID");


To trigger the events, you add a call to your event:

    Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99});

Notice you can provide different properties for each trigger event.

In the _layout.cshtml for Server side or the index.html for the web assembly side, you will need to add a reference to a JavaScript file:

    <script src="_content/Blazor-Analytics/blazor-analytics.js"></script>

I have always stated I want to do everything in C# instead of JavaScript but with several of my recent projects I am starting to understand the power that combining the two technologies can give you. This is a good example. Google Analytics was built with the “normal” web in mind, HTML/CSS/JavaScript, wrapping a C# class around it brings into the Blazor world.


My Implementation

I decided I was going to add Analytics to an existing public web site I had built and published several years ago. I updated it to .Net 6, the latest Blazor release and renamed it to Insprational2. It is a simple web site that is meant to make people smile or think a little. 

I followed the instructions to add the NuGet package to the project. My goal is to capture the page views of the main page. For the Sever Side application I added the following to the index page:

await Analytics.TrackEvent("Inspirational Page View", new {app = "Inspirational", value = "SS"});

The "SS" is to the server-side application.

For the Web Assembly application, I had to move the tracking call to the OnInitializedAsync call:

    await Analytics.TrackEvent("Inspirational Page View", new {app = "Inspirational", value = "WASM"});

For this one, I am setting the property value to "WASM". This should allow me to see the differences between the two applications.


Add Configuration

Once it was published and I verified it capturing data, I decided I was only interested in the analytics information from the production sites. I wanted a configuration value to turn the tracking on or off.

Server-side Application

This was straight forward just using app settings. I used the key:

    "AllowTracking" : true,

Then I added an if check to see if tracking was on before calling it on the OnAfterRenderAsync method.

Web Assembly Application

This turned out to be a trickier since using app settings in a Web Assembly application is not a good security idea. I took the simple path of defining a “Settings” class and adding the property “Allow” tracking. This is a Singleton class that will read in the application settings from the server and be available application wide.

I then added the following code to the OnInitializedAsync method to get the setting, check to see if it is on and if so, call the tracking

    var value = await Http.GetFromJsonAsync<Settings><("data/Tracking.txt");
    if (value != null)
    {
         bool.TryParse(value.Tracking, out allowTracking);
         if(allowTracking)
             await Analytics.TrackEvent("Inspirational Page View", new {app = "Inspirational", value = "WASM"});
    }


Summary

I went through and documented this process because I am planning to using Google Analytics more often moving forward in both my personal applications and customer projects. This provides a sample on how to implement Google Analytics for a Blazor app, Server-side or Web assembly.

This implementation ended up being easier than I had expected mainly because of the nice NuGet package that I am using.


[Blazor-Analytics]

[source code]


Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component