HUGO

Total number of views

Hi, is there any way to show the total number of views of a post or even the entire website within hugo? Could that data be imported from google analytics somehow?

Yes. You have to have the views in a data format, and then use a data format to include it in your site.

But you should take the number directly from the server logs, which will require you to keep that data persistent elsewhere. Google Analytics is blocked all over the place and is becoming less accurate, and if you post those numbers you are lying, and we know you don’t want to lie to your site visitors, ne?

That information itself comes from outside Hugo, but you could either make it available to Hugo at or before deploy time, or, do it dynamically. As @maiki mentioned sources might be your server log, Google Analytics, or, you could use a service like http://www.reliablecounter.com/. I’m not vouching for that, but, you could try it or search for others like it.

If your web server has stats like awstats or webalyzer (which just looks at your server logs, but, presents in a useful way) you might be able to somehow extract that info into some useful format that Hugo can use, i.e. putting it into a data file in json which you parse and display when you build. Similarly, you could use Google Analytic’s API via some script, to pull its data for your site into a json for Hugo parsing.

For example, if you use awstats see:

https://sourceforge.net/p/awstats/discussion/43430/thread/8f45b7b7/

You could write a script to parse those files and re-write the data into something Hugo can access.

See: https://gohugo.io/templates/data-templates/

Server logs are neither an accurate indication of views given the abundance of bots, unfortunately.

You can set up Google Analytics to store data in a Google Calc document (stored in your Google Drive account), and that spreadsheet document can be made available online for people who know the special link. Then with Hugo’s getCSV function you can download and read that file.

I use the same approach to show popular content.

Edit: This tutorial shows how to load (and periodically update) Google Analytics data in Google Calc on your Drive. (The CSV file that gets published by Calc also updates automatically.)

1 Like