# Load customer collection customers <- canopy::find("customers", query.all()) # Enter email IDs to analyze in the list, each with "quotes" # You can choose as many email IDs as you like emailID<-c("22222222","33222233") # Set the time lag from receiving an email to opening or clicking campaignlag = 5; # Set the time lag from clicking an email to making a purchase clicklag = 2; # Set the time lag from opening an email to making a purchase openlag = 2; # Loop through email IDs for(h in 1:length(emailID)) { actionsCtr = 0; campaigncount = 0; clickcount = 0; opencount = 0; buycount = 0; sentCount = 0; amountcount = 0; now = Sys.Date(); emailClickTimestamp = now openTime = now receivedTime = now # Loop through customers. for(i in 1:length(customers)) { actionsCtr = actionsCtr + length(customers[[i]]$actions) curCustActions = customers[[i]]$actions; gotEmail = FALSE; clickedEmail = FALSE; openedEmail = FALSE; boughtInCampaign = FALSE; emailClickTimestamp = now openTime = now receivedTime = now for(j in 1:length(curCustActions)) { if(curCustActions[[j]][["action"]] == "email") { if (curCustActions[[j]][["additional-data"]][["email-id"]] == emailID[[h]]) { if(gotEmail == FALSE){ campaigncount = campaigncount + 1; gotEmail=TRUE; # print(customers[[i]][["customer-id"]]); } if (curCustActions[[j]][["additional-data"]][["email-action"]] == "received") { receivedTime = curCustActions[[j]][["timestamp"]]; } if (curCustActions[[j]][["additional-data"]][["email-action"]] == "click") { if (clickedEmail == FALSE){ clickcount = clickcount + 1; clickedEmail = TRUE; emailClickTimestamp = curCustActions[[j]][["timestamp"]]; } if (curCustActions[[j]][["timestamp"]] < emailClickTimestamp){ emailClickTimestamp = curCustActions[[j]][["timestamp"]]; } } if (curCustActions[[j]][["additional-data"]][["email-action"]] == paste("ope", "n", sep="")) { if (openedEmail == FALSE){ opencount = opencount + 1; openedEmail = TRUE; } openTime = curCustActions[[j]][["timestamp"]]; } } } } # We do a second loop as we can't assume actions are sorted. for(j in 1:length(curCustActions)) { if(curCustActions[[j]][["action"]] == "purchase") { if(clickedEmail) { if(as.Date(curCustActions[[j]][["timestamp"]]) > emailClickTimestamp) { if(as.Date(curCustActions[[j]][["timestamp"]]) < (as.Date(emailClickTimestamp) + clicklag)) { if(as.Date(curCustActions[[j]][["timestamp"]]) < (as.Date(receivedTime) + campaignlag)) { boughtInCampaign = TRUE; buycount = buycount + 1; amountcount = amountcount + curCustActions[[j]][["additional-data"]][["amount"]] } } } } if (boughtInCampaign == FALSE) { if(openedEmail) { if(curCustActions[[j]][["timestamp"]] > openTime) { if(as.Date(curCustActions[[j]][["timestamp"]]) < (as.Date(openTime) + openlag)) { if(as.Date(curCustActions[[j]][["timestamp"]]) < (as.Date(receivedTime) + campaignlag)) { boughtInCampaign = TRUE; buycount = buycount + 1; amountcount = amountcount + curCustActions[[j]][["additional-data"]][["amount"]] } } } } } } } if(boughtInCampaign) { print(customers[[i]][["customer-id"]]); } } print(campaigncount) print(opencount) print(clickcount) print(buycount) print(opencount/campaigncount) print(clickcount/opencount) print(buycount/clickcount) print(amountcount) }