Problem with `mutate()` input ` (dplyr_error)


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:

Problem with mutate() input user1.
x object 'userType' is not found
i Input user1 is userType.

  1. googleAnalyticsR::google_analytics(...)
  2. googleAnalyticsR:::anti_sample(...)
  3. googleAnalyticsR:::aggregateGAData(out, agg_names = agg_cols)
  4. dplyr::select(., !!!mean_selects)
  5. dplyr::group_by(., !!!dots)
  6. dplyr::group_by_prepare(.data, ..., .add = .add)
  7. dplyr:::add_computed_columns(.data, new_groups)
  8. dplyr:::mutate_cols(.data, !!!vars)

My full code is below:



## get your accounts
account_list <- ga_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()

#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',

#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),
                                 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')

dimension_list <- list('user'=list('userType'))
# yields
#                      user 
"dimension1"   "userType" 
dplyr:::mutate_cols(.data, !!!vars)

Simplified code shows that UserType is captured not as a variable but as an element. It is user that ends up as a variable. Can't help further without a reprex.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

@technocrat thank you for a reply!

Basically, here is an exact code that I want to reproduce

You will need a Google Analytics account with some site connected to it in order to make API request.

I am not sure if reprex will help because there should be a working Google Analytics view id to get access.
Please let me know if reprex is still needed.