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()
After calling this function first time, a web browser will be opened. 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 GAToken (can be changed with rga.token option) variable is created in the separate TokenEnv 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(profile.id = "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(profile.id = "ga:XXXXXXXX", token = ga_token)
To authorize of the new Google Analytics account use new.auth argument when authorize() function call.
RGA package provides predefined Client ID and Client secret, but you may need to create a new application in Google Developers Console and obtain a Client ID and Client secret to access to the Google Analytics API.
Step by step instructions are as follows:
You can return to the Google Developers Console at any time to view the Client ID and Client secret on the Client ID for native application section on Credentials page.
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, 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. 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.
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")
Then pass this token to other functions:
list_profiles(token = work_token)
list_profiles(token = home_token)
To revoke access the package do the following: