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.
We’ll use three packages to pull data from Google Analytics.
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
install.packages("gargle") install.packages("googleAuthR") install.packages("googleAnalyticsR")
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.
- Create a project on Google Cloud Platform. I’ll call mine “gormanalysis”.
- Make sure your new project is selected in the Google Cloud Console.
- Search for and choose “Google Analytics” in the console search bar.
- Enable the Google Analytics API.
- Repeat steps 3 & 4, enabling the Google Analytics Reporting API.
- Click on Credentials
- Manage Service Accounts
- Create Service Account. I’ll call mine “gormanalysis-primary”
- Give your service account a name. I’ll call mine “gormanalysis-primary”
- 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!).
- 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 firstname.lastname@example.org. Copy it. Then, in Google Analytics, go to the admin page.
- Give your service account some permissions. For the purposes of this tutorial, read permissions are enough.
# Authenticate mytoken <- gargle::credentials_service_account( scopes = c("https://www.googleapis.com/auth/analytics", "https://www.googleapis.com/auth/analytics.readonly"), path = "/Users/bgorman/Desktop/gormanalysis-6c0c90a25f78.json" ) googleAnalyticsR::ga_auth(mytoken) # Query list of accounts accounts <- googleAnalyticsR::ga_account_list()
accounts ## accountId accountName internalWebPropertyId level websiteUrl ## 1 42145007 Ben519 76988971 STANDARD http://gormanalysis.com ## 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" )
sessions ## date sessions ## 1 2019-03-20 195 ## 2 2019-03-21 197 ## 3 2019-03-22 186