Learn R Programming

googleAnalyticsR (version 0.5.0)

ga_auth: Authenticate with Google Analytics OAuth2

Description

A wrapper for gar_auth and gar_auth_service

Usage

ga_auth(token = NULL, new_user = FALSE, no_auto = FALSE)

Arguments

token

An existing token or file location of a token to authenticate with

new_user

If TRUE, reauthenticate via Google login screen

no_auto

Skip auto authentication

Value

Invisibly, the token that has been saved to the session

Multiple accounts

You can authenticate with a new auth file for each account. Supply argument token with the name of the cache file you want to use e.g. ga_auth(token = "one.httr-oauth") for one account, ga_auth(token = "another.httr-oauth") for a different account.

Auto-authentication

You can choose to auto-authenticate by moving your .httr-oauth or by creating a Google OAuth service account JSON file.

Specify an environment variable in R via a .Renviron file or using Sys.setenv which point to the file location of your chosen authentication file. See Startup

Once you have set the environment variable GA_AUTH_FILE to a valid file location, the function will look there for authentication details upon loading the library meaning you will not need to call ga_auth() yourself as you would normally.

An example .Renviron file is below:

GA_AUTH_FILE = "/Users/bob/auth/googleAnalyticsR.httr-oauth"

GA_AUTH_FILE can be either a auth file for a token generated by gar_auth or service account JSON ending with file extension .json

Your own Google Project

Be default the Google Project used is shared by all users, so you may find it runs out of API calls. To mitigate that, create your own Google Project and turn on the Analytics APIs.

You can then create your own client ID and client secret, to place in options or environment arguments (whichever is easiest)

The environment args are below. Similar to auto-authentication, you can place your entries in an .Renviron file

GA_CLIENT_ID="XXXX" GA_CLIENT_SECRET="XXX" GA_WEB_CLIENT_ID="XXX" GA_WEB_CLIENT_SECRET="XXX"

Service accounts

If you use the service account JSON, you will need to add the service account email to your Google Analytics users to see data e.g. xxxx@yyyyyy.iam.gserviceaccount.com

Details

Run this function first time to authenticate with Google in your browser.

After initial authentication, a .httr-oauth will be saved to your working directory, where your authentication details are kept. Keep this file safe.

If you want to reauthenticate, delete this file from your directory or run ga_auth(new_user = TRUE)