Using Google Analytics with R

For the most part, SMB’s tend to utilize free analytics solutions like Google Analytics for their web and digital strategy. A powerful platform in its own right, it can be combined with the R to create custom visualizations, deep dives into data, and statistical inferences. This article will focus on the usage of R and the Google Analytics API. We will go over connecting to the API, querying data and making a quick time series graph of a metric.

To make an API call, you’ll need two things. A Client ID and a Secret ID. You can use this ID over and over again, so you only need to do the following steps once:

  1. Login to your GA analytics account
  2. Go to the Google Developers page:
  3. Create a New Project and enable the Google Analytics API
  4. On the Credentials screen (under the API’s and auth menu), create a new Client ID for Application Type “Installed Application”
  5. Copy the Client ID and Client Secret

In R (I’ll be using RStudio), load the necessary packages:

With the packages loaded, we will run the oauth call to the Google API:

**Note: This part can be a little tricky to understand if you haven’t used R to call to an API before. A new tab should open in your web browser asking if you accept R Analytics to access your GA. Press “Accept”, the page should then move to a message screen that says “Authentication complete. Please close this page and return to R”. When you return to your R IDE, you should see the message in your console saying “Authentication complete.”

Now save the authorization token for future sessions:

Using Google Analytics with R

To make a query of analytics data you’ll need to identify a few things first. Namely, what your start date and end date of the query should be and also what metric(s) you want to pull for.

**Note: Table ID is in the URL of your Google Analyics page. It is everything past the “p” in the URL. Example,

Create the Query Builder object so that the query parameters are validated

Extract the data and store it in a data-frame

You can now make a quick graph of your data. Here we will look at bounces in January:

For further documentation and use cases, refer to this link:

Hoyt Emerson

I am a programmatic data analyst and R programmer with a focus on big data, digital analytics, API's and data viz applications using the Shiny framework. I like to solve problems programmatically and give consultation conceptually. I achieve both with a heavy background in web development, digital marketing and product management. I bring a entrepreneurial perspective to a company's ecosystem.

  • Pingback: Distilled News | Data Analytics & R()

  • Pingback: Using Google Analytics with R – ThinkToStart | Unstable Contextuality Research()

  • Fang Xianfu

    Google have a really nice query builder to help with queries through the Core Reporting API, which this package uses. It’s here:

    This Query Explorer logs in as you, exactly as the package does, then displays the information your account has access to via the API. You can choose different Analytics accounts, properties and views by name, and reference dimensions and metrics by name, and it will return the values you need to enter into Init() to get that information back from the API. Really useful.

    Also, I personally find the code is much neater and less wasteful of namespace with magrittr piping syntax:

    Init(Params) %>% QueryBuilder() %>% GetReportData(oath_token) -> results

    • Hoyt Emerson

      Hi Fang,

      Thanks for the Query Builder link! I also love the Magrittr syntax, and I use it regularly. For those that are comfortable enough with the line by line code above, I highly suggest looking at ways to include pipes into your code to reduce the number of lines and improve readability.

  • Hoyt – thanks for the tutorial…..I am getting a “Error: Refresh token not available”. Any ideas why?

    > ValidateToken(oauth_token)

    Error: Refresh token not available

    > save(oauth_token, file=”oauth_token”)

    > query.list ga.query <- GetReportData(ga.query, oauth_token, split_daywise = T)

    Error: Refresh token not available

    • Hoyt Emerson

      Hi Mark,

      A ‘token not available’ error sounds like the token oauth call didn’t happen correctly and the token wasn’t saved.

      Did you run: oauth_token <- Auth( = "Client ID", client.secret = "Client Secret")

      And when you ran the above code with your own Client ID and Client Secret did you see a confirmation message saying "Authentication Complete"?

  • E-R


    I think now the “installed application” is replaced with “other”.

    Also, I get:
    Error in ParseDataFeedJSON(GA.Data) :
    code : 403 Reason : User does not have sufficient permissions for this profile.

    Do you have any idea what may have gone wrong?

  • chandra sutrisno


    Thank you for this tutorial. Do you have an idea how to get the possible maximum result per each query?

    • Julian Hillebrand

      So you mean how you can get the API limits?

      • chandra sutrisno

        There is max.results parameter right? I want to put the max result that possible I get, so how to get the max number ?

        • Julian Hillebrand

          So there doesn´t seem to be a specific maximum for this. I´ve seen examples with 10.000 or 50.000. I´d recommend you to just try some higher numbers but I´m pretty sure that they all deliver results

  • Nate Sanchez

    Thnks for the tutorial, I´m getting this error just after trying to extract the data and store it in a data-frame <- GetReportData(ga.query, oauth_token, split_daywise = T)
    Setting Max Results to 10000 for efficient Query Utilization
    Access Token is valid
    [ Run 0 of 353 ] Getting data for 2016-12-31
    Error in ParseDataFeedJSON(GA.Data) :
    code : 403 Reason : User does not have sufficient permissions for this profile.

    Any ideas?