ga_auth: Authenticate with Google Analytics OAuth2

View source: R/auth.R

ga_authR Documentation

Authenticate with Google Analytics OAuth2


A wrapper for gar_auth and gar_auth_service


ga_auth(token = NULL, email = NULL, json_file = NULL)



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


An existing cached email to authenticate with or TRUE to authenticate with only email available. If not set then you will get an interactive prompt asking you to choose which email to authenticate with.


Authentication service key you have downloaded from your Google Project - an alternative to OAuth2 email authentication


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

After initial authentication, your authentication details will be kept globally for use later, tied to your email, and the next time you authenticate you will be given a prompt to choose which email to authentcate from. Set email="" to skip the interactive prompt.


Invisibly, the token that has been saved to the session

Multiple accounts

You can authenticate with a new email for each account. Supply a different email to use those details for your session.

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.


You can choose to auto-authenticate by creating a Google OAuth service account JSON file.

Specify an environment variable in R via a .Renviron file or using Sys.setenv which points 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.json"

GA_AUTH_FILE can be a service account JSON ending with file extension .json. Make sure to give the service account email access to your Google Analytics account as mentioned above.

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.

The best way to do this is to use gar_set_client by downloading your JSON client credentials and setting them to be found on package startup via the GAR_CLIENT_JSON environment argument. See ?googleAuthR::gar_set_client function help pages for details.

Or you can then copy your Google Cloud Project's 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



## Not run: 

# to use default package credentials (for testing)

# to use your own Google Cloud Project credentials
# go to GCP console and download client credentials JSON 
# ideally set this in .Renviron file, not here but just for demonstration
Sys.setenv("GAR_CLIENT_JSON" = "location/of/file.json")
# should now be able to log in via your own GCP project

# reauthentication
# Once you have authenticated, set email to skip the interactive message
ga_auth(email = "")

# or leave unset to bring up menu on which email to auth with
# The googleAnalyticsR package is requesting access to your Google account. 
# Select a pre-authorised account or enter '0' to obtain a new token.
# Press Esc/Ctrl + C to abort.
# you can set authentication for many emails, then switch between them e.g.
ga_auth(email = "")
ga_account_list() # lists one set of accounts
ga_auth(email = "") 
ga_account_list() # lists second set of accounts

# or authenticate via the service key, that has been added to the GA as a user
ga_auth(json_file = "service-key.json")

## End(Not run)

MarkEdmondson1234/googleAnalyticsR_public documentation built on Dec. 10, 2023, 2:43 a.m.