How to use Google Search Console API with R

May 15, 2019 in Google Search Console

Last update: 03.05.2021

At the beginning of the year, I decided to buy a subscription plan on Datacamp because I wanted to learn R – a statistical programming language who can help you to run advanced analysis but also automatize some processes.

I’m still a beginner – but at least, I started to use it for keyword research and also pulling data from APIs.

And today, I want to talk about the Google Search Console API – that you can easily use with R. It’s a very huge topic, that’s why in this post I’ll only show you the basic commands and how to setup the connection with the API.

Learn how to install R & R Studio

1. Open R Studio and install the package

Open to R studio and write the first lines of code. First thing, you have to install two R packages called searchConsoleR .

2. Load the package and enter your credentials

You have to load the package using library(). and launch the OAuth 2.0 process using scr_auth().

3. Authorization code

The code will open automatically your browser and you should see the Google login page. You have to login with your Google Account (the one that owns the GSC account) in order to connect Google Search Console API to R.

At the end of the process you should see a success message. Now you can go back to R Studio and learn the first commands!

4. Basic commands

I highly recommend to read the official documentation of the searchConsoleR package because there you can find all the commands and functions available (but don’t worry! In the next post I’ll show you how to manipulate the data as I did for Data Studio) – for example, below you can see the search_analytics() function – that you can use to query the Search Analytics report.


With list_websites() you can load the list of all the properties connected to your Google Search Console Account.

Write a Comment

Comment

20 Comments

    • Hi Jon,

      I used an R Package, that’s why it only works in RStudio and not Colab (where you have to use Python)

  1. Thank you very much for the article! I did all the steps but in the last one, when rstudio open de cloud browser it did not returs any autorization code… may you help me with this?
    Thanks you a lot!

    • Hi Soraya,

      Do you get any error message? First of all, I would re-install the searchConsoleR package and restart RStudio.

  2. Hi ruben , i wanted to apply filter on query (dimension) containing the name of my brand , could you please help with the syntax

    • Hi Karan, first of all, sorry for the late reply. It’s quite easy and straight forward, I would create the “Brand / Non-Brand” column and apply the filter on that dimension instead of using the query dimension.

  3. Hey Ruben,
    I have some trouble setting up this on the Google Cloud Platform. When it asks me “add credentials to your project” it brings up a different menu and I have to select “app engine admin api” and only then I can select “Oauth” and then it asks me to choose the User Type “internal” or “external” and an “app domain” is that what I should do?

  4. Now it just gives me the error when I run R studio ” Error 401: invalid_client
    The OAuth client was not found. “

  5. Hi there,
    I keep getting this error even on a single day.
    Error in x[[1]] : subscript out of bounds
    Any ideas as to why?