The GAR - Google Analytics for R package - is designed to help easily retrieve data from Google Analytics. The following tutorial walks through set up, authentication and requesting data.


Sections:

Note: For obvious reasons, permission/access to at least one Google Analytics Account is necessary.


API Project Set Up:

  1. Log in to Google's API Console (https://code.google.com/apis/console).
  2. From the Projects tab select Create Project and fill out a Project Name. It's not necessary to change the Project ID. Note: the Project ID can not be changed later.
    ~Figure 1~
    IMAGES ~Figure 2~
    IMAGES
  3. Navigate to APIs under the APIs & auth section on the left side nav. Select the Analytics API link and enable the Analytics API.
    ~Figure 3~
    IMAGES
    ~Figure 4~
    IMAGES
  4. Navigate to Credentials under the APIs & auth section on the left side nav and select Create new Client ID.
    ~Figure 5~
    IMAGES
  5. Create the Client ID as an Installed application. Fill out appropriate information on the following 'Consent Screen'.
    ~Figure 6~
    IMAGES
    ~Figure 7~
    IMAGES
  6. Copy Client ID and Client Secret to R Session.
    ~Figure 8~
    IMAGES

Initial Authentication

install.packages('GAR', type=source)
library(GAR)

Getting the Refresh Token:

  1. Store Client ID and Client Secret as variables.
GAR_CLIENT_ID <- 'client id copied from API Console'
GAR_CLIENT_SECRET <- 'client secret copied from API Console'
  1. Pass the Client ID variable to the getCode() function. This will open up a browser with a prompt for authentication.
getCode(GAR_CLIENT_ID)
  1. After accepting, the resulting URL will contain a ?code parameter followed by a long string. Copy this code to the R Session.
    ~Figure 9~
    alt text
  2. Pass the Client ID, Client Secret and code variables to the getRefresh() function and store result in a variable.
GAR_REFRESH_TOKEN <- getRefresh(GAR_CLIENT_ID, GAR_CLIENT_SECRET, code)

Note: Initial authentication and retreiving the refresh token is a one time requirement. Repeating these steps is only necessary if authentication is revoked or if the Client ID, Client Secret or Refresh token is misplaced.


Getting the Access Token:

  1. Pass the Client ID, Client Secret and Refresh Token variables to tokenRefresh(). The resulting access token is stored as environmental variable accessible by the GAR Package.
tokenRefresh(GAR_CLIENT_ID, GAR_CLIENT_SECRET, GAR_REFRESH_TOKEN)
  1. Since access tokens do expire, it is necessary to execute the tokenRefresh() function at the beginning of a session or any time an access token expires.

Getting the Data:

  1. Data is retrieved using the gaRequest() function. The request will authenticate using the access token stored as an environmental variable by default.
df <- gaRequest(
  id=c('ga:123456789','ga:987654321'),
  dimensions='ga:date,ga:month',
  metrics='ga:sessions, ga:users, ga:pageviews',
  start='YYYY-MM-DD',
  end='YYYY-MM-DD',
  sort='-ga:sessions,ga:users'
  )
  1. The arguments used by the gaRequest() function are based on the structure of the typical API call to Google Analytics. See ?gaRequest for more information on which arguments are required and which are optional.

The Result:

In addition to the requested dimensions and metrics, the data frame output of gaRequest() contains information about the request:

This information is useful as the gaRequest() function allows for querying multiple GA accounts at one time.
~Figure 10~
IMAGES


Helpful hints:

Since the Client ID, Client Secret and Refresh Token are needed each session, it is recommended to store these values in your .Rprofile config file as environmental variables named: GAR_CLIENT_ID, GAR_CLIENT_SECRET and GAR_REFRESH_TOKEN. The GAR package is designed to look for and use these variables by default.

Simple! Once initial authentication is made and credentials are saved to your config file it is very easy to refresh the acccess token (refreshToken()) and retrieve data (gaRequest()) on a continual basis.

Enjoy!



andrewgeisler/GAR documentation built on May 10, 2019, 10:31 a.m.