Last active
March 18, 2021 06:54
-
-
Save ryanpraski/237d2893a69c5b8ef69196fa7fdb14fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Thanks so much Ryan!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.