Share on:

Google Analytics in R
March 23, 2020

In this tutorial, we’ll anlayze the performance of my website using Google Analytics, R, and googleAnalyticsR.


Before we get started with R, I’m assuming you have some basic familiarity with Google Analytics. For example, if you want to use Google Analytics with R, you’ll obviously need to set up a Google Analytics account and property (i.e. website), and you’ll need to insert your google analytics tracking code into your website.

R Packages

We’ll use three packages to pull data from Google Analytics.

  1. googleAnalyticsR
  2. gargle
  3. googleAuthR

googleAnalyticsR is the workhorse that implements most of the stuff we need. gargle handles authentication to the google cloud platflorm. googleAuthR is only required because its a dependcy of googleAnalyticsR. In a sense, gargle is a new and improved version of googleAuthR.

You can install these packages via


Google Cloud Platform

Buckle up. This is the hard part. If you follow these instructions closely (and google doesn’t change the process anytime soon) you should be okay.

  1. Create a project on Google Cloud Platform. I’ll call mine “gormanalysis”.

  1. Make sure your new project is selected in the Google Cloud Console.

  1. Search for and choose “Google Analytics” in the console search bar.

  1. Enable the Google Analytics API.

  1. Repeat steps 3 & 4, enabling the Google Analytics Reporting API.

  1. Click on Credentials

  1. Manage Service Accounts

  1. Create Service Account. I’ll call mine “gormanalysis-primary”

  1. Give your service account a name. I’ll call mine “gormanalysis-primary”

  1. Click CREATE KEY. This should generate a key in the form of a json file like gormanalysis-6c0c90a25f78.json. Put this key somewhere secure and don’t share it with anyone (e.g. don’t upload it to github!).

  1. From within Google Analytics, you’ll need to give permission to your newly created service account. If you go back to Manage Service Accounts (as in step 7), you’ll see the name of your new service account like Copy it. Then, in Google Analytics, go to the admin page.

  1. Give your service account some permissions. For the purposes of this tutorial, read permissions are enough.

API Quotas

At the time of writing this, Google Analytics allows you 50,000 requests per project per day with a max of 10 requests per second. See this article for more details. You can also monitor the status of your daily quotas from the Analytics API Quotas page in Google Cloud.


# Authenticate
mytoken <- gargle::credentials_service_account(
  scopes = c("", ""),
  path = "/Users/bgorman/Desktop/gormanalysis-6c0c90a25f78.json"

# Query list of accounts
accounts <- googleAnalyticsR::ga_account_list()
##   accountId accountName internalWebPropertyId    level              websiteUrl
## 1  42145007      Ben519              76988971 STANDARD
##   type webPropertyId webPropertyName   viewId          viewName
## 1  WEB  UA-4224507-2    GormAnalysis 97581569 All Web Site Data

It works! Here, we’ve used gargle to generate an auth token that we pass onto googleAnalyticsR. Then we use googleAnalyticsR’s ga_account_list() function to get a summary of the accounts that our authenticated service account can see within google analytics.

Let’s see how we can use the viewId from the accounts table above to see how many daily sessions my website had from 2020-03-20 thru 2020-03-22.

sessions <- googleAnalyticsR::google_analytics(
  viewId = accounts$viewId[1L],
  date_range = c("2019-03-20", "2019-03-22"), 
  metrics = "sessions", 
  dimensions = "date"
##         date sessions
## 1 2019-03-20      195
## 2 2019-03-21      197
## 3 2019-03-22      186

Enjoyed this article? Show your support and buy some GormAnalysis merch.
comments powered by Disqus