How to use Google Search Console API with R

Automation is a big hub in SEO and R is one of the greatest tool to help you doing just that. But first, you need to be able to understand how to collect data from APIs.

In this guide, I will show you how to connect to the Google Search Console API and extract keywords from the API with the “searchConsoleR” package created by Mark Edmondson.

How to connect to the Google Search Console API

If this is your first time using an API, don’t worry, I will try to show you how to use the API and make it as easy as possible.

What is an API?

Simply put, an API, or Application Programming Interface, is a set of defined methods of communication between various software components. Basically, someone creates a bit of code, or software, and give you access to parts of it via an API. In this case, Google gives you access to advanced functionalities of the Search Console via their API.

Step 1: Create a Project in Google API

1. Go to Google’s developers console, and sign-in.

Here, I will call my project google-search-console.

create project in API

2. Go to Dashboard and click “Enable APIs and Services”

3.  Search and click on the “Google Search Console API”

enable search console api

4. Click on “Enable”

enable api

Step 2: Create your credentials

Now, the reporting API being activated, Google tells you that you need credentials.

To use this API, you may need credentials. Click ‘Create credentials’ to get started.

you now need to generate your client.id and your client.secret.

1. To do this, go in the “credential” tab on the left, click on “create credential” and select OAuth Client ID.

auth client id

2. You will need to configure the consent screen. So click on the blue button.

configure consent screen

3. You will now have to set your product name and add your Support email which is associated with your Google Search Console Account. Save your product.

give API product a name

4. Once saved a new screen will appear and you will choose the application type. Select other and name it whatever you want. I will call itrstudio-client. Hit “Create”.

creat oauth client ID

5. You now need to save your client.id and client.secret somewhere safe. We sill use it later.

copy client id and client secret

How to use the Search Console API in RStudio (Step-by-step)

Now that we have set-up our credentials for the Search Console API, I will show you how to use the searchConsoleR package to extract keywords data from Google Search Console.

1. The basics: downloading R and RStudio

For the next step, you will need to download R and RStudio.

If you don’t know what it is or how to use it, I strongly recommend that uou readHands-On Programming With R, especially the part that shows how to install R and RStudio.

R programming isn’t too hard to learn and RStudio makes it easy to work with this language.

2. Get ready: Install packages and load libraries

Now that you understand the basics of R and RStudio, I will show you how to Install packages and load libraries.

What are packages and why use them?

Packages in R are collections of functions and compiled code. Most of the best R functions are not in R when you download it. They can be installed on top of R so you can gain access to their libraries.

Install packages

In this project, we will use the googleAuthR  and the searchConsoleR packages. The first will allow you to authenticate and the second will give you access to the API from R. To use those packages, you must first install them on your computer.  Then you will load them in your current R session.

Copy this bit of code in your R session and run the code.

#install packages
install.packages("googleAuthR")
install.packages("searchConsoleR")
#Load Libraries
library(googleAuthR)
library(searchConsoleR)

3. Authenticate to the searchConsoleR

Now you will need to authenticate to the search console.  Copy this bit of code and add it next to what you already had, then run the code.

#Setting your data
website <- "https://www.example.com" # Enter your website here
nbrow <- 10000 #Number of rows you will collect from Search Console
#Authenticate in the Google Search Console API
options("googleAuthR.scopes.selected" =
c("https://www.googleapis.com/auth/webmasters",
"https://www.googleapis.com/auth/webmasters.readonly"))
#Add your client.id and client.secret here
options("googleAuthR.client_id" = "xxxxxxxx-.apps.googleusercontent.com")
options("googleAuthR.client_secret" = "xxxxxxxx_xxxxxxxxxxxxxx")
googleAuthR::gar_auth()
#Stop and authenticate in your browser

If everything goes right, you should have a pop-up in your browser looking like this:

googleauthr search console api

Great! Now that you are set up, let’s dive-in the best part!

SEO Hack you can do using the Search Console API with R

In this part, I will show you how to export queries per page, get your Job Posting best ranked queries and to extract all your indexed pages from the Search Console API using R Studio SEO Hacks.

SEO Hack #1: Export best queries per page from Google Search Console

To export best queries per page, you will need to copy and paste this bit of code in RStudio.

queries_page <- search_analytics(siteURL="https://www.example.com",
startDate = Sys.Date() - 93, #Start of analysis period (90 days before the end date)
endDate = Sys.Date() - 3 , #data given in search console is from 3 days ago, so we collect data from -3 days
dimensions = c('query', 'page'), #There are 6 dimensions you can dowload: date, searchAppearance, query, page, device, country
#searchType = c('image'), #Optional, default is "web"
rowLimit = nbrow,
#dimensionFilterExp = c("device==DESKTOP","country==GBR"), #optional
walk_data = "byBatch")