Before you send any requests to the Google Analytics API, it’s necessary to perform authorization and to obtain access token. It can be done with the following command:

authorize()

(Please note that you can immediately revoke the authorization in Google Account Management page Apps connected to your account, so that no need to worry about the security of your Google account. For more details please go to the section below Revoke access application)

After calling this function first time, a web browser will be opened. If username argument was not specified you need choose an account for the authorization. First entrance with a Google Account confirms access to the Google Analytics data. Note that the package requests access for the read-only data.

When the authorize() function is used, the Token variable is created in the separate .RGAEnv environment which not visible for user. So, there is no need to pass every time the token argument to any function which requires authorization.

list_profiles()
ga_data <- get_ga(profileId = "ga:XXXXXXXX")

Access token can also be stored in a variable and passed as the argument to the functions which request the API Google Analytics. It can be useful when you are working with several accounts at the same time (for details see below).

ga_token <- authorize(client.id = "client_id", client.secret = "client_secret")
ga_data <- get_ga(profileId = "ga:XXXXXXXX", token = ga_token)

To authorize of the new Google Analytics account use new.auth argument when authorize() function call.

## Obtain OAuth 2.0 credentials

RGA package provides predefined Client ID and Client secret, but we recomend to obtain your own a Client ID and Client secret to access to the Google Analytics API.

To find your project’s client ID and client secret, do the following:

1. Open the Credentials page.
2. If you haven’t done so already, create your project’s OAuth 2.0 credentials by clicking Add credentials > OAuth 2.0 client ID and select Other application type.
3. Look for the Client ID in the OAuth 2.0 client IDs section. You can click the application name for details.

To enable Analytics API for your project, do the following:

1. Open the Analytics API Overview page.
2. CLick on the Enable API button to activate Analytics API.

## Use environment variables

Besides the explicit specifying of the client.id and client.secret arguments, their values can be defined via environment variables: RGA_CLIENT_ID and RGA_CLIENT_SECRET. In this case, specifying of the client.id and client.secret arguments is not required for calling authorize() function:

authorize()

Note: do not put the Client secret on a publicly visible location.

Setting the environment variables is different for various operating systems. The user should refer to the relevant reference materials (view the list of references at the end of this manual). Also there is a setup method of the environment variables when running R sessions using the .Renviron files in the working or user’s home directory. Contents of the file might look like this:

RGA_CLIENT_ID="client_id"
RGA_CLIENT_SECRET="client_secret"

Environment variables can also be set directly from R session using the Sys.setenv() function. For instance:

Sys.setenv(RGA_CLIENT_ID = "client_id", RGA_CLIENT_SECRET = "client_secret")

This string can be added to the file .Rprofile in the user’s current оr home directory in order to set these variables automatically when the R session starts.

When the cache argument and the rga.cache option is not changed and username param is not specified, then after successful authorization the .ga-token.rds file with access token to Google API will be created in the current working directory. The .ga-token.rds file is used between sessions, i.e. at a subsequent call to the authorize() function and authorization in the browser tab is not required. If username argument specified token will be cached in the .username-token.rds file.

Using the cache argument you can also suppress caching (FALSE value) or specify an alternate path to the file storage (in this case It is necessary to specify the path and file name explicitly).

authorize(client.id, client.secret, cache = "/path/to/file")

Also you can specify a cache path to change rga.cache option. After this you no need specify cache argument for calling authorize() function:

options(rga.cache = "/path/to/file")
authorize(client.id, client.secret)

To disable caching set the rga.cache option or cache argument to FALSE.

## Working with multiple tokens

If you want to work with RGA package with multiple Client IDs and Client secrets from different accounts (for example from business account at work and personal account at home) you need to clearly distinguish them. The best way in this case is creating two different tokens with disabled cache option:

work_token <- authorize(client.id1, client.secret1, cache = FALSE)
home_token <- authorize(client.id2, client.secret2, cache = FALSE)

or define a specific cache path for each token:

work_token <- authorize(client.id1, client.secret1, cache = "work.token")
home_token <- authorize(client.id2, client.secret2, cache = "home.token")

Also you can specify username argument:

work_token <- authorize(username = "home@domain.com")
home_token <- authorize(username = "work2@domain.com")

Tokens will be stored to the separated files.

Then pass this token to other functions:

list_profiles(token = work_token)
list_profiles(token = home_token)

## Revoke access application

To revoke access the RGA package do the following:

1. Go to the Apps connected to your account page;
2. Find RGA package entry. Then click on it;
3. Click on the Revoke access button in the sidebar on the right.