knitr::opts_chunk$set(
    comment = "#>",
    collapse = TRUE,
    error = FALSE,
    tidy = FALSE
)

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. 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 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.

Obtain OAuth 2.0 credentials

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:

  1. Creation of a new project (can be skipped if the project is already created):
    • Open the page https://console.developers.google.com/project in your favorite browser;
    • Click on Create Project blue button at the top left of the page;
    • Enter the name of the project into the PROJECT NAME field in the pop-up window;
    • Click Create to confirm the creation of the project.
  2. Enabling access to the Google Analytics API:
    • Select your project from the project list on https://console.developers.google.com/project page;
    • Select APIs & auth and then APIs sub-menu in the left sidebar;
    • Click on the Analytics API link in the Advertising APIs section;
    • Click Enable API for activation Analytics API.
  3. Creating a new application:
    • Select APIs & auth and then Credentials sub-menu in the left sidebar;
    • Click Create new Client ID on the left side of the page;
    • Select Installed application from the APPLICATION TYPE list and Other from INSTALLED APPLICATION TYPE list in the pop-up window.
    • Click on the Create Client ID button to confirm the creation of the application.
  4. Obtaining Client ID and Client secret:
    • Select the project from the project list on the https://console.developers.google.com/project page;
    • Select APIs & auth and then Credentials sub-menu in the left sidebar;
    • Copy the values of the following fields: Client ID and Client secret in the Client ID for native application table.

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.

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.

Cache an access token to disk

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 \pkg{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.

References



jdeboer/RGA-1 documentation built on May 18, 2019, 11:29 p.m.