Last active
April 5, 2016 16:13
-
-
Save ryanpraski/75730116b6a693ccea0b to your computer and use it in GitHub Desktop.
Download Google Search Console data (formerly webmaster tools) to R using searchConsoleR package. This script downloads the last 90 days of available data: from 93 days ago to 3 days ago and write the data to a .csv file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## A script to download and archive Google search console analytics (formerly webmaster tools) | |
## | |
## searchConsoleR package created by Mark Edmondson (http://markedmondson.me) | |
## | |
## This script downloads and writes data to .csv for the last 90 days of data available | |
## load the required libraries | |
## (Download them with install.packages("googleAuthR") and install.packages("searchConsoleR") if necessary | |
library(googleAuthR) | |
library(searchConsoleR) | |
## Authorize script with Google Developer Console. | |
## Add path to your Google Developer Console Service Account secret.json file | |
service_token <-gar_auth_service("C:/Users/praskry/Documents/r_search_console_secret.json") | |
## data in search console is reliable for 3 days ago so set end date = today - 3 | |
## start date = today- 93 | |
start <- Sys.Date() - 93 | |
end <- Sys.Date() - 3 | |
## set website to your URL including http:// | |
website <- "http://www.ryanpraski.com" | |
## what to download, choose between data, query, page, device, country | |
download_dimensions <- c('date','query') | |
## what type of Google search, choose between 'web', 'video' or 'image' | |
type <- c('web') | |
## other options available, check out ?search_analytics in the R console | |
## this is the query to the search console API | |
## rowLimit = 5000 is the max | |
## use batching set walk_data = TRUE | |
searchquery <- search_analytics(siteURL = website, | |
startDate = start, | |
endDate = end, | |
dimensions = download_dimensions, | |
searchType = type, | |
walk_data = TRUE) | |
## Specify Output filepath | |
filepath <-"C:/Users/praskry/Desktop/rdata/" | |
## filename will be set like searchconsoledata_2016-02-08 (.csv will be added in next step) | |
filename <- paste("searchconsoledata90days", start, sep = "_") | |
## the is the full filepath + filename with .csv | |
output <- paste(filepath, filename, ".csv", sep = "") | |
## this writes the sorted search query report to full filepath and filename row.names=FALSE does not write dataframe row numbers | |
write.csv(searchquery, output, row.names = FALSE) | |
## Complete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment