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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much Ryan!