Sometimes you just want to know how your site is doing.

Getting some metrics without the hassle of Google Analytics is nicely done with a application called GoAccess. This handy app reads your Nginx or other web server log files and pulls the nifty stats into a live web front end or static file.

Personally, I chose to export my web stats as a JSON file that I can grab later with a Python script.  

Here are the basics to get up and running with GoAccess.

Install

sudo apt-get install goaccess

Well that was easy right?  The config file is mega-large, but the defaults will get you going.

Example Usage

Run it in a Terminal to view Nginx logs

$ goaccess -f /var/log/nginx/access.log

You will be prompted to select “Log Format Configuration”:

I used the first one as all the others didn't work.

terminal dashboard

Image taken from the GoAccess website

Output to static HTML file:

$ goaccess -f /var/log/nginx/access.log --log-format=COMBINED -o MyWebStats.html

Output to static JSON file:

$ goaccess -f /var/log/nginx/access.log --log-format=COMBINED -o MyWebStats.json

OK, now that you have your stats in a nifty file you can open the HTML page, or JSON file. As mentioned before, I am using the JSON output in a Python script.  Here is a quick and dirty script to get the "Total Requests" and "Unique Visitors".

Note: Replace $USER and the filename with the directory and file you exported to.

import os
import json

def get_stats():
    file = open('/home/$USER/MyWebStats.json', 'r')
    f = file.read()
    j = json.loads(f)
    run_date = j['general']['date_time']
    unique_visitors = j['general']['unique_visitors']
    total_requests = j['general']['total_requests']
    d = {
        "Total Requests": total_requests,
        "Unique Visitors": unique_visitors, 
        "Run Date": run_date
    }
    print(d)
    file.close()
    return d
    
get_stats()

Here is what the output looks like:

{'Total Requests': 3020, 'Unique Visitors': 367, 'Run Date': '2019-12-07 23:41:19 +0000'}

You will notice that I do not get a lot of hits, so I really don't need something as robust as the other analytical tools.  I just want something to tell me if I am going viral or something.

Have fun.