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.