Edit
Contact us
Salocin Group Leaders in data and AI-enabled connected customer experiences
Edit Engineers of connected customer experiences
Join the Dots Independent, data-led media thinking for sustainable growth
Wood for Trees Optimisers of future fundraising performance
  • Home
  • Our services
    • Cloud solutions
    • Data science
    • Modern Data Platform
    • Privacy and AI compliance
  • Our partners
    • Apteco
    • Creatio
    • Microsoft
    • Salesforce
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Salocin Group
    • Careers
  • Contact Salocin Group
  • Home
  • Who we are
    • B Corp
    • Careers
  • Our work
  • What we do
    • Intelligent data
    • Marketing technology
    • Transformational CRM
    • Our technology partners
    • Privacy review
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • Contact Edit
  • Home
  • Broadcast media
  • Digital media
  • Print
    • Direct mail
  • Data
    • Our work with Herdify
    • EPiC
  • Media agency
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Join the Dots
    • Careers
  • Contact Join the Dots
  • Home
  • Services
    • Actionable insight
    • Data discovery
    • Data engineering
    • Data hygiene
    • Privacy review
  • Products
    • InsightHub
    • Apteco
    • Microsoft
    • Data management
    • Consent and preference management
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Wood for Trees
    • Operating principles
    • Careers
  • Contact Wood for Trees
Blog

The power of R: Google Analytics and other assorted APIs

By Edit | 18 Jan 2018

If you’re reading this post, you’re probably familiar with data. And if you’re familiar with data, you’ve probably at least heard of R, that great statistical programming tool. We’re going to focus on Google Analytics in this post, but these skills are transferable to other APIs. So how can you integrate the power of R with Google Analytics and other applications accessible with an API?

Being able to interrogate Google Analytics data is great within the interface, and you can do loads of cool stuff in Google Analytics to make the most of the wealth of data. But to combine the analytics data with other data sources to enrich your analysis, we need to pull the data from Analytics, and we can do this with R and its wealth of packages.

The primary package you’ll need to install in R is the imaginatively named RGoogleAnalytics, but other more creatively titled packages are available. To install and run this in your R system, use the following command:

install.packages(“RGoogleAnalytics”, dependencies=TRUE)

library(RGoogleAnalytics)

This should install all the packages you need to retrieve the data from R. You’ll also need to make sure you have a developer’s project active so you have the credentials to access the API. To do this, create a new project from the Google Developer Console and turn on the Analytics API. Then create a new Client ID that is an installed application. You should now have access to your client ID and client secret. Use this client ID and client secret as part of the authentication using the following command:

gaToken <- Auth(client.id, client.secret)

When you first execute this command to generate the token, you will have to sign in to authorise access for the token, but only once if you follow this process.

Saving the token allows the user to repeatedly access the Google Analytics API without having to input their credentials every time, or even having to sign in with the process in the previous step. If you don’t provide a filepath, like here, then the token will be saved directly into your working directory:

save(gaToken, file=”gaToken”)

If you want to access the API with your token in a separate session, use the load function (making sure you include the filepath if you haven’t set your working directory):

load(“filepath/gaToken”)

You also should validate your token, to make sure it is up to date and can access the API. Make sure you have loaded the RGoogleAnalytics package as the validation function is included in this package:

validateToken(gaToken)

Within the validateToken function, use the Robject name that was assigned when the token was created using the Auth function, as this is the object that was saved within the token file that R can recognise.

Now you can access the Google Analytics API, and pull out as much data as Google allows using functions like QueryBuilder and GetReportData. This is one of the more complex processes for accessing an API using R. Other tools use much simpler APIs.

For retrieving keyword rankings from Advanced Web Ranking, all you need to do is submit a GET request and you’ll get your data back. Consult the API documentation for your favourite tools to retrieve data more efficiently, with added reproducibility. Doing t-test’s in Excel requires vigilance to make sure all of the references are correct, unlike a few lines of code in R which we can reference and reproduce later if we need to.

We can easily access APIs and pull a wealth of data, and combine many data sources to gain extremely rich datasets. The power available with R is that we can then analyse the data using any of the statistics and machine learning packages available to us, which we can’t access with other tools.

Summary

  • Access and extract data from the Google Analytics API (and other APIs) using R
  • Combine API access with the vast amount of analysis packages
  • Create a reproducible workflow for extracting and analysing data using R

Click here for more information on how we can help with your insights and analytics, and put our useful tools into practice.

Share this

  • Email
  • WhatsApp
  • LinkedIn
  • Facebook
  • X (Twitter)

More insights

AI isn’t going to take your job (unless you really want it to) 
Blog

AI isn’t going to take your job (unless you really want it to) 

By Edit | 18 Jun 2024
Customer relationship marketing: How generative AI is revolutionising engagement  
Blog

Customer relationship marketing: How generative AI is revolutionising engagement  

By Edit | 4 Apr 2024
Personalisation as a process
Blog

Personalisation as a process

By Edit | 8 Mar 2024
  • Privacy policy
  • Cookie policy
  • Ts&Cs
  • Report a concern

© 2025 Edit, part of Salocin Group Ltd. All rights reserved. Company no.: 0362​4881. VAT no.: 4208​34911.

Salocin Group Certified B Corporation | Cyber Essentials Certified | British Assessment Bureau, ISO 27001 Information Security Management
Salocin Group
Your cookie preferences

We use cookies to ensure this website functions properly, to analyse website traffic and for marketing purposes.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}