Real time alerts

Most of analytics solutions won't provide real time features alerts because they are providing you with a lot of noise that you probably already have. But let's imagine that you are interested in. Let's see some ways to implement them.

It all starts with a CRON

A computer can execute some tasks at specific time thanks to what we call CRON. CRON is nothing more than a file in which you are giving the instructions to your computer about what it should do and when. CRON is not linked with analytics, it is linked with anything about telling your computer when it should do it. So the possibilities offered by CRON are endless.

To access your CRON file:

crontab -e

Then you will see a file full of comments and at the end of it you can add your task for example:

* * * * * python3 /home/ulrich/Desktop/rtalert.py

means that every minute execute a Python script with Python 3. Each * represents a period to define. I am not going to enter much into details, search for cron on the internet if you would like more guidance but here we already have enough. We know how we can ask our computer to do something every minute.

Why not every second? Straight answer, because CRON does not allow it. And even if it could it will be very annoying. Every minute is already annoying.

Ok so now, we know how to say every minute do something but what the script will look like?

Designing a script to read Matomo data every minute?

The idea behind our script is to go and fetch a given value every minute and tell us if this value is below or above a certain treshold, if yes then send us an alert, so our script will look like this:

#!/bin/bash
bounce_rate=$(curl "https://demo.matomo.org/index.php?module=API&method=VisitsSummary.get&idSite=62&period=day&date=yesterday&format=JSON&token_auth=anonymous" | jq .bounce_rate | cut -d "%" -f1 | cut -c 2-)
if [ $bounce_rate == 0 ]
then
  eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

  #Code:
  DISPLAY=:0 notify-send "Bounce rate is very low"

fi

here our script is looking at the bounce rate, if the bounce rate equals 0 then it is sending us a notification on our Desktop computer.

Sound alert

Note that it is also possible to do it with sound:

#!/bin/bash
bounce_rate=$(curl "https://demo.matomo.org/index.php?module=API&method=VisitsSummary.get&idSite=62&period=day&date=yesterday&format=JSON&token_auth=anonymous" | jq .bounce_rate | cut -d "%" -f1 | cut -c 2-)
if [ $bounce_rate == 0 ]
then
  #eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
   XDG_RUNTIME_DIR=/run/user/1000 play -q /home/ulrich/Downloads/sound.mp3
  #Code:
  #DISPLAY=:0 notify-send "Bounce rate is very low"

fi

Last modified: Sunday, 17 November 2019, 5:14 PM