Hi
I am using R to get unsampled data from Google Analytics using googleAnalyticsR and tidyverse libraries.
Would you be able please to help me understand how to fix the error I get?
I get the following error:
<error/dplyr_error>
Problem with mutate()
input user1
.
x object 'userType' is not found
i Input user1
is userType
.
Backtrace:
- googleAnalyticsR::google_analytics(...)
- googleAnalyticsR:::anti_sample(...)
- googleAnalyticsR:::aggregateGAData(out, agg_names = agg_cols)
- dplyr::select(., !!!mean_selects)
- dplyr::group_by(., !!!dots)
- dplyr::group_by_prepare(.data, ..., .add = .add)
- dplyr:::add_computed_columns(.data, new_groups)
- dplyr:::mutate_cols(.data, !!!vars)
My full code is below:
library(googleAnalyticsR)
library(tidyverse)
ga_auth()
## get your accounts
account_list <- ga_account_list()
account_list
## View account_list and pick the viewId you want to extract data from.
ga_view_id <- 11111111
# date range
date_start <- "2020-08-15"
date_end <- "2020-11-15"
#retrieve list of available dimensions
ga_dim <- ga_meta()
head(ga_dim)
#Unsampled data
#GA will aggregate the data based on the supplied dimensions. So, dimensions
#need to be carefully selected to avoid miss-aggregation, e.g., collecting deviceCategory
#(desktop/mobile/tablet)at the same request with movileDeviceModel will return data
#from mobile/tablet only (API will omit desktop data)
#Using a list-of-list allows us to control the dimensions supplied to the API (maximum of six
#dimensions allowed for each list as the API requests provided with the transactionId as the key)
dimension_list <- list('user'=list('userType','deviceCategory','city',
'browser','operatingSystem'),
'user2'=list('visitLength','userGender','userAgeBracket'),
'seasonality'=list('day','dayOfWeek','yearWeek'))
#identify list of metrics
metric_list <- c('transactionRevenue','transactions','avgSessionDuration','users','bounceRate','avgPageLoadTime',
'avgTimeOnPage', 'sessions', 'avgServerConnectionTime','avgDomainLookupTime')
#initiate the data frame to store the merged dataset
ga_data <- data.frame()
for (i in 1:length(dimension_list)) {
ga_request <- google_analytics(viewId = ga_view_id,
date_range= c(date_start, date_end),
metrics=metric_list,
dimensions = c('dimension1',unlist(dimension_list[i])),
anti_sample = TRUE)
if (i == 1) {
#the first output will be set as a base
ga_data <- ga_request
} else {
#merge the subsequent output with the base using a specific key,
#(in this case: clientId)
#first, need to remove the metrics column to avoid duplicated metrics
#column name (suffix: _x & _y)
ga_request <- ga_request %>% select(-metric_list)
ga_data <- ga_data %>% left_join(ga_request, by = 'dimension1')
}
}
ga_data```