Created
September 9, 2016 07:17
-
-
Save IronistM/a92bdfd2f0ed32061be3498082331106 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Requirements ----------------------------------- | |
require(googleAnalyticsR) | |
require(lubridate) | |
require(dataframes2xls) | |
## Authentication with GA ------------------------- | |
options(googleAuthR.scopes.selected = | |
c("https://www.googleapis.com/auth/webmasters", | |
"https://www.googleapis.com/auth/analytics", | |
"https://www.googleapis.com/auth/analytics.readonly")) | |
googleAuthR::gar_auth() | |
acc.list <- google_analytics_account_list() | |
## Date range to fetch | |
start <- floor_date(Sys.Date() - months(19), "month") | |
end <- date(Sys.time()) | |
merged.df <- NULL | |
## Getting the GA data ------------------------------ | |
for (i in 1:nrow(acc.list)) { | |
id <- acc.list$viewId[i] | |
# print(paste("Now getting data for ViewID : ", id)) | |
gadata <- | |
google_analytics_4(id, | |
date_range = c(start,end), | |
metrics = c("sessions", | |
"transactions"), | |
dimensions = c("yearMonth"), | |
# dim_filters = google_seo, | |
order = order_type("transactions", | |
sort_order = "DESC", | |
orderType = "VALUE"), | |
max = 20000) | |
gadata$'type' <- acc.list$type[i] | |
gadata$'viewName' <- acc.list$viewName[i] | |
gadata$'accountName' <- acc.list$accountName[i] | |
gadata$'webPropertyName' <- acc.list$webPropertyName[i] | |
merged.df <- merge(merged.df, gadata, all=TRUE) | |
} | |
# Export data --------------------------------------- | |
index1 <- with(merged.df, grepl("[^3]*[DHH][^3]*", accountName)) | |
clean.df <- merged.df[!index1,] | |
write.csv(merged.df, file=paste0("ga_session_",ymd(today()),".csv")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment