Blog»2012-09-24-r-google-analytics-reproducable-research

2012-09-24-r-google-analytics-reproducable-research

--- title: Using R and Google Analytics tags: analytics,reporting,R,google analytics ---

No one loves client reporting and everyone wants a better way of doing it. What would my perfect client reporting tool look like?

  1. Automatic data export. No more exporting data in one format just to copy and paste it into another.
  2. Flexible. I'm not a huge fan of inflexible report templates. Sometimes I want to show something else, or highlight the performance of a particular campaign without have to rewrite the whole template.
  3. PDF export. .xls(x) is the file format of the analyst. HIPPOs like .pdf (probably so they can print it out).

I've never found anything that fitted all of these criteria but now I'm starting to think I might have found something.

Org-babel is an extension to org-mode which lets you run code as you write documents. I can combine this with the amazing graph drawing capabilities of R and the Google Analytics API to start producing pretty sweet reports without tiresome data munging.

1 Quick Aside: Using R with the Google Analytics API

Matt Clarke over at TechPad has a good guide to getting started with R and the Google Analytics API. One part of the article is out of date (unless Matt has updated the post); the Google Analytics library he links to is out of date after the latest API changes. Instead you can use the one at https://github.com/JerryWho/rgoogleanalytics (works for me anyway). You'll also need an GA API key too.

2 Show me the code!

Putting the following code in an org file will get the visits and pageviews per day for a site and then draw a fancy graph of them:

#+name: visits-pageviews-graph 
#+begin_src R :file /files/2012-09-24-pageviews.png :exports results :results output graphics :width 700 :height 400
  library(rgoogleanalytics)
  library(ggplot2)
  ga <- RGoogleAnalytics()
  ga$SetCredentials("LOGIN_EMAIL", "PASSWORD", "API_KEY")
  query <- QueryBuilder("API_KEY")
  query$Init(start.date=Sys.Date()-30, 
           end.date=Sys.Date()-1, 
           dimensions="ga:date", 
           metrics="ga:visits,ga:pageviews", 
           sort="ga:date", 
           table.id="PROFILE_ID")
  ga.data <- ga$GetReportData(query)
  d<-data.frame(ga.data$data)
  d$ga.date <- as.Date(d$ga.date,"%Y%m%d")
  pageviews <- d$ga.pageviews
  qplot(d$ga.date, d$ga.visits,xlab="Date",ylab="Visits", size=pageviews, color=pageviews)

#+ENDSRC

It is then quite simple to write the insightful commentary that should be the most time consuming part of reporting.

3 Exporting to PDF

The resulting report can then be exported to PDF directly. In order to get a stylish and good looking result at the end you'll have to style the template using LaTeX - this might be a bit much! I haven't got this far yet; I might try my hand at some templates in the future.

4 Alternatives

If you don't want to use org-mode there is an R package called knitr that can do the same thing. I prefer org-mode for the ease of editing text, but knitr integrates more closely with R.

Authored by Richard Fergie