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
- Batched reports
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:
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:
# authenticate with your Google Analytics login
# call google analytics v4
ga4 <- google_analytics_4(viewId = 123456,
date_range = c("2016-01-01",
metrics = c(calc1='ga:sessions /
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. ￼
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.
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.
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