5

I am trying to build a Markov Chain digital attribution model using GA data.

I would like some help with for loops for date ranges and then the creation of a column in my dataset which shows the month.

The query extracts data from GA and specifically from the multi channel funnel report. So, I would like all unique paths and conversions by month, so I can run a Markov Chain model and get the channel attribution by month.

I have done some basic work, but I have stuck as I am not familiar with loops.

Any help would be great.

I will try to break the code in two stages:

Stage a: Extracting the data

finding the starting and ending date per month

start_date <- seq(as.Date("2018-01-01"),length=12,by="months")
end_date <- seq(as.Date("2018-02-01"),length=12,by="months")-1

looping over starting and ending dates

    mcf_data <- list()
      for(i in 1:length(start_date)){
        for(j in 1:length(end_date)){
                   mcf_data<-print(get_mcf(ga_id,
                    start.date = start_date[i], end.date = end_date[j], 
                    metrics = "mcf:totalConversions", 
                    dimensions = "mcf:basicChannelGroupingPath", 
                    sort = NULL,
                    filters = NULL, 
                    samplingLevel = NULL, 
                    start.index = NULL, max.results = NULL, fetch.by = NULL)
  }
 }

This works fine but just gives me the total number of unique paths and Conversions. Ideally, I would like to use the i,j's in order to create an additional column where each time the loop runs from 1 to length I get a data frame which relates to a certain month, so at the end I have dataset with unique paths and conversions by Month.

Stage b: Running the Markov Chain Model

Ideally, I would like to continue the for loop from extracting the data to running the model by month

df_mcf_data <- data.frame(mcf_data$basicChannelGroupingPath
                          ,mcf_data$totalConversions,
                          conv_null = 0)

mod1 <- markov_model(df_mcf_data,
                     var_path = 'mcf_data.basicChannelGroupingPath',
                     var_conv = 'mcf_data.totalConversions',
                     var_null = 'conv_null',
                     out_more = TRUE)

df_res1 <- mod1$result

The most important part is the stage a of the code for my work. But, if there can be help for stage b as well, my total aim is to have a for loop which runs all the code at once and gives me monthly results.

John69
  • 51
  • 1

0 Answers0