How to Create Automated CWV Reports in Python
Core Web Vitals are a set of metrics that measure how users experience a website including loading performance, interactivity and visual stability. Achieving a strong Core Web Vitals score is recommended for success with search and also user experience generally. By consistently tracking a website's CWV, you can track how technical improvements are improving the site over time. However, manually checking and reporting the Core Web Vital reports for multiple sites can be very time consuming. Using this Python CWV Automation specific URLS CWV score every month removes the need for manual checks.
What Are The 3 Core Web Vitals Metrics
Largest Contentful Paint
LCP reports the render time of the largest image, text block, or video visible in the viewport, relative to when the user first navigated to the page. A good LCP score is less than 2.5 seconds.
Interaction to Next Paint
INP is a metric that assesses a page's overall responsiveness to user interactions by measuring the latency of all clicks and interactions that occur throughout the user's visit to a page. Raw INP scores are not available through the PageSpeed Insights API due to them needing real-user interaction data, the API does provide a category score. This score categorises the page as either Slow, Average, or Fast.
Cumulative Layout Shift
CLS calculates the shifting of elements while the page is being downloaded and rendered. A good CLS score is less than 0.1.
How To Use This Script?
The Python script is available in a Google shared Drive where NOVOS will host code for your use. To use this script, you must have an Excel or CSV file with a column labelled "URL." This column should contain the pages you want analysed by the PageSpeed Insights API. When you open the code in Google Colab, you'll see a play button to start the script.
Press the play button to start the script. You will then see a "Choose files" button. Use this to select your Excel or CSV file containing your URL’s.
The code will start running, displaying a progressive count as each URL is analysed. Each URL will be analysed twice; once for mobile and once for desktop. Each URL should take 30-seconds to run. When the code is complete, your results will download automatically.
How It Works
The Python script starts by reading the uploaded CSV or Excel file using the Panda’s module. Next it defines a function to send an API request to the Google Pagespeed Insights API which analyses web performance on Desktop and Mobile. The result of this API request provides multiple metrics including the CWV metrics and Maximum First Input Delay, and Time to First Byte. The python code analyses each URL in the spreadsheet for mobile and desktop. Once the analysis is complete, an excel file will be automatically downloaded.
How To Improve It
There are multiple ways to improve the code included above to match your needs better from further analysis to scheduling:
Setting Up a Cron Email Service
One of the great things about python scripts is they all have the ability to be scheduled using Cron or Windows Scheduler. This will allow you to run automated CWV reports on your site regularly to track page speed as you make technical changes to your site. You can also add an automated email service using a mail server to ensure you are notified when your reports are ready every month.
Concurrency Speed Increase
The one disadvantage of this script is that it takes an estimated 30 seconds per page to be analysed. If you have a Page Speed Insights API key you will be able to send more than 1 API request per second. To speed this up you can use Python's Concurrency module to send more than 1 API request per second. This will severely reduce the runtime of the script.
Extra Analysis
There are multiple ways to further analyse those URLS. Semrush has an API which will allow you to track keywords on those pages. You can also add other competitors' pages to do a comparison between your pages and the competitors.
Ready to improve your website's Core Web Vitals Score? Speak to our eCommerce SEO team to find out more.
References
2. https://web.dev/articles/lcp
3. https://web.dev/articles/inp
4.https://web.dev/articles/cls
5. https://developers.google.com/speed/docs/insights/v5/get-started