• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    R
  • License
    Other
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

R interface with Google Search Console API v3, including Search Analytics.

searchConsoleR

CRAN Travis-CI Build Status

R interface with Google Search Console (formally Google Webmaster Tools) API v3.

Setup Guide

Install dependency googleAuthR from CRAN:

install.packages("googleAuthR")
library(googleAuthR)

Install searchConsoleR 0.3.0 from CRAN:

install.packages("searchConsoleR")
library(searchConsoleR)

If you want the development version of searchConsoleR on Github:

devtools::install_github("MarkEdmondson1234/searchConsoleR")
library(searchConsoleR)

Shiny Compatible

Authentication can be done locally or within a Shiny app. See a very bare bones example here: https://mark.shinyapps.io/searchConsoleRDemo/

Info Links

Google Search Console

Search Console v3 API docs

Function Quick Guide

Search analytics

  • search_analytics() - download Google SEO data into an R dataframe.

Website admin

  • list_websites() - list websites in your Google Search Console.
  • add_website() - add a website to your Google Search Console.
  • delete_website() - delete a website from your Google Search Console.

Sitemaps

  • list_sitemaps() - list sitemaps recognised in Google Search Console.
  • add_sitemap() - add sitemap URL location to Google Search Console.
  • delete_sitemap() - remove sitemap URL location in Google Search Console.

Error listings

  • crawl_errors() - list various types of crawl errors googlebot has found.
  • list_crawl_error_samples() - get a list of example URLs with errors.
  • error_sample_url() - show details about an example URL error (for example, links to a 404 URL)
  • fix_sample_url() - mark a URL as fixed.

Authentication functions from googleAuthR

  • scr_auth() - main authentication function. Works locally and within a Shiny environment.

Work flow

Work flow always starts with authenticating with Google:

library(searchConsoleR)
scr_auth()

Your browser window should open up and go through the Google sign in OAuth2 flow. Verify with a user that has Search Console access to the websites you want to work with.

Check out the documentation of any function for a guide on what else can be done.

?searchConsoleR

If you authenticate ok, you should be able to see a list of your websites in the Search Console via:

sc_websites <- list_websites()
sc_websites

We'll need one unique sc_websites$siteUrl for the majority of the other functions.

Most people will find the Search Analytics most useful. All methods from the web interface are available.

Here is an example query, which downloads the top 100 rows of queries per page for the month of July 2015, for United Kingdom desktop web searches:

gbr_desktop_queries <- 
    search_analytics("http://example.com", 
                     "2015-07-01", "2015-07-31", 
                     c("query", "page"), 
                     dimensionFilterExp = c("device==DESKTOP","country==GBR"), 
                     searchType="web", rowLimit = 100)

For a lot more details see:

?search_analytics

Batching

You can get more than the standard 5000 rows via batching. There are two methods available, one via a API call per date, the other using the APIs startRow parameter.

The date method gets more impressions for 0 click rows, the batch method is quicker but gets just rows with clicks.

Specify a rowLimit when batching - if using method byDate this will be the limit it fetches per day, and currently needs to be over 5000 to work. (Issue #17 will fix this).

test0 <- search_analytics("http://www.example.co.uk", 
                          dimensions = c("date","query","page","country"), 
                          rowLimit = 200000, 
                          walk_data = "byBatch")
Batching data via method: byBatch

### test0 has 13063 rows

test <- search_analytics("http://www.example.co.uk", 
                         dimensions = c("date","query","page","country"), 
                         walk_data = "byDate")
Batching data via method: byDate

### test has 419957 rows

> sum(test0$clicks)
[1] 12866
> sum(test$clicks)
[1] 12826
> sum(test$impressions)
[1] 1420217
> sum(test0$impressions)
[1] 441029
> 

Demo script

Here is an example for downloading daily data and exporting to .csv

## A script to download and archive Google search analytics
##
## Demo of searchConsoleR R package.
##
## Version 1 - 10th August 2015
##
## Mark Edmondson (http://markedmondson.me)

library(searchConsoleR)

## change this to the website you want to download data for. Include http
website <- "http://copenhagenish.me"

## data is in search console reliably 3 days ago, so we donwnload from then
## today - 3 days
start <- Sys.Date() - 3
## one days data, but change it as needed
end <- Sys.Date() - 3 

## what to download, choose between date, 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.
scr_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)

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

