Skip to content

Instantly share code, notes, and snippets.

@ryanpraski
Last active March 18, 2021 06:54
Show Gist options
  • Select an option

  • Save ryanpraski/237d2893a69c5b8ef69196fa7fdb14fc to your computer and use it in GitHub Desktop.

Select an option

Save ryanpraski/237d2893a69c5b8ef69196fa7fdb14fc to your computer and use it in GitHub Desktop.
library(googleAnalyticsR)
library(ggplot2)
#Authorize Google Analytics R- this will open a webpage
#You must be logged into your Google Analytics account on your web browser
ga_auth()
#Use the Google Analytics Management API to see a list of Google Analytics accounts you have access to
my_accounts <- google_analytics_account_list()
View(my_accounts)
#Use my_accounts to find the viewId. Make sure to replace this with your viewId.
my_id <- 94579701
#set date variables for dyanmic date range
start_date <- "60daysAgo"
end_date <- "yesterday"
#Page View Query
df1 <- google_analytics_4(my_id,
date_range = c("2016-12-10", "2017-02-07"),
metrics = c("pageviews"),
dimensions = c("pagePath"))
#Session Query - Uses start_date and end_date
df2 <- google_analytics_4(my_id,
date_range = c(start_date, end_date),
metrics = c("sessions"),
dimensions = c("date"))
#graph sessions by date
ggplot(data=df2, aes(x=date, y=sessions)) +
geom_line(stat="identity")
@mmsfpr

mmsfpr commented Sep 16, 2019

Copy link
Copy Markdown

Ryan, I'm brand new to programming languages and R. I was watching your tutorial http://www.ryanpraski.com/google-analytics-r-tutorial/vid and actually had all my accounts in a tible! (That was a major accomplishment, even if I then got a permission error; something was right because I had 486 Analytics Accounts piped into R!)

Also, I was getting something about deprecated something or other. Anyway, my machine froze, I lost my work and I'm starting all over. I see there are great instructions along with the video I hadn't noticed. Just going to run this script this time, instead of doing each step from the video, lol.

@mmsfpr

mmsfpr commented Sep 16, 2019

Copy link
Copy Markdown

image
2019-09-16 13:53:32> Downloaded [1000] rows from a total of [5521]. Warning message: 'google_analytics_4' is deprecated. Use 'google_analytics' instead. See help("Deprecated") and help("googleAnalyticsR-deprecated").

Just trying to figure out the Deperecated message.

@mmsfpr

mmsfpr commented Sep 16, 2019

Copy link
Copy Markdown

Thanks so much Ryan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment