Install

The latest stable version of googleAnalyticsR is available on CRAN. CRAN

install.package("googleAnalyticsR", dependencies = TRUE)

Development version off GitHub

You may prefer to use the latest development version is on GitHub which you can install via the below.

Github check status: Travis-CI Build Status

remotes::install_github("MarkEdmondson1234/googleAnalyticsR")

Check out the NEWS to see whats currently available in each version.

Dependencies

googleAnalyticsR requires the packages described in the Imports field of the DESCRIPTION file to be installed first, which it will do via install.package("googleAnalyticsR", dependencies = TRUE)

Note that on linux systems, due to its reliance on httr and in turn curl, it may require installation of these dependencies via apt-get or similar: libssl-dev and libcurl4-openssl-dev.

Authentication

Authentication is done via the googleAuthR package, see there for advanced use cases.

For most typical day to day use, the below suits most needs:

## setup
library(googleAnalyticsR)

## This should send you to your browser to authenticate your email.
## Authenticate with an email that has access to the Google Analytics View you want to use.
ga_auth()

## get your accounts
account_list <- ga_account_list()

## pick a profile ID with data to query
ga_id <- account_list[1,'viewId']

Should you need to authenticate under a new user, use ga_auth(new_user=TRUE)

When you authenticate a ga.oauth file will be created in your working directory. If that file is present, then you will not need to go via Google login when you next use ga_auth()

You can specify the name and location of your cache file if you pass that to the ga_auth() function e.g.

ga_auth("auth/my.oauth")

…which will create your authentication file at the location you specify, relative to your working directory.

It is recommended to specify your cache file when authenticating in an RMarkdown document, since that can change the working directory during knitting.

Service accounts

You can alternatively authenticate by downloading a JSON file atached to a Google Cloud service account. More details on how to do that are on the googleAuthR website.

Make sure to add the service account as a user to your Google Analytics account so it has access to your Google Analytics data e.g. your service account email will look something like xxxx@yyyyyy.iam.gserviceaccount.com - add this email to your Google Analytics account in the Web Admin panel to the views you want it to read data from.

Auto-authentication

If you often use the library to access the same accounts from one machine, then it is helpful to setup auto-authentication. This is also the most reliable way for scheduled scripts. It means you do not need to use ga_auth().

A short video on how to do this is available here: googleAnalyticsR - how to do auto-authentication

You choose to auto-authenticate by moving your authentication cache file to a global location on your computer, that all future R sessions will use, then telling googleAnalyticsR where that file is when it is loaded into your R session.

This is done by creating an environment variable called GA_AUTH_FILE that points at your valid cache file.

You can set environment variables using a .Renviron file or via the function Sys.setenv() - see ?Startup for details. The first method is described below:

  1. Choose your authentication cache file - either one you have generated via ga_auth() or or your own Google Project’s service account JSON ending with file extension .json
  2. Create a file called .Renviron and place it in your user’s home directory (~ on OSX/linux or C:\\ on Windows). If using RStudio, you can find your the correct location directory by clicking on the Home breadcrumb link in your File Explorer panel.
  3. Place the absolute location of the file from step 1 into the .Renviron file, e.g. GA_AUTH_FILE = "/Users/mark/dev/auth/ga.oauth".
  4. Save the file and restart your R session.

If all goes well you should see something similar to this when you load googleAnalyticsR:

library(googleAnalyticsR)
#>Successfully authenticated via /Users/mark/dev/auth/ga.oauth

## can do authenticated calls straight away
al <- ga_account_list()

Meta data

To see what dimensions and metrics are available via the API, you can use this command:

## get a list of what metrics and dimensions you can use
meta <- google_analytics_meta()

If offline, this data is also available by typing meta, although it won’t be quite as up to date.

The meta data does not include multi-channel as they are not available, for those see the online version.

When using the library, you can call metrics and dimensions with or without the ga: prefix (mcf: for multi-channel metrics/dimensions.)

Multiple API authentication

If you are using more than one API for authentication (such as Search Console), then authenticate using googleAuthR::gar_auth() instead, to ensure you authenticate with the correct scopes. See the multiple authentication section on the googleAuthR website for details.

Your own Google Project

With the amount of API calls possible with this library via batching and walking, its more likely the default shared Google API project will hit the 50,000 calls per day limit.

To mitigate that, use your own Google Developer Console Project key, so it is not shared across all users of this library.

Donal Phipps has very kindly created a screencast on how to set this up that you can find embedded below and via this link.

Example adding your own Google Developer Console keys

Set these options before any call to ga_auth() or other data fetching calls.

options(googleAuthR.client_id = "uxxxxxxx2fd4kesu6.apps.googleusercontent.com")
options(googleAuthR.client_secret = "3JhLa_GxxxxxCQYLe31c64")
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/analytics")

In the next example, the scopes are also set so a user can authenticate with searchConsoleR at the same time, which is possible as they both use googleAuthR as a backend.

options(googleAuthR.client_id = "tuxxxxxxx2fd4kesu6.apps.googleusercontent.com")
options(googleAuthR.client_secret = "3JhLa_GxxxxxCQYLe31c64")
options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/analytics",
                                        "https://www.googleapis.com/auth/webmasters"))

If you want to use with Shiny, then set the webapp clientId/secrets.

options(googleAnalyticsR.webapp.client_id = "xxxxx9pcab1u005cv.apps.googleusercontent.com")
options(googleAnalyticsR.webapp.client_secret = "zcofxxxxxxElemXN5sf")

Copyright (c) 2018 Sunholo Ltd. Released under MIT license.