Oct 12, 2015

Query your Google Analytics Data with the GAR package

Google Analytics API connection with R
Recently my friend Andrew Geisler released a new version of the GAR package. Like other similar packages, the GAR package is designed to help you retrieve data from Google Analytics using R. But with some new features.

I have been playing a bit with the package and the feature I enjoy the most is the ability to query multiple Google Analytics View IDs in the same query. To do that, you simply need to pass a vector of the View IDs in the correspondent gaRequest() command, and you get back a data frame with each view/profile clearly identified and all their correspondent metrics/dimension you included in the query.
Pretty simple, no?

I think this is a very useful feature which makes the GAR package stand out from other similar packages out there (as far as I know there are currently 4 Google Analytics packages available: RGoogleAnalytics, RGA, ganalytics and GAR of course).

You could also build a loop in R to query multiple View IDs at once, and this is actually what I did previously using the RGoogleAnalytics package. But having this feature included in a package, it just make your life easier!

The GAR package is available on CRAN repository (v1.1 was released on 17 Sep 2015) and you can install it and load with the following commands:

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

Getting the data from Google Analytics

To get data from Google Analytics is easy and similar to other packages.

First of all you need to:

  1. Create a new project in the Google Developers's API Console, if you have not done it before.
  2. Authenticate using your project credentials. 

You can find a detailed explanation for these two steps on the GAR github tutorial here.

So, assuming you got the authentication right and obtained a token, you now need to make sure your token is refreshed (GA access tokens expire) every time you need to retrieve data, and finally execute your query from R.

To refresh the token you use the tokenRefresh() function. The resulting access token will be stored as an environmental variable accessible by the GAR Package.


To get the data, you will use the gaRequest() function.

