Using Azure Application Insights for SWA Statistcs - Part 1 - Setup
Overview
Want to see which pages on your site are the most popular? Static Web Applications (SWA) like those generated by Hugo (see Create New Hugo Blog Site in Azure SWA) and any site that can add Javascript to content can send details to Azure where you can view details through many different lenses. For low volume sites, the cost is under a US$0.01 per month.
- Unique user counts
- Users’ page paths through your site to identify high engagement areas and exit points
- Internal and external referring pages
- Page load times
- Sites’ uptimes
- You must use a resource group in a non-free Azure subscription.
- You will need to modify your Hugo site’s toml file.
- You probably have to make modifications to your theme.
- This post is specific to Hugo SWAs hosted in Azure and Netlify, but you can modify the process for other types of sites.
- I relied heavily on others’ posts in the References section below. This process works, and I chose some slightly different options for my own needs and preferences.
Create App Insights in Azure and Get Instrumentation Key
Each Azure App Insights instance has its own Instrumentation Key which you pass via Javascript to Azure. Follow these steps.
-
Optional - If you don’t like guids in your resource group names, create a new Log Analytics first which takes about a minute to create. Otherwise, you will end up with something like DefaultWorkspace-343bc83-rest-of-a-guid-xxxx. To do this, navigate to Azure Home > Create a Resource > search on log analytics workspace > Create > Fill out the details as desired. https://portal.azure.com/#create/Microsoft.LogAnalyticsOMS
-
Azure Home > Create a resource > Application Insights to navigate to https://portal.azure.com/#create/Microsoft.AppInsights
-
Create
-
I chose the same resource group as my swa sites
-
Review and Create > Create
-
Copy the Instrumentation Key value for this Application Insights instance
Associate the Instrumentation Key with Hugo site via JavaScript in Theme
If your theme already has a InstrumentationKey
or some variant spelling in its hugo.toml or for older (most as I write this in 2024) themes, config.toml, you may be able to uncomment the line add the Instrumentation Key.
As I write, the Hugo Doit
theme requires adding Instrumentation Key yourself as does the lightbi
theme.
For this post, I made the updates based on the Doit and lightbi themes. If you are using a different theme, adjust your paths accordingly. Note that the “lightbi” Hugo theme does not have a separate directory for head, but Doit does, so again, adjust accordingly.
- In the
[params]
section of hugo.toml or config.toml, add this line using your key
|
|
- Update the \themes\Doit\layouts_default\baseof.html
in the
<head>
section or near it, add
|
|
This is where it ended up in the lightbi theme. To be honest, I forget why I didn’t put it within the <head>
section, but it works in <body>
.
- Create \themes\Doit\layouts\partials\head\appinsights.html or whatever the appropriate path is for your theme with this content.
|
|
- Publish your site and navigate to it.
What is up with the built-in Azure SWA Insights?
My Azure SWAs says App Insights is enabled, but the screen also says, “You don’t have the permissions to update App Insights resource for your app”, and the entire UI is dimmed and unresponsive. I think adding a web function to a SWA can directly enable App Insights on the SWA admin UI, but the above process works.
Stats Reporting
The easy way:
- In Azure, open your new instance of App Insights > Investigate - Performance > Browser
- The bottom-left pane shows your pages sorted by average time to load. Click on COUNT to see the most popular pages at the top.
However, this includes non-end user page hits such as Googlebot crawl and previewing your Hugo site on http://localhost:1313/.
View popular pages by site in a chart
- From your App Insights instance, click Monitoring - Logs > Close Queries pop up > Paste this KQL code
|
|
- Click Run
This filters out unwanted pageviews such as the preview on port 1313 and Googlebot crawls. Clicking Results shows page hits broken out by 1, 7, 14, 30 and 90 days.
In part 2 of this series, I plan to go into more details of viewing usage.