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)
library(GAR)


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.

tokenRefresh(GAR_CLIENT_ID, GAR_CLIENT_SECRET, GAR_REFRESH_TOKEN)


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

df <- gaRequest(
id=c('ga:123456789','ga:987654321'),
dimensions='ga:date,ga:month',
metrics='ga:sessions, ga:users, ga:pageviews',
start='YYYY-MM-DD',
end='YYYY-MM-DD',
sort='-ga:sessions,ga:users'
)

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',
sort='-ga:sessions,ga:pageviews')

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!

6 comments:

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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/

    ReplyDelete
  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

    ReplyDelete