Test the website's sustainability with PageSpeed
Explanation:
Import necessary libraries:
requests
for making HTTP requests to the Google PageSpeed Insights APIzapier.platform.app
for defining the Zapier applicationzapier.platform.intent
for defining the trigger intentzapier.platform.user
for handling user authentication (not used in this example)
Define the
GooglePageSpeedInsights
class:Inherits from the
Zap
class to create the Zapier integration.Initializes with the API key from the Zapier app settings.
Raises an error if no API key is provided.
create()
function:Defines the trigger logic for the Zap.
Takes the website URL as input.
Makes an HTTP GET request to the Google PageSpeed Insights API using the provided URL and API key.
Processes the API response to extract the desktop and mobile scores and recommendations.
Returns the extracted data to the Zap.
trigger()
function:Defines the trigger intent for the Zap.
Sets the name of the trigger to "Get PageSpeed Insights Data".
Defines an input field for the website URL.
Sets the
perform
function to thecreate()
function.
resource()
function:Defines the resource for the Zap.
Returns the desktop and mobile scores and recommendations.
Uses helper functions
get_score()
andget_recommendations()
to retrieve the data from thedata
attribute.
get_score()
andget_recommendations()
helper functions:Extract the scores and recommendations from the
data
attribute based on the specified device (desktop or mobile).
if __name__ == '__main__':
block:This block runs the Zapier integration when the script is executed.
To install this integration in Zapier:
Create a new Zapier app.
Add a new trigger and select "Google PageSpeed Insights" from the list of apps.
Configure the app with your Google PageSpeed Insights API key.
Choose the "Get PageSpeed Insights Data" trigger.
Configure the trigger with the website URL you want to analyze.
Add an action to your Zap and select the "Google PageSpeed Insights" app again.
Choose the desired action, such as sending the PageSpeed scores to a Slack channel or Google Sheet.
Configure the action and connect your Zap to the chosen service.
Test your Zap to ensure it works correctly.
Note: Before using this integration, you must obtain a Google PageSpeed Insights API key from the Google Cloud Platform console.
Last updated