Skip to content

Instantly share code, notes, and snippets.

@MarkEdmondson1234
Last active March 2, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkEdmondson1234/6f55d2aefa2ab35718d7 to your computer and use it in GitHub Desktop.
Save MarkEdmondson1234/6f55d2aefa2ab35718d7 to your computer and use it in GitHub Desktop.
demo on how to download and archive search analytics data using searchConsoleR
## A script to download and archive Google search analytics
##
## Demo of searchConsoleR R package.
##
## Version 1 - 10th August 2015
##
## Mark Edmondson (http://markedmondson.me)
## load the required libraries
## (Download them with install.packages("googleAuthR") and install.packages("searchConsoleR" if necessary
library(googleAuthR)
library(searchConsoleR)
## change this to the website you want to download data for. Include http
website <- "http://example.com"
## data is in search console reliably 3 days ago, so we download from then
## today - 3 days
start <- Sys.Date() - 3
## one days data, but change it as needed
end <- start
## 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
## Authorize script with Search Console.
## First time you will need to login to Google,
## but should auto-refresh after that so can be put in
## Authorize script with an account that has access to website.
gar_auth()
## first time stop here and wait for authorisation
## get the search analytics data
data <- search_analytics(siteURL = website,
startDate = start,
endDate = end,
dimensions = download_dimensions,
searchType = type)
data
## do stuff to the data
## combine with Google Analytics, filter, apply other stats etc.
## write a csv to a nice filename
filename <- paste("search_analytics",
Sys.Date(),
paste(download_dimensions, collapse = "",sep=""),
type,".csv",sep="-")
write.csv(data, filename)
@avavinash
Copy link

Anybody trying the above solution, please do remember to install the package "curl" else us won't be able to authorize with your secret json file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment