Static site generators are cool to make simple, efficient and easy to host websites, but obviously not able to realize dynamic features. As it cannot be implemented server-side, the only way to implement such feature is to do it browser-side with some JavaScript.

I have already done that for:


One feature I was using on my previous site engine (WordPress) was the top viewed articles. It is obviously not possible with only Jekyll and JavaScript, but if you use some website usage analytics it is possible to get data and add it with some JavaScript to your static website. As website usage analytics solution, I use Piwik Pro, a cloud solution implementing the open source matomo, free for my low-traffic, and respectful of GPDR.

So we will have to write some JavaScript that will:

  • retrieve the latest data from usage statistics
  • create the content to display those data

Setting up a public report

Analytics solutions will offer to access data with an API, which is very cool. But most of the time, it will require an API key/token, and because the script we make will need to be run at browser side, that would be the same as giving the key publicly, and with it, a malicious user will be able to access and manage all your analytics account, so do not do that!!

With Piwik / matomo, you are able to create some public reports. The generated URL will be accessible to all, but only on the data you have chosen to include in this report. It is not quite what we are looking for as it is a web page, but if we dive a little closer in the way this page work, it is built as a web application that will fetch the data as JSON on a URL encoded as parameter of the application. Perfect!

To create the public report in Piwik Pro:

  • create a custom report
  • add dimension Page title and dimension Page URL (and other dimensions if you need extra information of the page)
  • add metric events
  • add filter Event type dimension is “Page view”
  • save and share as public URL

The URL will look like[...]YuanNvbg==/ ; the first part is the URL of the web application, and the obscure last part is the base64 encoded URL of the JSON data and the data will look like:

            "event_title":"M\u00e9thode pour r\u00e9cup\u00e9rer des cartouches d\u2019imprimante s\u00e8ches (super bizarre) |",
            "event_title":"Piloter votre domotique avec Alexa gr\u00e2ce \u00e0 ha-bridge + Domoticz ou Tasmota |",
      "totals":{ "events":1966, ...  },
   "reportConfig": ...
   "siteData": ...

We now have the data we need to be used in our Jekyll site!

The javascript script to create the most viewed list

The script is quite straightforward, and I have created it as include in _includes/piwik-top.html


    let config = {
        reportUrl: "",
        dataUrl: "",
        showSelector: "",
        tableSelector: "",
        itemClassList: "top-post-title",
        itemCount: ""

    if (config.dataUrl == "" && config.reportUrl != "") config.dataUrl = atob(config.reportUrl.split("/").slice(-2)[0])

    if (config.dataUrl) fetch(config.dataUrl).then(async (response) => {
        let json = await response.json()
        if (json && json.reportData && && Array.isArray( {
            // Add elements to table
            document.querySelectorAll(config.tableSelector).forEach(tableElement => {
                tableElement.innerHTML = ""; // Clears current contents
      ,config.itemCount).forEach(d => {
                    let div = document.createElement("div")
                    div.classList = config.itemClassList.split(" ")
                    let a = document.createElement("a")
                    a.href = d.event_url
                    a.title = d.event_title
                    a.innerHTML = d.event_title.split('|')[0] + " <span class='num'>(" + + ")</span>"
            // Show element
            if (config.showSelector) document.querySelectorAll(config.showSelector).forEach(e => = "block")

It will:

  • load the data
  • add in the provided element selected with tableSelector the first entries with itemClassList classes to be styled ; the item will have the post title and be linked to the post
  • and if you want to hide the content before it is loaded, it will display the item selected by showSelector if everything have gone well

You will be able to see the result on the right of my blog page

Going a step further with the top articles cards

If you also want to display those results as cards, the above principle can be extended and use the same technique as the one used to my search page :

  • a Jekyll page will generate a single page with all the possible cards (in my case, the same as the one used for the search feature)
  • a JavaScript script will fetch the top articles data and sort the cards accordingly (and hist he one you don’t need)

It is only feasible on a relatively small site as mine with not many posts. If you need to operate on numerous posts, you can generate a Jekyll card for each page and fetch only the ones you need.

You will be able to see the result on my home page, with the Top viewed option