Hey folks,
Earlier this week, I wrote about how I migrated my Hugo blog site from Azure Storage Account-based site to the newer Azure Static Web Apps with Hugo.
While this was a smooth process, both migrating the actual site content, as well as transferring the public domain name, the piece missing was the statistics. I always used Azure Application Insights for this, but specifically for Azure Static Web Apps, App Insights is only supported when using Functions (as per this article on the Microsoft docs). Which I don’t have with Hugo.
However, App Insights also supports a JavaScript-based approach, and this works fine with Hugo static website.
Let’s get this going…
- The first step involves deploying an Azure Application Insights resource from the portal.

- Enter the necessary details to get your App Insights resource deployed:
- Subscription
- Resource Group
- App Insights Instance Name
- Region
- Resource-Mode Workspace-based
- Log Analytics WorkSpace: accept the suggested one (or select an existing one if you already have one and want to consolidate the logging information)

- After a few minutes, the resource got created successfully. Navigate to the blade

- From the blade, notice the Instrumentation Key in the top right corner. Copy this key aside, as you need to add it into the Hugo config file.
With App Insights up-and-running, let’s head over to our Hugo site source files. Look for a file “config.toml” in the root of your Hugo folder structure. Open the file in an editor, and add the following snippet into the “[param]” section of the config file:
|
|
replacing the sample key with the Instrumentation Key of your Azure Application Insights resource you copied earlier.
- Next, to make sure your App Insights statistics get captured for every visit of every page of the site, add a little snippet of code for appinsights to the top section of the baseof.html file, which should be in the \themes<theme>\layouts_default\ folder of the Hugo Theme you are using.
|
|
- Next, create a new file called appinsights.html in the \themes<theme>\layouts\partials\ folder of the Hugo Theme you are using, having the following code in it:
|
|
-
Save the files and wait for the Static Web Apps pipeline to complete the update successfully.
-
Navigate to your blog website, and open a few different articles, shortlinks to other parts in the web site or navigate back-and-forth to the home page. This to generate some statistics information.
-
After only a few minutes, your App Insights data will get loaded, which can be retrieved from App Insights / Usage / section, using different views:

For example, select Users, which shows the number of unique visitors over the last 24 hours (note you can drill down to the last 30min, up to any custom period in time).

Click on the View More Insights button below the chart, which will expose even more granular information regarding the visits. For example the location, time frame, client, browser version, etc… all the way to the full sequence of blog articles visited.

In this article, I explained how to integrate Azure App Insights into a Hugo-based Azure Static Web Apps, using some JavaScript and HTML code.
If you are running Hugo on Azure SWA as well, let me know!
Cheers!!
/Peter
