Daniel López Azaña

Theme

Social Media

Blog

GNU/Linux, Open Source, Cloud Computing, DevOps and more...

Script to automatically monitor website Google PageSpeed Insights score

Logo Google Pagespeed

One of the most targeted goals when optimizing a website for page load speed is to get a good score on Google PageSpeed Insights test. But it is not enough to work hard on optimization, achieve a good score and go to sleep. It is essential to periodically monitor score changes as a website evolves and undergoes changes over time which affect this metric.

Here’s a little script that will allow you to automatically track Google PageSpeed Insight score and be alerted if it falls below a custom value.

Psi tool

The script is based on a small but very useful Javascript tool called psi developed by Addy Osmani, so you need first to install it using the Npm package manager of Node.js:

$ npm install --save psi

Once installed, this program shows on the command line the same information we can get through the official Google web application:

Salida del comando /usr/bin/psi

The desktop-pagespeed-score-watchdog.sh script

This is a small Bash script that filters the information provided by the psi command and dumps the result to a log file that records score changes over time. It can also be processed by a monitoring system which generates some kind of alert when the website’s score falls below a certain value.

#! /bin/bash 
 
scoreLimitDesktop="90"
website="www.daniloaz.com"
error=0 
 
log () 
{ 
        logFileName=`echo $0 | egrep -o "[^\/]+$"` 
        logStatusFile=$logFileName.status 
        logFile=$logFileName.log 
 
        echo "$1|$2" > /var/log/$logStatusFile 
        echo `date +%Y-%m-%d\ %H:%M:%S`"|$1|$2" >> /var/log/$logFile 
} 
 
if [ ! -f /usr/bin/psi ];then 
    echo "Error: /usr/bin/psi command is not installed." 
    exit 1 
fi 
 
scoreDesktop=`/usr/bin/psi --strategy=desktop "$website" | grep 'Speed:' | awk '{print $2}'` 
 
if [ $scoreDesktop -lt $scoreLimitDesktop ];then 
    msg="ERROR: the desktop PageSpeed score of $website fell to ${scoreDesktop}/100."  
    echo $msg 
    log 2 "$msg"
    exit 1 
else 
    msg="OK: the desktop PageSpeed score is equal to or greater than the limit: ${scoreDesktop}/100." 
    echo $msg 
    log 0 "$msg" 
    exit 0 
fi

This example script gets the score for desktop, but could equally get it for mobile by just changing the –strategy=desktop parameter by –strategy=mobile.

Resultado PageSpeed www.daniloaz.comThe script can be executed as often as you want by adding a cron task:

# Monitor every 15 minutes the www.daniloaz.com site score in Google PageSpeed Insights
0,15,30,45      *       *       *       *       /bin/bash /usr/local/scripts/desktop-pagespeed-score-watchdog.sh

Finally, this is the look of the log that periodically records the evolution of PageSpeed score:

$ cat desktop-pagespeed-score-watchdog.sh.log
2017-05-26 12:00:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 12:15:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 12:30:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 90/100. 
2017-05-26 12:45:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 90/100. 
2017-05-26 13:00:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:15:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:45:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:00:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:15:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:45:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:00:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:15:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:45:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100.
2017-05-26 16:00:04|2|ERROR: the desktop PageSpeed score of www.daniloaz.com fell to 89/100.
2017-05-26 16:15:03|2|ERROR: the desktop PageSpeed score of www.daniloaz.com fell to 89/100.
Daniel López Azaña

About the author

Daniel López Azaña

Tech entrepreneur and cloud architect with over 20 years of experience transforming infrastructures and automating processes.

Specialist in AI/LLM integration, Rust and Python development, and AWS & GCP architecture. Restless mind, idea generator, and passionate about technological innovation and AI.

Related articles

facebook-like-button

Speed up your website with a SUPERLIGHT Facebook «Like» button

It is often common to embed a Facebook «Like» button on your website so that on the one hand you show the number of followers of your Facebook page and on the other hand you invite the user to click on it to start following your page. However, this type of buttons tend to overload a website quite a lot because they consist of Javascript code that dynamically generates the button with the updated number of followers and the functionality needed to give a «Like». This means that every time you load a page of your website you have to send 11 extra requests to the Facebook servers to download all the necessary elements. Given that these servers are currently located on the west coast of the United States and are not available through any CDN or similar service, depending on where the user is located, it is likely that each of these requests will have to cross half the globe to complete the download. All this causes your website to slow down unnecessarily and its loading speed is affected, which is quite negative in multiple aspects.

September 23, 2016
Velocidad de carga de páginas web: una importante pieza del puzzle

5 most important reasons to increase your website’s loading speed

It’s common not to pay much attention to website loading speed as long as it remains between more or less acceptable levels wich don’t make us waiting forever for content. However, web speed is becoming an increasingly important factor and optimization efforts in this area can be decisive for success or failure in achieving our goals.Below are the top 5 reasons why you should start worrying about website speed and page loading times.

September 24, 2014
Script to automatically change all gp2 volumes to gp3 with aws-cli

Script to automatically change all gp2 volumes to gp3 with aws-cli

Last December Amazon announced its new EBS gp3 volumes, which offer better performance and a cost saving of 20% compared to those that have been used until now (gp2). Well, after successfully testing these new volumes with multiple clients, I can do nothing but recommend their use, because they are all advantages and in these 2 and a half months that have passed since the announcement I have not noticed any problems or side effects.

February 16, 2021

Comments

Lane September 17, 2018
Nice! Thanks!
Kieran Kiernan May 14, 2019
Pagespeedplus.com monitors your URLs daily and sends an alert when they are below the threshold

Submit comment