googleAnalyticsR can be used to generate reports in RMarkdown documents.

RMarkdown Authentication

To authenticate in a non-interactive R environment such as rendering RMarkdown, you are required to setup your own Google Cloud Project with clientId/secret. See setting up your own Google Project

Once that is done, you need to create the email cache details at least once outside of the RMarkdown file - this is a global cache for all projects.

You need to avoid the interactive menu though when using RMarkdown - so specify the email you have authenticated with to skip it e.g. ga_auth(email="me@work.com")

Alternatively you can use the JSON service account method - see “server” mode of authentication help for details.

Example


---
title: "Google Analytics Report"
author: "Mark Edmondson"
date: "8 July 2016"
output: html_document
---

```{r}
knitr::opts_chunk$set(echo = TRUE)
library(googleAnalyticsR)

## specify an email you have already authenticated with once outside of Rmd
ga_auth(email="me@work.com")

ga_data 

Alternatively, you can use googleAuthR::gar_auth_service() and use the authentication JSON downloaded from the Google console.



```{r}
knitr::opts_chunk$set(echo = TRUE)
library(googleAuthR)
library(googleAnalyticsR)

## service JSON download from your own GCP project
ga_auth_service("your-creds.json)

# service JSON email needs to be added as a user of the viewId to work
ga_data 

Multi-user authentication

Using googleAuthR is authentication from within RMarkdown using javascript authentication instead of Shiny. A demo of using this with G+ is here: https://mark.shinyapps.io/googleAuthRMarkdown/

Similar code can be adapted to use with Google Analytics data. You will still need to host the RMarkdown document on a Shiny server though, such as http://shinyapps.io

Uploading RMarkdown files to Shinyapps.io/Shiny Server

To see the reports online, you can upload RMarkdown documents to Shiny servers or shinyapps.io.

The recommend method for authenticaation is to use service accounts and googleAuthR::gar_auth_service() since this is easier and more secure in the long run. However, if you want to continue using ga_auth() you need to ensure that the authentication cache file is present when uploaded. This can be specified in the meta data via resource_files

Since the token is not usually saved at a project level since googleAnalyticsR 0.7.0 you will need to move or specify the cache folder for the tokens. The cache folder is usually at the global location ~/.R/gargle/gargle-oauth. Via gargle options, this needs to be changed for a project folder via:

options(gargle_oauth_cache = ".secrets")

This will then create a project folder containing cache. Upload this to the server.


---
title: "Google Analytics Report"
author: "Mark Edmondson"
date: "8 July 2016"
output: html_document
resource_files:
  - .secrets/4324vfdv425v45wvrwer
---

Then when you upload to Shiny Server or shinyapps.io, you should see the file in your upload choices:

See details on gargle vignette

However, the recommend method is to use service accounts and googleAuthR::gar_auth_service() when using a server since this is easier and more secure in the long run. Upload the JSON file instead of the cache folder.