There are several R packages for reaching the Google Analytics API, but the one we’re describing here is googleAnalyticsR, as its the one the author wrote! However you may prefer the syntax of one of the others, which are listed on the googleAnalyticsR homepage.

The example below generates a web_data object that is used in various exercises throughout this site. So, you may want to build a script based on the instructions on this page and save it for future use as you work through other examples and exercises.

Installation

## This installs googleAnalyticsR if you haven't got it already
if(!require(googleAnalyticsR)) install.packages("googleAnalyticsR")
## Loading required package: googleAnalyticsR

Authentication

By default you can authenticate using googleAnalyticsR default project:

library(googleAnalyticsR)
ga_auth()

Pause here, as the first time it should open a web browser the first time to your Google login.

Sign in to give access for your session.

This will create a .httr-oauth file in your working directory folder that contains your authentication details. Keep this file safe as people could access your data if they have access to it.

Getting your View Id

To get your data, you need to specify which Google Analytics View you want to pull it from.

The easiest way to find this is to use google_analytics_account_list() to list your Views, then read the number in the viewId column:

my_accounts <- google_analytics_account_list()
head(my_accounts)
##   accountId        accountName webPropertyId    webPropertyName
## 1  51323256       African Stay UA-51323256-1       African Stay
## 2  27778354 Beggin' YT Channel UA-27778354-1 Beggin' YT Channel
## 3  26328617         Beneful YT UA-26328617-1         Beneful YT
## 4  26559210        Cat Chow YT UA-26559210-1           Cat Chow
## 5  48673516            CBUSWAW UA-48673516-1 http://cbuswaw.com
## 6  48673516            CBUSWAW UA-48673516-1 http://cbuswaw.com
##   internalWebPropertyId    level                                websiteUrl
## 1              83512095 STANDARD              http://www.africanstay.co.za
## 2              53216781 STANDARD        http://www.youtube.com/user/beggin
## 3              50958444 STANDARD                    http://www.beneful.com
## 4              51327860 STANDARD http://www.youtube.com/user/purinacatchow
## 5              80242125 STANDARD                        http://cbuswaw.com
## 6              80242125 STANDARD                        http://cbuswaw.com
##      viewId               viewName type starred
## 1  86518341      All Web Site Data  WEB    <NA>
## 2  54051087      Beggin YT Channel  WEB    <NA>
## 3  51615377             Beneful YT  WEB    <NA>
## 4  52028210               Cat Chow  WEB    <NA>
## 5  82999029            cbuswaw.com  WEB    <NA>
## 6 107951642 unfiltered cbuswaw.com  WEB    <NA>
## The account viewIds (since all columns is a little messy when displayed here)
head(my_accounts$viewId)
## [1] "86518341"  "54051087"  "51615377"  "52028210"  "82999029"  "107951642"

Querying your data

You can then download your data via the v4 reporting API with the google_analytics_4() function. See ?google_analytics_4 and the website for complete details and examples.

## change this to your own ViewId
## this one will only work if you have access to Mark's blog!
my_id <- 81416156 
web_data <- google_analytics_4(my_id, 
                                date_range = c("2016-01-01", "2016-08-01"),
                                metrics = c("sessions","users","pageviews",
                                            "entrances","bounces"),
                                dimensions = c("date","deviceCategory",
                                               "channelGrouping"))
## Downloaded [1000] rows from a total of [1698].
head(web_data)
##         date deviceCategory channelGrouping sessions users pageviews
## 1 2016-01-01        desktop          Direct        8     8         9
## 2 2016-01-01        desktop  Organic Search       13    12        15
## 3 2016-01-01        desktop        Referral        3     3         5
## 4 2016-01-01        desktop          Social        2     2         2
## 5 2016-01-01         mobile  Organic Search        6     6         7
## 6 2016-01-01         tablet  Organic Search        3     2         3
##   entrances bounces
## 1         8       7
## 2        13      11
## 3         3       1
## 4         2       2
## 5         6       5
## 6         3       3