googleAnalyticsR: A new R package for the Analytics Reporting API V4

Hello, I'm Mark Edmondson and I have the honour of being a Google Developer Expert for Google Analytics, a role that looks to help developers get the most out of Google Analytics. My specialities include Google APIs and data programming, which has prompted the creation of googleAnalyticsR, a new R package to interact with the recently released Google Analytics Reporting API V4.

R is increasingly popular with web analysts due to its powerful data processing, statistics and visualisation capabilities. A large part of R’s strength in data analysis comes from its ever increasing range of open source packages. googleAnalyticsR allows you to download your Google Analytics data straight into an R session, which you could then use with other R packages to create insight and action from your data.

As well as v3 API capabilities, googleAnalyticsR also includes features unique to v4:
  •  On the fly calculated metrics 
  • Pivot reports 
  • Histogram data 
  • Multiple and more advanced segments 
  • Multi-date requests 
  • Cohorts 
  • Batched reports 
The library will also take advantage of any new aspects of the V4 API as it develops.

Getting started

To start using googleAnalyticsR, make sure you have the latest versions of R and (optionally) the R IDE, RStudio

Start up RStudio, and install the package via:

install.packages("googleAnalyticsR")

This will install the package on your computer plus any dependencies.

 After successful installation, you can load the library via library(googleAnalyticsR), and read the documentation within R via ?googleAnalyticsR, or on the package website.

An example API call — calculated metrics

Once installed, you can get your Google Analytics data similarly to the example below, which fetches an on-the-fly calculated metric:

library(googleAnalyticsR)

# authenticate with your Google Analytics login
ga_auth()

# call google analytics v4
ga4 <- google_analytics_4(viewId = 123456,
                         dateRange = c("2016-01-01",
                                       "2016-06-01"),
                         metrics = c(calc1='ga:sessions /
                                            ga:users'),
                         dimensions = 'medium')


See more examples on the v4 help page.

Segment Builder RStudio Addin

One of the powerful new features of the v4 API is enhanced segmentation, however they can be complicated to configure. To help with this, an RStudio Addin has been added which gives you a UI within RStudio to configure the segment object. To use, install the library in RStudio then select the segment builder from the Addin menu. 

Create your own Google Analytics 

Dashboards googleAnalyticsR has been built to be compatible with Shiny, a web application framework for R.  It includes functions to make Google Analytics dashboards as easy as possible, along with login functions for your end users. 

Example code for you to create your own Shiny dashboards is on the website.

BigQuery Google Analytics 360 exports 

In addition to the v4 and v3 API functions, BigQuery exports from Google Analytics 360 can also be directly queried, letting you download millions of rows of unsampled data.  

Aimed at analysts familiar with Google Analytics but not SQL, it creates the SQL for you to query common standard metrics and dimensions, using a similar interface as the API calls.  See the BigQuery section on the website for more details.

Anti-sampling 

To more easily fetch non-sampled data, googleAnalyticsR also features an anti-sampling flag which splits the API calls into self-adjusting time windows that are under the session sampling limit.  The approach used is described in more detail here.

Get involved 

If you have any suggestions, bug reports or have any ideas you would like to contribute, then you are very welcome to raise an issue or submit a pull request at the googleAnalyticsR Github repository, or ping me on Twitter at @HoloMarkeD.

Posted by Mark Edmondson, Google Developer Expert