The dimensionFilterExp parameter

This parameter is used in search_analytics to filter the result.

Filter using this format: filter operator expression

Filter can be one of:

  • country,
  • device
  • page
  • query

Operator can be one of ~~, ==, !~, != where the symbols mean:

  • ~~ : 'contains',
  • == : 'equals',
  • !~ : 'notContains',
  • != : 'notEquals'

Expression formatting:

  • for page or query is free text.
  • for country must be the three letter country code as per the the ISO 3166-1 alpha-3 standard. e.g. USA, GBR = United Kingdom, DNK = Denmark
  • for device must be one of: 'MOBILE', 'DESKTOP' or 'TABLET'

You can have multiple AND filters by putting them in a character vector. The below looks for desktop searches in the United Kingdom, not showing the homepage and not including queries containing 'brandterm'.

c("device==DESKTOP","country==GBR", "page!=/home", "query!~brandterm")

OR filters aren't yet supported in the API.

The dataState parameter

In this parameter could be used for downloading fresh data.

  • final : response will contains just final data
  • all : response will contains also fresh data from lattest days

In default state search_analytics function works with final state. Be careful with the interpretation of fresh data. They get progressively more accurate as they become final data.

Using your own Google API project

As default searchConsoleR uses its own Google API project to grant requests, but if you want to use your own keys:

  1. Set up your project in the Google API Console to use the search console v3 API.

For local use

  1. Click 'Create a new Client ID', and choose "Installed Application".
  2. Note your Client ID and secret.
  3. Modify these options after searchConsoleR has been loaded:
  • options("searchConsoleR.client_id" = "YOUR_CLIENT_ID")
  • options("searchConsoleR.client_secret" = "YOUR_CLIENT_SECRET")

For Shiny use

  1. Click 'Create a new Client ID', and choose "Web Application".
  2. Note your Client ID and secret.
  3. Add the URL of where your Shiny app will run, as well as your local host for testing including a port number. e.g. https://mark.shinyapps.io/searchConsoleRDemo/ and http://127.0.0.1:4624
  4. In your Shiny script modify these options:
  • options("searchConsoleR.webapp.client_id" = "YOUR_CLIENT_ID")
  • options("searchConsoleR.webapp.client_secret" = "YOUR_CLIENT_SECRET")
  1. Run the app locally specifying the port number you used e.g. shiny::runApp(port=4624)
  2. Or deploy to your Shiny Server that deploys to web port (80 or 443).

More Repositories

1

googleAuthR

Google API Client Library for R. Easy authentication and help to build Google API R libraries with OAuth2. Shiny compatible.
R
169
star
2

ga-dashboard-demo

A demo on how to build your own Google Analytics dashboard with R, Shiny and MySQL
R
121
star
3

gentelellaShiny

R
95
star
4

googleCloudRunner

Easy R scripts on Google Cloud Platform via Cloud Run, Cloud Build and Cloud Scheduler
R
81
star
5

serverless-R-API-appengine

Describes how to deploy an R API using Plumber onto App Engine and using Cloud Endpoints
R
77
star
6

langchain-github

Python
65
star
7

Shiny-R-SaaS

Bootstrap a paid R SaaS using Shiny, Firebase and Paddle
R
60
star
8

BigQuery-Visualiser

A Shiny app to visualise BigQuery data in R. Open sourced so you can deploy on your own Shiny server.
R
49
star
9

edmonbrain

