My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages

Overview

This project provides access to Google Analytics data natively from the R Statistical Computing programming language. You can use this library to retrieve an R data.frame with Google Analytics data. Then perform advanced statistical analysis, like time series analysis and regressions.

Supported Features

  • Access to v2 of the Google Analytics Data Export API Data Feed
  • A QueryBuilder class to simplify creating API queries
  • API response is converted directly into R as a data.frame
  • Library returns the aggregates, and confidence intervals of the metrics, dynamically if they exist
  • Auto-pagination to return more than 10,000 rows of information by combining multiple data requests. (Upper Limit 1M rows)
  • Authorization through the ClientLogin routine
  • Access to all the profiles ids for the authorized user
  • Full documentation and unit tests

Get The Library

Get the latest library from the download section or checkout the source

In the R folder, there are 2 files, QueryBuilder.R and RGoogleAnalytics.R. These are the main library files. To load the libraries within your application use:

source("./RGoogleAnalytics.R")
source("./QueryBuilder.R")

where the parameter to source is the location of each file on your machine.

Before You Begin

If you haven't already, download a version of R from the R project home page.

This projects depends on 2 libraries. They can both be downloaded from http://www.omegahat.org/R.

  • RCurl - provides https support from within R. You can install the package from within R using:
  • install.packages("RCurl", repos = "http://www.omegahat.org/R")
  • XML - provides support to parse the XML response from the API. You can install the package from within R using:
  • install.packages("XML", repos = "http://www.omegahat.org/R")

If errors occur when downloading/installing Rcurl or XML packages, ensure the libcurl and libxml libraries are up to date.

On a Linux machine, you can run:

  • sudo apt install libxml2-dev
  • sudo apt install libcurl4-gnutls-dev

Getting Started

Once everything is configured it's a snap to get Google Analytics Data! Here's an Example:

# 1. Create a new Google Analytics API object
ga <- RGoogleAnalytics()

# 2. Authorize the object with your Google Analytics Account Credentials
ga$SetCredentials("INSERT_USER_NAME", "INSERT_PASSWORD")

# 3. Get the list of different profiles, to help build the query
profiles <- ga$GetProfileData()
 
# 4. Build the Data Export API query
query <- QueryBuilder()
query$Init(start.date = "2010-05-01",
                   end.date = "2010-08-20",
                   dimensions = "ga:date",
                   metrics = "ga:visitors",
                   sort = "ga:date",
                   table.id = profiles[1])

#5. Make a request to get the data from the API
ga.data <- ga$GetReportData(query)

#6. Look at the returned data
head(ga.data$data)

Additional Reference Information

Google Analytics provides over 170 dimensions and metrics which you can access through the API. See the:

Powered by Google Project Hosting