How To Conditionally Group Google Analytics Channel Data In R Programming

How To Conditionally Group Google Analytics Channel Data In R Programming

Custom channel definitions feature in Google Anlytics [or Adobe as well] allows us to create channels based on parameters that we define. However, that can also mean having excess channels, thereby making them too granular for analysis. Examples of this can be channels such as Social Paid, Social Organic, Video Preroll etc. It sounds good in theory but unless you have a strong control over who uses what campaign params, you’ll end up with more exceptions than norms.

Below is an example script with solution from Stackoverflow that runs an IF / ELSE condition on the field and creates a new field for groupChannel.

The script pretty much contains channel data and sessions. Using base R, it runs a check for whether channelGrouping column = Direct , THEN groupChannel will be called Direct. If channelGrouping (Default channel grouping) contains Organic Search, THEN groupChannel = Organic Search ELSE “All other channels”.

View ID here has been changed to 1234567. Please use your own correct ViewId and pull the data.

Link to my question in Stackoverflow and answer provided by the Stackoverflow community: Same link provided in R script as well.

https://stackoverflow.com/questions/61505957/use-str-detect-function-to-conditionally-create-a-new-column-in-r-dataframe/61507415#61507415

#install and load packages
install.packages("googleAuthR")
install.packages("googleAnalyticsR")
library(googleAuthR)
library(googleAnalyticsR)
library(ggplot2)
library(googleAuthR)

ga_auth()
ga_account_list()
#use your own view ID from GA, good sire
viewId <- 1234567 

#sample data from GA
gaData <- google_analytics(viewId = viewId,
date_range = c("2020-04-01","2020-05-06"),
metrics = c("sessions"),
dimensions = c("channelGrouping"),
anti_sample = TRUE)
View(gaData)
#stackoverflow solution
#https://stackoverflow.com/questions/61505957/use-str-detect-function-to-conditionally-create-a-new-column-in-r-dataframe/61507415#61507415
# Use base R regex functions to for conditions, 
#and return values for new column
?grep()
gaData$groupChannel <- sapply(gaData$channelGrouping, 
FUN = function(x){
if (grepl("direct", tolower(x))){
return("Direct")
}else if (grepl("organic search", tolower(x))){
return("Organic Search")
}else{
return("All other channels")
}
})
#Shows entire data as a table
View(gaData)
Geocoding Customer Locations To Produce Google Maps (Bubble Chart) in Data Studio

Geocoding Customer Locations To Produce Google Maps (Bubble Chart) in Data Studio

How To Send Additional Page Load Time Metrics Into Google Analytics Using Performance.Timing variables

How To Send Additional Page Load Time Metrics Into Google Analytics Using Performance.Timing variables