A Langchain driven project to create flexible LLM bots on Google Cloud Platform
Python
37
star
10

shinyga

shinyga - Shiny Google Authentication. Quick start user authentication for Google Analytics
R
35
star
11

dartistics.com

Tutorial R website tailored for digital web analysts
HTML
31
star
12

cloudRunR

Running R on Cloud Run
R
31
star
13

ga-bq-stream

Stream JSON data into BigQuery
Python
30
star
14

code-examples

Code for Learning Google Analytics book
Python
26
star
15

autoGoogleAPI

Unofficial Google R packages. These are a collection of Google API R packages auto-generated by googleAuthR v0.5
R
25
star
16

gago

Google Analytics for Go
Go
22
star
17

predictClickOpenCPU

Code for the prediction example from user webpages
HTML
20
star
18

youtubeAnalyticsR

YouTube Analytics API into R
R
17
star
19

googleMeasureR

Send tracking hits to Google Analytics from R code using the Google Analytics Measurement Protocol
R
17
star
20

appengine-shiny

Looking at deploying Shiny apps on Google App Engine with flexible containers
R
17
star
21

googleID

Authentication and identifying Google users using Google+ API
R
14
star
22

verbal_ga_shiny

Talking Google Analytics reports in Shiny
R
14
star
23

google-analytics-cloud-functions

Some cloud functions helpful to Google Analytics
Python
13
star
24

appengine-rstudio

Experiment deploying Rstudio to Google AppEngine
11
star
25

realtimeShiny

R
9
star
26

stripeR

Interface between Stripe API and R
R
8
star
27

r-twitter-api-ggplot2

Demo plotting Twitter API data in R, using library(ggplot2) and library(twitteR)
R
8
star
28

edmonlytica-browser-template

Edmonlytica is a proof of concept for making your own digital analytics stream using GTM Server Side and BigQuery.
Smarty
7
star
29

ga-get-to-post

Call me a GET gif with the correct parameters and you get a POST to your GA account
Python
6
star
30

gtm-redirect-tracker

Track in GTM Server Side using redirects
Smarty
6
star
31

gtmR

R
6
star
32

unsampledGAData

A simple Shiny app to request unsampled GA data
R
6
star
33

docker-shiny-server

Shiny Server for me
6
star
34

gentelellaShiny_nopackage

gentelella bootstrap theme as a Shiny HTML template
HTML
5
star
35

edmonlytica-server-side-client

Edmonlytica is a proof of concept for making your own digital analytics stream using GTM Server Side and BigQuery.
Smarty
4
star
36

measurementProtocol

Use R to send server-side tracking data to Google Analytics 4. https://developers.google.com/analytics/devguides/collection/protocol/ga4
R
4
star
37

antiFakeNews

R
4
star
38

MarkEdmondson1234.github.io

Website
HTML
2
star
39

alphago

Animated visualisations of historical Go matches
R
2
star
40

edmonlytica-server-side-tag

Edmonlytica is a proof of concept for making your own digital analytics stream using GTM Server Side and BigQuery.
Smarty
2
star
41

r-code-commit-github

Example of running R code on a schedule to commit a file to GitHub
2
star
42

timehumps

Examine how sources influence others over time
R
2
star
43

MarkEdmondson1234

Me
2
star
44

anomaly_detection_Rmail_alert

This script pulls time series data from Mixpanel and checks to see whether anomalies are present using Twitters AnomalyDetection package in R
R
2
star
45

entropyMatrixViz

Visualisation of matrix binary grids entropy
R
1
star
46

googleAnalyticsRv4Demo

R
1
star
47

ga_model_pins

ga_models that can be fetched with library(pins)
1
star
48

gdelt_brand_monitoring

Exploring if brand monitoring is doable using GDELT
R
1
star
49

utilities

Helpful functions
R
1
star
50

dotfiles

Its my dotfiles
1
star
51

shiny-real-time-chart

R
1
star