How to use Google Search Console API with R

This post is part of the Guide on Google Search Console API

Automation is a big hub in SEO and R is one of the greatest tools to help you do 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.

This guide is part of the complete tutorial on the Google Search Console API.


Subscribe to my Newsletter


How to Get Google Search Console API Keys

You will need an API key to be able to connect to the Google Search Console API.

The API key is like your username and password to access the API.

Follow this guide to help you get your Google Search Console API key.

Once you have created your Google developer account and have downloaded your API key, you will be ready to connect to the Search Console API using R.

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 you read Hands-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.

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") 

Other APIs You Might Be Interested In

Looking for the next steps to improve your skills using APIs?

4/5 - (4 votes)