A reader counter using PowerShell, Azure functions, and table storage
For the page views or count of readers who visited a specific page, you need to get a hook when the page gets accessed. This is what happens in a WordPress plugin. Plugin code gets executed when the page is accessed and the related count for the page URL in the backend gets incremented. For a static this is bit of a challenge since you don’t have any backend as such. When you have to create a page view counter or a reader counter, you need to do something when a page is accessed and then get something back into the page to display as the reader count. When I started thinking about an implementation for this, Azure functions came to my mind. Azure functions have a HTTPS endpoint that can be triggered which in turn triggers a PowerShell script that does some magic of storing/retrieving the reader counter values for a given URL.
To make this happen, I needed a few Azure resources.
- Azure Storage account with a Table
- An Azure function app (PowerShell)
There is already ton of information around creating and publishing an Azure function and this article won’t repeat that. Also, there is a great article on working with Azure Table storage using PowerShell. I strongly recommend that you go through these articles before you attempt re-creating what is described in this article.
For enabling a reader counter, I created and published a PowerShell function following the steps outlined in the article I already mentioned. Here are some changes I made to the function files before publishing.
For accessing AzTable module cmdlets, you need to create a dependency on the module. This is done by updating the requirements.psd1.
I also modified the function.json to allow POST method only.
Finally, here is what the run.ps1 looks like.
Since this script accesses Azure resource information and updates Azure resources, you need to assign a Managed System Identity (MSI) and assign the appropriate role. To make sure I can update the Azure Table storage, I chose to assign storage account contributor role. There may be a more restrictive role that would work but I did not focus much on figuring out that yet. Maybe I should.
This script expects a query string parameter called Url and that will be the page a reader is visiting. You will have to replace the values of resource group name, storage account name, and table storage name in the above script. My blog uses permalinks of the format /blog/link-to-an-article/. Line 13 in the above example is specific to my blog URLs as I want to store the final leaf object as the RowKey in the Azure Table. Line 16 creates a filter to check if the RowKey already exists in the table storage. If it exists, I retrieve the views from the row, then increment it, and finally update the row again.
If the RowKey does not exist, I add a new row (line 36) and set views to 1. Finally, I package the response as JSON and push it to client. Here is the output from a test run.
This is simple and took me less than an hour to figure this out. The fun part was trying to integrate this into the static site and took me more than half day for the lack of any web development experience. You may skip the next part if you use a different static page generator platform.
This code is self-explanatory (now that I figured out! :)) and it invokes the Azure function URL along with the value of Url (of the article page) passed to this function.
I updated single.html to add the reader count in the post front matter.
Overall, it was fun weekend project to make this work. The application may be very trivial but the learning through this whole process was worth the time I spent on this.Share on: