Learn R Programming

This repo is the new home for the RGoogleAnalytics library migrated from Google Code SVN

What is it

RGoogleAnalytics is a R Wrapper around the Google Analytics API. It allows fast and easy data extraction in R so that further statistical analysis can be run on the data

Key Features

  • Provides Access to v3 of the Google Analytics Core Reporting API

  • Ability to pull more than 10,000 rows of data in batches via pagination of queries

  • Ability to mitigate the effect of Query Sampling by splitting the date-range of queries and hence extract (nearly) unsampled data

  • Ability to cache data fetched from Google

  • Supports authorization via OAuth 2.0

  • In cases where queries are sampled, the output also returns the percentage of sessions that were used for the query

Installation

To get the current development version from github:

# require(devtools)
devtools::install_github("ManuelDeFrancisco/RGoogleAnalytics")

Dependencies

  • httr handles the underlying OAuth2.0 Authorization flow and the API requests

  • lubridate handles the date manipulation logic underlying Query Partitioning

Background

Work on RGoogleAnalytics was started by Michael Pearmain at Google. He was supported by Nick Mihailowski (Google) and Vignesh Prajapati (Tatvic). Current package in CRAN is maintained by Manuel de Francisco Vera (HotelSpecials)

Tutorials and Use-cases

  • Basic tutorial to get started linking Google Analytics to an API pull
  • Under development

Important Links

  • List of Valid Dimension/Metric Combinations from the Google Analytics API Reference Guide

  • Query Feed Explorer allows you to test your queries for syntatical correctness. Once verified, the query parameters can then be copied to your R Script

  • Demo link on how to use this package to extract data from Google Analytics.

Copy Link

Version

Install

install.packages('RGoogleAnalytics')

Monthly Downloads

22

Version

0.1.6

License

Apache License 2.0

Maintainer

Manuel Francisco Vera

Last Published

October 9th, 2018

Functions in RGoogleAnalytics (0.1.6)

ValidateToken

Check whether the Access Token has expired
Init

Initialize the Google Analytics query parameters
SetColDataType

This will set the appropriate data type to the each column of the provided dataframe.
SplitQueryDaywise

This function breaks up the time range (as specified by Start Date and End Date) into single days and hits a query for each day. The responses for each queries are collated into a single dataframe and returned. This procedure helps decrease the effect of sampling.
ToUri

Returns the URI constructed from the parameter settings. This also URI-encodes all the values in each query parameter.
PaginateQuery

Paginate through pages of Google Analytics Query responses
RefreshToAccessToken

This function takes the Refresh Token as an argument and retrives a new Access Token based on it
SetDataFrame

To prepare the dataframe by applying the column names and column datatypes to the provided data frame.
QueryBuilder

Initialize a QueryBuilder object with the given parameters and perform validation
GetReportData

Query the Google Analytics API for the specified dimensions, metrics and other query parameters
GetProfiles

Retrieve the list of Profiles for the Google Analytics Account
Auth

Authorize the RGoogleAnalytics package to the user's Google Analytics Account using OAuth2.0
GetProfilesFromJSON

This function will do the parsing operation on the JSON reponse returned from the Google Management API and return the dataframe stored with the profile id and profile name
GetDataFeed

This will request with the prepared Query to the Google Analytics Data feed API and returns the data in dataframe R object.
ParseApiErrorMessage

To check whether the returned JSON response is error or not. If it is error then it will
ParseDataFeedJSON

This function will parse the json response and checks if the reponse is contains an error, if found it will promt user with the related error message.
GetAcctDataFeedJSON

This function will make a request to the Google Management API with the query prepared by the QueryBuilder() and ToUri() for retrieving the GA Account data.