A wrapper for gar_auth and gar_auth_service
ga_auth(token = NULL, new_user = FALSE, no_auto = FALSE)
An existing token or file location of a token to authenticate with
If TRUE, reauthenticate via Google login screen
Skip auto authentication
Invisibly, the token that has been saved to the session
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.
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
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"
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
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)