Skip to content

Instantly share code, notes, and snippets.

@voltek62
Last active September 4, 2018 21:57
Show Gist options
  • Save voltek62/8d8cd4ee44467203aa2e50e5a0a44c36 to your computer and use it in GitHub Desktop.
Save voltek62/8d8cd4ee44467203aa2e50e5a0a44c36 to your computer and use it in GitHub Desktop.
library(googleAnalyticsR)
# https://dataseolabs.com
## authentication with both GA and SC
options(googleAuthR.scopes.selected =
c("https://www.googleapis.com/auth/analytics",
"https://www.googleapis.com/auth/analytics.readonly"))
googleAuthR::gar_auth()
## replace with your GA ID
ga_id <- 123456
## date range to fetch
start <- as.character(Sys.Date() - 93)
end <- "2018-07-20"
## Using new GA v4 API
## GAv4 filters
google_seo <- filter_clause_ga4(list(dim_filter("medium",
"EXACT",
"organic"),
dim_filter("source",
"EXACT",
"google")),
operator = "AND")
max_results <- 20000
## Getting the GA data
gadata <- google_analytics_4(ga_id,
date_range = c(start,end),
metrics = c("sessions",
"transactions",
"transactionRevenue",
"pageviews"),
dimensions = c("landingPagePath"),
dim_filters = google_seo,
order = order_type("transactions",
sort_order = "DESC",
orderType = "VALUE"),
max = max_results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment