PageSpeed Insight Script

Google’s PageSpeed Insights is a free tool checking page speed and provides a score out of 100 for both desktop and mobile. Many features are checked, such as image optimisation, minification of HTML, JavaScript, and CSS, and many other potential problems.

This tool can be very useful in checking if there are any problems with your landing pages, however it can be an automated task with the creation of a script.

To automate checking all your landing pages, we will first need to get a list of all your ad’s final URLs. Because the normal selectors in Google scripts have limits on how many objects they can check, we use the report API. We use the FINAL_URL_REPORT API and the only metric we are interested is the EffectiveFinalUrl.

var report =
“SELECT EffectiveFinalUrl ” +
“WHERE CampaignStatus = ENABLED and AdGroupStatus = ENABLED ” +
{apiVersion: ‘v201705’});

OK so just to explain the various parts in the API call above, in SELECT we put in the attributes and metrics to be pulled into the report. (To find more valid metrics look in the link above.) In this example we are getting the effective final URL which is where you get taken after you click the ad.
In FROM we put the API we are using.
In WHERE we put conditions that we want our search to meet- in this case we are getting all the final URLs from ads that have enabled campaigns and ad groups.
In DURING put the time period you want the data retrieval to be.
And finally we put in the API version we are using. As Google updates its API it will eventually stop supporting the use of older versions, and this field will need updating eventually which may require more changes to the call if they change the names of some metrics.

Now, split the report into rows with:

var rows = report.rows();

Next, now that we have a list of our URLs we want to check, we just need to send them to the PageSpeed Insight API.

Before we can make a call to the PageSpeed Insight API we first need a key. This can be easily got from for free from the Google API Dashboard. After we have this, its just a matter of creating a string containing the API call which is comprised of 4 parts:

  1. The main API call –
  2. The URL you want to check – ?url=EFFECTIVE_FINAL_URL
  3. The device to check speed on – &strategy=mobile or desktop
  4. Finally your API key – &key=yourAPIkey

The string will probably look something like this:

var api =
‘’ +
row[“EffectiveFinalUrl”] + ‘&filter_third_party_resources=’ + true +
‘&strategy=’ + ‘desktop’ + ‘&key=’ + yourAPIkey;

After you run that string through the urlFetchApp that Google scripts has and parse its JSON response:

var response = UrlFetchApp.fetch(api, {muteHttpExceptions: true });

var result = JSON.parse(response.getContentText());

you can get the score from this and output the score with the URL.

After looking at the score you can decide which landing pages need improvements. The PageSpeed Insights website will give you optimised images and minified resources as well as going into more detail in the errors found.

Are you looking for:

PPC Agency
Seo Marketing
Adwords Agency
Google Analytics Agency

Our Qualifications

Our Services

Can we help?

Your Name:*
When is the best time to contact you:*
Phone number:*

* Indicates required fields

How to find us