Query Un-sampled Google Analytics Premium Data From R

What is it?

RGoogleAnalyticsPremium is an R package that provides an interface to the Google Analytics API. It enables Google Analytics Premium Users to query and extract Google Analytics un-sampled data within R.

It aims to provide the much needed Google Analytics un-sampled data extraction functionality. It is designed for analysts and data scientists who wish to apply R’s analytics capabilities to their Google Analytics un-sampled data.

Here are a few things RGoogleAnalyticsPremium does well out of the box:

  • Ability to pull un-sampled data from Google Analytics Premium Accounts
  • Ability to export un-sampled Google Analytics data into an csv file
  • Supports authorization via OAuth 2.0

At its core, RGoogleAnalyticsPremium handles the low level API routine (Authorization). Moreover, it ensures that you extract your data without you having to worry about the plumbing. It is therefore well suited for analysts and will give best analysis.

How Package works:
  • This package fires a query from R to google analytics API
  • In response to this POST request, a document containing unsampled data is created in google drive.
  • Next from R, a GET request to google drive API is sent which in response, returns the download details (download URL) for the document
  • Finally a GET request to the download URL is sent which in response downloads data from google documents into the local drive of the user’s system in the form of a file (.csv).

For more details on the functionality of the package, please refer to this link.

How to Install?

To get the current released version from CRAN:


If you are using the RStudio IDE then you can simply select RGoogleAnalyticsPremium from the dropdown in the Install Packages option

Steps to extract unsampled data using RGoogleAnalyticsPremium Package:

Step 1. Install RGoogleAnalyticsPremium package from CRAN and set working directory in R


Step 2. Go to https://console.developers.google.com, create a new project and client id & client secret and enable Analytics API, Drive API and Contacts API for the project. Copy that client id and client secret into R script as shown below.

client.id <- "4610XXXXXXX3.apps.googleusercontent.com"
client.secret <- "O6fGXXXXXXXXXX1-fXXXXXxH"

Check out this blogpost to learn in detail how to create New Project & Client ID


Step 3. Authorize the credentials and obtain token.

token <- Auth(client.id,client.secret)
# Save the token object for future sessions

Step 4. Initialize the query parameters which are to be fired to the API. Here start date, end date, metrics and title parameters are mandatory while dimensions,segment and filter parameters are optional.

query.list <- Init(end.date = "2015-10-01",
metrics = "ga:users,ga:bounces,ga:avgTimeOnSite",
start.date = "2014-01-01",
title = "Behaviour_visits_and_OS",
dimensions = "ga:userType,ga:operatingSystem,ga:landingpagepath",
segment = "dynamic::ga:operatingSystem==Android")

Step 5. Build query and validate the parameters.

ga.query <- QueryBuilder(query.list)

Step 6. Set account id, web property id and profile id. (Google Analytics Premium Account ids to which the query is to be fired)

accountid <- "53XXXX5"
webpropertyid <- "UA-53XXXX5-1"
profileid <- "18XXXXX8”

Step 7. Fire the query to the API which in response returns path to file (.csv) which is saved onto the local drive.

path <- GetFile(ga.query, token, accountid, webpropertyid, profileid)

Step 8. Use read.csv() function on the path returned by GetFile() function in step 6, save the unsampled data in the R object (data frame).

data <- read.csv(path,header = FALSE,sep = ",",comment.char = "#")

If you have any query or need more information about the package, feel free to contact us.

upcoming webinar date
Upcoming Webinar
Achieve 1.5x Higher ROI with Predictive Remarketing Know more
From the Blog

Forecasting the number of visitors on your website using R. Part I

This blog post is the first in the series of three blogs. The current blog will introduce the reader to the importance of forecasting... Read more

Forecasting the number of visitors on your website using R. Part II

This blog is the second post of a series of three blogs. In the Previous Blog I had introduce the reader to the impo... Read more


How Predictive Analytics Can Help You Strengthen Your Re-marketing Strategies

Who should attend

Digital Marketers, Web Analysts, Business Analysts, Anyone who knows Google Analytics and is curious to explore more