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

Script to automatically monitor website Google PageSpeed Insights score

2 comments

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.


Error: Your Requested widget " ai_widget-6" is not in the widget list.
  • [do_widget_area above-nav-left]
    • [do_widget_area above-nav-right]
      • [do_widget_area footer-1]
        • [do_widget id="wpp-4"]
      • [do_widget_area footer-2]
        • [do_widget id="recent-posts-4"]
      • [do_widget_area footer-3]
        • [do_widget id="recent-comments-3"]
      • [do_widget_area footer-4]
        • [do_widget id="archives-4"]
      • [do_widget_area logo-bar]
        • [do_widget id="oxywidgetwpml-3"]
      • [do_widget_area menu-bar]
        • [do_widget id="search-3"]
      • [do_widget_area sidebar]
        • [do_widget id="search-4"]
        • [do_widget id="ai_widget-2"]
        • [do_widget id="categories-5"]
        • [do_widget id="ai_widget-3"]
        • [do_widget id="ai_widget-4"]
        • [do_widget id="ai_widget-5"]
      • [do_widget_area sub-footer-1]
        • [do_widget id="text-4"]
      • [do_widget_area sub-footer-2]
        • [do_widget_area sub-footer-3]
          • [do_widget_area sub-footer-4]
            • [do_widget_area upper-footer-1]
              • [do_widget id="search-2"]
              • [do_widget id="recent-posts-2"]
              • [do_widget id="recent-comments-2"]
              • [do_widget id="archives-2"]
              • [do_widget id="categories-2"]
              • [do_widget id="meta-2"]
            • [do_widget_area upper-footer-2]
              • [do_widget_area upper-footer-3]
                • [do_widget_area upper-footer-4]
                  • [do_widget_area widgets_for_shortcodes]
                    • [do_widget id="search-5"]
                    • [do_widget id="ai_widget-6"]
                  • [do_widget_area wp_inactive_widgets]
                    • [do_widget id="wpp-2"]
                    • [do_widget id="text-1"]
                    • [do_widget id="recent-posts-3"]
                    • [do_widget id="categories-3"]
                    • [do_widget id="archives-3"]
                    • [do_widget id="icl_lang_sel_widget-3"]

                  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.
                   

                  About the author

                  Daniel López Azaña
                  Freelance AWS Cloud Solution Architect & Linux Sysadmin

                  Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, AWS, DevOps, DevSecOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

                  DanielScript to automatically monitor website Google PageSpeed Insights score

                  Related Posts

                  2 comments

                  Join the conversation

                  Leave a Reply

                  Your email address will not be published.