Using the Google Analytics API

Using the Google Analytics API with R

  ●   November 4, 2021 | Analytics
Written by
November 4, 2021 | Analytics

Why use the Google Analytics API?

The custom reports functionality in Google Analytics (GA) is useful for gathering specific information and performing bespoke analysis directly in the GA platform. But suppose you want to pull that data out of the platform to process it further? 

The GA API allows you to automatically extract data and integrate it with other business applications without the need for manual data processing. For non-GA360 users with large visitor numbers, the API is vital for reducing data sampling as it allows data to be pulled day by day. The API also allows you to speed up DataStudio dashboards by grabbing the required data, processing it and feeding it into a Google Sheets document which can then be used as the data source. This avoids having to query the GA API directly each time you load the dashboard.

Other GA API uses

  • Storing GA data in CSV format 
  • Automating queries and reporting
  • Feeding GA data into BigQuery or Sheets
  • Merging GA data with other sources (i.e. CRM data)

Brief introduction to R and where to download it

R is a free programming language and software environment that can be easily installed and run on the RStudio Workbench. While you can connect to the GA API in Python and other programming languages, the R package ‘googleAnalyticsR’ is particularly straightforward to use. 

Make sure to install R first and then install RStudio. This concise guide will show you how to do so.

The solution – googleAnalyticsR

In this tutorial, I will show you how the googleAnalyticsR package by Mark Edmondson can be used to access data from the GA API. Assuming that you have R installed on your machine, this should be relatively easy. No experience with R is required – in fact, this package is a really good one to start with if you are new to R. 

By the end of this tutorial, you should be able to run your first query with googleAnalyticsR and store this data in a CSV or Sheets document. We will also touch on using filters and segments in your query.

1. Getting started with googleAnalyticsR

Install the package ‘googleAnalyticsR’ and load up the library:

install.packages(‘googleAnalyticsR’)
library(googleAnalyticsR)

2. Authorize your GA account 

There are a couple of ways to do this – we will stick with the easy ‘Quick Start’ way for now, but there is also the ‘Professional’ way of doing so using your own Google Project. 

Run this line of code and you will be redirected to your web browser, and asked to accept permissions. Make sure you are already logged into your GA account on your browser:

ga_auth()

3. Set up your query

Using the code below as a template, change the variables to fit your conditions:

data <- google_analytics (91023145,
                                  date_range = c(“2021-01-01”,”Yesterday”),
                                  metrics = c("sessions"),
                                  dimensions = c("date", "sourceMedium","campaign"),
                                  anti_sample = TRUE)

4. Add filters to your query

In order to pull the relevant data, you may want to create some filters. The example below shows how to create a filter for organic form submissions. Notice that the filter is stating that both conditions must be satisfied, hence the “AND”. This could be changed to “OR” however, to expand the scope of the report.

formSubmissionEvent <- dim_filter("eventCategory", "EXACT", "FormSubmission")
organicTraffic <- dim_filter("channelGrouping", "EXACT", "Organic Search")

organicFormSubmissionFilter <- filter_clause_ga4(list(formSubmisisonEvent,organicTraffic),”AND” ) 

Now let’s put this into our query to count the sessions where a user comes through organic search and submits a form:

data <- google_analytics (set_view_ID,
                          date_range = c(“2021-01-01”,”Yesterday”),
                          metrics = c("sessions"),
                          dimensions = c("date", "sourceMedium","campaign"),
                          dim_filters = organicFormSubmissionFilter, 
                          anti_sample = TRUE)

5. Add segments to your query

You can create segments within googleAnalyticsR, but this is a bit difficult. We recommend creating your segments within the GA platform and reading them into your R environment instead. Run the following code to bring up all your existing segments.

View(ga_segment_list())

Now find the desired segment and use the segmentID and name to create a segment variable.

exampleSeg <- segment_ga4("Semetrical Example", "gaid::HfpJEyqIRlqq7OL1a4-6rg")

Now, let’s add this segment to the query:

data <- google_analytics (set_view_ID,
                          date_range = c(“2021-01-01”,”Yesterday”),
                          metrics = c("sessions"),
                          dimensions = c("date", "sourceMedium","campaign"),
                          dim_filters = organicFormSubmissionFilter, 
                          segments = exampleSeg,
                          anti_sample = TRUE)

6. Store your data in CSV or Sheets

We recommend storing your data in a CSV where possible, even if you also plan on sending it into Sheets. Simply adapt the code below to create a document in the relevant file.

write.csv(data, "C:\\Users\\Semetrical\\Google Analytics\\OrganicFormSubmission.csv")

Sending data to Sheets requires the googlesheets4 library and authentication. Create a new sheet and copy/paste the code from your URL into the “ss” part of the below code.

library(googlesheets4)
gs4_auth()
sheet_write(data, ss = "17lkbyrb4Pask8j3KE4bMnmfJPj6jaeePFiDBDZmw2OE", sheet = "Google Analytics - OrganicFormSubmission")

You should now be able to:

  • Query data from GA in R
  • Create filters and read in segments to add to your query
  • Store your data in a CSV file or send it to a Google Sheets document

The below code shows the full example of this process:

library(googleAnalyticsR)
library(googlesheets4)
ga_auth()
gs4_auth()

formSubmissionEvent <- dim_filter("eventCategory", "EXACT", "FormSubmission")
organicTraffic <- dim_filter("channelGrouping", "EXACT", "Organic Search")

organicFormSubmissionFilter <- filter_clause_ga4(list(formSubmissionEvent,organicTraffic),”AND” )  

exampleSeg <- segment_ga4("Semetrical Example", "gaid::HfpJEyqIRlqq7OL1a4-6rg")

data <- google_analytics (set_view_ID,
                          date_range = c(“2021-01-01”,”Yesterday”),
                          metrics = c("sessions"),
                          dimensions = c("date", "sourceMedium","campaign"),
                          dim_filters = organicFormSubmissionFilter, 
                          segments = exampleSeg,
                          anti_sample = TRUE)


write.csv(data, "C:\\Users\\Semetrical\\Google Analytics\\OrganicFormSubmission.csv")

sheet_write(data, ss = "17lkbyrb4Pask8j3KE4bMnmfJPj6jaeePFiDBDZmw2OE", sheet = "Google Analytics - OrganicFormSubmission")
Summary

Conclusions

Although Google Analytics offers a user-friendly interface, the platform has many limitations for more experienced users with advanced data analytics needs; most of whom will already be looking for a way to access the data outside of the platform. The GA API allows you to be more flexible with your web analytics by facilitating the automation of data processing, hugely improving the efficiency of such processes.

The googleAnalyticsR package is an easy way to take advantage of the GA API and offers functions to send your data into a CSV or Google Sheets document. For those with minimal or no R experience, this package is a great place to start your programming adventure.

Our Blog