Access to Google Analytics Reporting API with R

Unleash powerful insights with R by connecting to Google Analytics and automating data extraction for web traffic analysis.

Ceyhun Enki Aksan
Ceyhun Enki Aksan Entrepreneur, Maker

Accessing WordPress REST API with R was the previous article. In this article, we will proceed to access the Google Analytics Reporting API using R. This way, we can create tables and adjust charts based on page and user metrics and dimensions.

For instance, in my previous article, we retrieved data about WordPress posts. In this article, we can also access information such as page views, bounce rate, and session count between specific dates as described in the basic Google Analytics formulas. In the next article, we can combine these two tables to generate charts.

Google Analytics Reporting API

The Google Analytics Reporting API can be described as the most advanced programmatic method for accessing Google Analytics report data. Using this API, we can create customized control panels (dashboards), automate complex reporting tasks, and link Google Analytics data with other applications.

Google Analytics
Google Analytics

The Google Analytics Reporting API (features may vary) provides the following capabilities1.

Metric Expressions

In addition to built-in metrics, the API also provides access to metric combinations expressed in mathematical operations. For example, we can obtain the number of goals completed per session (e.g., for goal1) using the expression ga:goal1completions / ga:sessions.

Google Analytics reporting interface allows us to retrieve data for only a single date range. However, via the API, we can access data from two date ranges with a single request.

Cohorts and Lifetime Value

The API offers a rich request scope for cohort and lifetime value reports.

Multiple Segment Usage

Using the API, we can process multiple segments in a single request. For a list and definitions of available segments, please refer to the Segments: list page2.

Google Analytics Reporting API
Google Analytics Reporting API

Accessing Google Analytics Reporting API via R

For Reporting API operations3, I’ll be using the googleAnalyticsR package4. However, prior to these operations, we must enable access to the Google Analytics Reporting API through the Google API Console, and we must define Client Information and OAuth Client IDs to authorize access5. We can establish access from R using the Client ID and Client Secret. Alternatively, we can configure these credentials through a json file without exposing them to any potential writing or definition errors. To do this, we simply download the JSON file via the Download JSON file link on the Client ID page, and then specify the file path within the gar_set_client() function. I will also address this topic in detail during the application phase.

For more information regarding the setup process, please refer to the Enable the API section under the relevant documentation6.

Let’s now look at an example of our application.

googleAuthR::gar_set_client("/<client-secret>.apps.googleusercontent.com.json")
library("googleAnalyticsR")
gar_auth(email = "<email>")

The JSON file named /<client-secret>.apps.googleusercontent.com.json mentioned above contains the credentials that I specified in the previous paragraph. Its content will look approximately as follows:

{"installed":{"client_id":"123456789012-a34bcdefghijklm5noor6stuw7qzab7c.apps.googleusercontent.com","project_id":"personal-123456","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"1aBcDEFgHiJklM_N2OpPrsT3","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

Providing the information in this format also reduces the likelihood of encountering the commonly seen 403 error7. The gar_auth(email = "") definition ensures we do not have to repeatedly define access requests. The email address specified here must be the one you used to enable the API via the Console.

So far, our actions have focused on establishing access. With accurate information and an active API, we’ve taken our first step. Now we proceed to select the user, property, and view settings for which we wish to access Google Analytics data.

First, let’s list our Google Analytics accounts.

googleAnalyticsR::ga_accounts()

Using this function, we can view details such as the property ID (webPropertyId), account name (name), creation date (created), last update date (updated), and our effective permissions (permissions.effective) for the defined accounts via the API.

         id                 name             created             updated                          permissions.effective
1   1234567         Account Name 2006-12-11 11:07:15 2015-09-23 22:12:07 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
2  24681012         Demo Account 2014-09-06 17:53:33 2019-06-10 17:56:03                               READ_AND_ANALYZE
3  35791113        Business Name 2015-02-16 14:25:34 2018-05-01 09:17:56 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE
8 135791113          Client Name 2017-12-08 14:24:09 2017-12-08 16:08:09              COLLABORATE,EDIT,READ_AND_ANALYZE

Let’s proceed with the account having ID 35791113 and list the other users associated with this account along with their permissions.

googleAnalyticsR::ga_users_list(35791113)

Now, after executing the above function, we can access metrics and dimensions through the values under entity.profileRef.id or, alternatively, the Table ID (ids)8. Additionally, you can retrieve these details via the Account Explorer9. Thus, we can proceed with our first request.

ga_id <- "ga:<entity.profileRef.id | Table ID (ids)>"

gaUserData <- google_analytics(ga_id,
  date_range = c(start_date, end_date), # date range (year-month-day)
  metrics = c("ga:sessions", "ga:users", "ga:pageviews"), # metrics
  dimensions = c("ga:sourceMedium", "date", "ga:userType"), # dimensions
  segments = segment_ga4(name = "Organic Traffic", segment_id = "gaid::-5"), # segments (optional)
  max = -1) # maximum number of returned records

The data we obtain, based on the provided metrics, dimensions, and segments, will look as follows.

         sourceMedium       date          userType sessions users pageviews
1   (direct) / (none) 2018-01-02       New Visitor        3     3         3
2   (direct) / (none) 2018-01-03       New Visitor        2     2         2
3   (direct) / (none) 2018-01-04       New Visitor        4     4         5
4   (direct) / (none) 2018-01-05       New Visitor        2     2         2
5   (direct) / (none) 2018-01-06       New Visitor        6     6         6
6   (direct) / (none) 2018-01-06 Returning Visitor        1     1         1
7   (direct) / (none) 2018-01-07       New Visitor        4     4         4
8   (direct) / (none) 2018-01-08       New Visitor        3     3         3
9   (direct) / (none) 2018-01-08 Returning Visitor        1     1         1
 [ reached 'max' / getOption("max.print") -- omitted 8734 rows ]

When inspecting the variable classes using the str function, the sourceMedium and userType fields will appear as chr. In reality, since these fields are categorical, it would be more appropriate to treat them as factor variables.

gaUserData$sourceMedium <- factor(gaUserData$sourceMedium)
gaUserData$userType <- factor(gaUserData$userType)

Yes, by modifying metrics and dimensions, you can access numerous user and page views across Audience, Acquisition, Behavior, Conversions, and Reports, and perform operations (visualizations, analysis, calculations, etc.) using these data. You can use the Dimensions & Metrics Explorer10 and Request Composer11 pages to experiment with different metrics, dimensions, and requests.

Advanced Learning

Footnotes

  1. Overview. Reporting API v4. Google Analytics
  2. Management API. Google Analytics
  3. Analytics Reporting API. Google Analytics
  4. googleAnalyticsR: Google Analytics API into R. CRAN
  5. Google API Console
  6. Enable the API. Reporting API v4. Google Analytics
  7. Error Responses. Reporting API v4. Google Analytics
  8. ga: Lists all columns for a Google Analytics core report type. rdrr.io
  9. Account Explorer. Google Analytics Demos & Tools
  10. Dimensions & Metrics Explorer. Google Analytics Demos & Tools
  11. Request Composer. Google Analytics Demos & Tools