df <- gaRequest(
metrics='ga:sessions, ga:users, ga:pageviews',

The arguments of this function are based on the structure of the typical API call to Google Analytics. So,  it's here that you will specify all the parameters of your query (metrics, dimensions, period,etc.). And it is here in particular that you specify the Google Analytics View IDs you would like to get the data from.

Of course the gaRequest() function will authenticate using the access token previously stored as an environmental variable.

Let's run an example. In the query below I am asking Google Analytics API to retrieve data about sessions and pageviews between 10 Oct 2015 to 11 Oct 2015, from five distinct View IDs.

df <- gaRequest(
id=c('ga:83424646','ga:77989457','ga:82857332','ga:65743580','ga:65743194'), dimensions='ga:date,ga:month',
metrics='ga:sessions, ga:pageviews',
start='2015-10-10', end='2015-10-11',

As expected, the resulting dataset has a total of 10 rows (5 View IDs x 2 days).

GAR package Query Output

As you can see on the screenshot, in addition to the metrics and dimensions you requested, the resulting data frame contains also details about your request, such as:

  • profile ID (or View ID)
  • accountId
  • webPropertyId
  • internalWebPropertyId
  • profileName (or View name)
  • tableId
  • start-date
  • end-date

Now that you have got your output data frame, you might want to categorize different websites or Views according to specific criteria and apply any aggregate functions (sum, average). It's up to you and to your internal business reporting needs. The key thing is that all the data you requested are included in a single table and ready analyse it with R.

Happy analysis!


  1. مرحلة التعبئة والتغليف: من المستحيل المرور دون الحديث عن هذه المرحلة لأنها تضمن سلامة المنقولات بنسبة تصل إلى حوالي 90 ٪ ، حيث يتم استخدام مواد تغليف متعددة لتغليف قطعة واحدة للحفاظ على سلامة الكسر والتلف بسبب استمرار الاحتكاك بسبب حركة المرور على الطريق.
    شركة نقل عفش

  2. whoah this blog is fantastic i really like reading your articles. Stay up the good work! You realize, lots of persons are looking around for this info, you could aid them greatly. creating a website

  3. Excellent post. I was checking continuously this blog and I'm impressed! Very useful info particularly the last part :) I care for such info a lot. I was looking for this particular information for a long time. Thank you and good luck. 24 hour dental clinic Singapore

  4. I am very much happy to read this blog post. Thank you so much for sharing this post here. This is my first visit to this site. But I loved this site a lot. It is with lots of vital information. Keep up the good work like this. You will definitely get plenty of visitors to your site. Thank you so much for sharing this here. I will definitely come back to this site soon. Have a nice day! Feel free to visit site: Cheap essay writing service

  5. whoah this blog is magnificent i love reading your posts. Keep up the great work! You know, lots of people are looking around for this info, you could help them greatly. https://royalcbd.com/product/cbd-oil-500mg/

  6. In your own home such an awesome way to share paintings as general the other a merchandising system should work so it weren't required to be about elitism and massive money spent. It usually is as well as direct in a tight strategy, Laufer told me that the sculptures o yoga exercise sparring floor will likely used not in the gallery give house or holding out for folks to enjoy in April. Product sales sales and profits continues to go the particular musician from clear wrapping and thus Art o yoga exercise sparring floor challenge, The performers, The actual world wide web hosting wedding venuw..

    It went all night correct it used {Cheap Ray Ban Sunglasses Outlet} to be see-through this has been sarcasm. As well as the application went so on {Coach Outlet Store Online} long afterwards the laugh used {Cheap Jordan Shoes For Men} to be expelled on {MK Outlet} to assist you passing away. At that point the site begun freely mocking {Ray Ban Sunglasses Sale} you along with putting in the precise solution you purchased the experience attempt and do.

    NPR transcripts were created more than a race deadline day caused by- Verb8tm, Corporation, An effective NPR designer, Furthermore created developing a little-known transcribing steps created by working that

  7. Really useful information.Thank you for this information.

  8. There are many Coach Outlet Online in the market.
    The best place to secure that you are really getting an authentic Michael Kors Bags Outlet is from the authorized stores.
    One of the primary concerns when buying a new Michael Kors Purse Outlet is that of authenticity.
    If you are smart and want full value for your money, then there are some ways in which you can identify an original Coach Outlet Store Online.
    Remember, the MK Outlet never comes off, and never tears out.
    Probably the best way to find out if the Coach Bags Clearance is actually.
    Time to go online and get yourself a Coach Bags Outlet if you haven't already.
    Cheap Real Yeezys, a Nike new product launched recent years, is one of them.
    Actually, there are many stores stocking Cheap Yeezys.

  9. Hello! I know how it's hard to make writing papers alone. Nowadays exist cool writing service that can help to solve this problem quickly. It's called essay writing service and on its page you can buy discussion post

  10. Incredible! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same page layout and design. Great choice of colors! sme grant

  11. They’ve cultivated strong interpersonal relationships, which has helped build trust and secure future collaboration.
    best web designer

  12. I'm extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it is rare to see a great blog like this one these days.. Buy Art Online and Art For Sale

  13. Thank you so much for sharing nice post, i really like your content Funny Poetry in Urdu

  14. This blog is the best for me, here I learned a lot of information about Google analytics, how to use it, how to create different sites in Google.
    I'm studying to be a programmer, this article is just for me, I want to write a superior essay on this topic.
    I also want to be useful


  15. Thanks for sharing this.,
    Leanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.

    Brain hacks with NLP

    NLP crash course


  16. Thanks for sharing this.,
    Leanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.

    Brain hacks with NLP

    NLP training

  17. The team provided dedicated service and diligently guided development.
    UX design company

  18. If you suffer from ED or a similar men's health condition, Priority Men’s Medical Center in Atlanta, GA can help you. We understand how important normal sexual function is to you and to your partner.erectile dysfunction compared to impotence

  19. I went over this website and I conceive you've got a large number of splendid information,
    best web design firms

  20. You want know more about Google Analytics then visit Saving Gain!

  21. new form of the GAR bundle. Like other comparative bundles, Need Someone To Write My Essay the GAR bundle is intended to assist you with recovering information from Google Investigation utilizing R.

  22. It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge.
    best design agencies in the world

  23. very good post, i surely really like this excellent website, persist with it affordable local seo services

  24. If you are a man suffering from PE, ED, Low-T, Excess Weight, Fatigue or other common men’s health issues, contact the men's health clinic, Proactive Men’s Medical Center in Cincinnati, Ohio. men's health clinic

  25. It’s my fortune to go to at this blog and realize out my required stuff that is also in the quality.
    top design firm