Use R to Connect to Google Analytics 4 to Download Data

Google Analytics BCS 9 months ago (10-31) 968 Views 0 Comments
文章目录[隐藏]

Updated: October 31, 2025

If you’ve ever tried to pull GA4 data into R, you know the usual drill: create a service account in Google Cloud Console, download a JSON key, set up authentication — it works, but it’s a hassle. Especially if you just want to quickly grab some data for analysis.

That’s where the `googleAnalyticsR` package comes in — with a much simpler approach: OAuth authorization right from your browser.

In this guide, I’ll walk you through how to connect to GA4 using R with OAuth authentication, pull data for the last 7 days, and get it into a clean data frame. No console setup, no service account, no JSON keys.

Step 1:Install the googleAnalyticsR Package

First, install and load the package in R:

install.packages(c("googleAnalyticsR")
library(googleAnalyticsR)

That’s the only dependency you need. The package handles all the GA4 API calls under the hood.

 

Step 2:Authorize with Google OAuth

Here’s the part that makes this approach so much easier than the traditional service account method. Instead of creating a service account in Google Cloud Console and managing JSON keys, you just call one function:

# Automatic pop-up login to Google account
ga_auth()

This will pop open a browser window asking you to log into your Google account and authorize access. Simply click through, and you’re authenticated. The token is cached locally, so you won’t even need to re-authenticate next time.

Why I recommend this approach: No Google Cloud Console setup, no service account keys to manage, no environment variables to configure. One function call and you’re done.

 

Step 3:Pull GA4 Data into R

Now let’s get some data. Replace the Property ID below with your own:

# ←← Replace your Property ID←←
PROPERTY_ID <- "206759202"  


# Retrieve raw data (returns a data.frame)
raw_data <- ga_data(
  propertyId = PROPERTY_ID,
  date_range = c("7daysAgo", "yesterday"),
  metrics = "activeUsers",
  dimensions = "date"
)

# Manually process dates + sort
# Conversion date: From "20251024" → "2025-10-24"
raw_data$date <- as.Date(raw_data$date, format = "%Y%m%d")

# Sort by date in ascending order
raw_data <- raw_data[order(raw_data$date), ]

# Output results
print(raw_data)

 

This returns a data frame with two columns: `date` (as a string like “20251024”) and `activeUsers`.
Use R to Connect to Google Analytics 4 to Download Data

You’ll get a clean, sorted data frame showing daily active users for the last 7 days.

That’s all it takes. Install → authorize → pull data. Three steps, no console configuration needed.

 

Final Words

The `googleAnalyticsR` package with OAuth authentication is by far the easiest way to pull GA4 data into R. No service accounts, no JSON keys, no Cloud Console setup — just `ga_auth()` and you’re in.

Here’s a quick recap:

  • Install the package: `install.packages(“googleAnalyticsR”)`
  • Authorize: `ga_auth()` — a browser window handles the rest
  • Pull data: use `ga_data()` with your Property ID, metrics, and dimensions
  • Clean: convert the date format and sort

I hope this guide helped you get started with GA4 data in R. If you run into issues with authentication or the API calls, feel free to leave a comment and I’ll do my best to help.

Like (0)
Post my comment
Cancel comment
Expression Bold Strikethrough Center Italic

Hi, you need to fill in your nickname and email!

  • Nickname (required)
  • Email (required)
  • Website