How to use Google Search Console URL Inspection API with R

February 01, 2022 in R Programming

A couple of days ago, Google announced the release of a new Google Search Console API called URL Inspection API.

The URL Inspection API are a good way to access URL-level data outside of the Google Search Console interface. It provides different information like:

  • date of the last crawl
  • indexing status
  • canonical tag
  • mobile-friendliness
  • structured data

If you want to know more about the Inspection API I recommend you to read the official documentation.

In this post, I’m going to show you how to access the URL Inspection API using R. This tutorial is quite similar to the one I wrote about Google Indexing API in R.

How to generate Google URL Inspection API credentials

The code that I’m going to show you only work if you activate the Google Search Console API and generate a client id and client secret.

Open the Google API Console and search “Google Search Console API” in the APIs library and then you have to enable the API.

Then go to the Credentials section where you’ll be able to generate your credentials. Click on Create Credentials and choose the OAuth client ID option.

Select Desktop App and choose the name you want.

Now, you should have a popup on your screen showing your client id and client secret credentials.

The R code for the URL Inspection API

My R code uses as input a .csv file like the one you see below. I decided to use it because I know that many SEOs are not familiar with R and they prefer to work with Excel / CSV files.

But if you know how R works you can directly use a vector containing all the URLs you want to analyze.

Let’s not forget that the URL Inspection API has a limit of 2,000 queries per day!

Once I ran for the first time the R code, I realized that in the response of the API there are 3 different types of information:

  • indexStatusResult – which contains the information about the last crawl, canonical tag, indexing status, sitemap,…
  • mobileUsabilityResult – which contains the result of the mobile usability test
  • richResultsResult – which returns all the structured data implemented on that url

In my case, I decided that I wanted as output only the first “sub-report” indexStatusResult.

The code below accepts in input a .csv file of URLs, if you want to use it you have to replace the 4 placeholders you find in the code:

  • client id
  • client secret
  • siteURL – the name of your Google Search Console Property e.g. https://www.mydomain.com/
  • languageCode – in which language you want the output

And this is an example of the output you can get from the URL Inspection API with some of the columns.

Write a Comment

Comment

  1. Hi Ruben, you made a useful guide.

    On some URL I’m receiving this error on RStudio:

    Error in (function (…, row.names = NULL, check.rows = FALSE, check.names = TRUE, :
    arguments imply differing number of rows: 1, 3, 2

    Within my investigation, I checked the differences of output of this specific URL on Google Search Console and it seems the error is caused by the presence of more than 1 result (currently 2) in “referringUrls”. All the other URLs with only 1 result in “referringUrls” aren’t affected by this error.

    Could it be a bug in your code?