Skip to main content

R for analytics beginners: part 2 – authorizing GA access

Adam Englebright29 January 20154 min read
R for analytics beginners: part 2 – authorizing GA access

In Part 1 of this series, I discussed R briefly and explained why it might be useful for people doing analytics work. I also introduced the libraries that allow you to interface R with Google Analytics. At this point, you should have R and RStudio installed on your computer, along with the RGA package. If you have not done this yet, please return to Part 1 and complete these steps first.

Next, we need to authorise the package to access our GA data. You have to go to the Google Developers Console and set up a new project – it doesn’t really matter what it’s called – then select the ‘APIs’ option from the “APIs & auth” menu on the left-hand-site, enable the Analytics API (click on it, turn it “on”), then go to “Credentials” (below “APIs” on the left-hand menu) and click “Create new Client ID”, and select “Installed application”, and click “Create ID”. This is a bit of a bother, and the Developer Console interface can be a bit sluggish and weird, but it’s preferable to all the messing around with temporary tokens that expire after an hour and that kind of nonsense that you have to do with some of the other libraries.

Now you’ve got the client ID and client “secret”, you can authorise… sorry, authorize* the application, so open RStudio, click on the "packages" tab in the bottom-right window if it's not already selected and tick the box next to "RGA". Then, give it access to your GA account by entering the following code into the R console:

authorize(client.id = "your client id", client.secret = "your client secret")

(quotes included). This will open an OAuth page in your browse asking if you want to allow the app to access your Google Analytics data (you do (obviously)).

Now you have access to the API with R, you can begin to pull in data.

get_accounts: This is a pretty basic command – it lists all the accounts to which you have access.

get_accounts(start.index = NULL, max.results = NULL, verbose = getOption ("rga.verbose",FALSE))

You can also get profiles (views), segments and goals, using similar commands:

get_profiles(account.id = "~all", webproperty.id = "~all", start.index = NULL, max.results = NULL, verbose = getOption("rga.verbose", FALSE)) get_segments(start.index = NULL, max.results = NULL, token, verbose = getOption("rga.verbose", FALSE)) get_goals(account.id = "~all", webproperty.id = "~all", profile.id = "~all", start.index = NULL, max.results = NULL, verbose = getOption("rga.verbose", FALSE))

For these, the things you can tweak are pretty similar – account.id, webproperty.id and profile.id for some of them – which just mean that you can get only the results for the accounts, properties and views (profiles) that you’re interested in. Only want the views for, e.g. the Universal Analytics property for an account? Enter the account.id and web property.id in the quotes in place of “all”. Pretty simple.

This isn’t, it should be emphasised, a comprehensive guide, and there are quite a few other things you can do in this vein – pulling lists of filters on views, for instance – but for that I would suggest you more fully investigate the library itself. As for the meat of the RGA library’s utility – getting analytics data, we’ll be looking at that next time.


  • Note to the reader: It’s probably easier to copy and paste a lot of this code, filling in your specific values where necessary. When I was initially experimenting with this stuff, I had the irritating experience of inexplicable non-functionality – and after [far more time than I’m comfortable to admitting to] I realised that my error had been introduced due to my attempting to type out the code (a useful discipline I picked up doing Zed A Shaw’s excellent Python The Hard Way course) but I had neglected the fact that, like all good British things – football, Doctor Who, etc – the spelling of the word ‘authorise’ has been ruined by Americans, – a ‘z’ not an ’s’.

Suggested content

Measurelab awarded Google Cloud Marketing Analytics Specialisation

At the start of the year, if you’d asked us whether Measurelab would be standing shoulder to shoulder with Europe’s biggest consultancies by September, we would've been surprised. Not because we don't believe in ourselves, but because these things feel so distant - until suddenly, they’re not. So, here it is: we’ve been awarded the Marketing Analytics Services Partner Specialisation in Google Cloud Partner Advantage. What’s the big deal? In Google’s own words (with the obligatory Zs): “Spec

Will Hayes11 Sept 2025

BigQuery AI.GENERATE tutorial: turn SQL queries into AI-powered insights

BigQuery just got a major upgrade, you can now plug directly into Vertex AI using the new AI.GENERATE function. Translation: your analytics data and generative AI are now best friends, and they’re hanging out right inside SQL. That opens up a whole world of new analysis options for GA4 data, but it also raises some questions: * How do you actually set it up? * What’s it good for (and when should you avoid it)? * Why would you batch the query? Let’s walk through it step by step. Step 1: H

Katie Kaczmarek3 Sept 2025

How to start forecasting in BigQuery with zero training

If you’d told me five years ago that I’d be forecasting product demand using a model trained on 100 billion time points… without writing a single line of ML code… I probably would’ve asked how many coffees you’d had that day ☕️ But its a brand new world. And it’s possible. Let me explain What is TimesFM? TimesFM is a new foundation model from Google, built specifically for time-series forecasting. Think of it like GPT for time, instead of predicting the next word in a sentence, it predicts t

Katie Kaczmarek14 Jul 2025