# Get total cost for all the channels
all_acquired <- all_acquired %>%
left_join(channel_lead_cost_df, by = c("LeadChannel_SalesActivity" = "channel_name"))
# Join table with npv to get revenue
all_acquired <- all_acquired %>%
left_join(npv_channel_df, by = c("LeadChannel_SalesActivity" = "channel_name"))
# Since we have not earned any revenue yet, we need only the cost from this dataset to calculate ROI
not_acquired <- not_acquired %>%
left_join(channel_lead_cost_df, by = c("LeadChannel_SalesActivity" = "channel_name"))
not_acquired_channels_cost <- not_acquired %>%
select(Channel = LeadChannel_SalesActivity, channel_cost) %>%
group_by(Channel) %>%
summarise(revenue = 0, cost = sum(channel_cost))
# compare Acquisition Date and Touch Date to get days from acquisition
# convert touchTime from factor to date variable
all_acquired_td <- all_acquired %>%
mutate(AcquisitionDate = as.Date(AcquisitionDate)) %>%
mutate(Days_to_acquisition = as.numeric(AcquisitionDate - touchTime))
# By default, the time decay attribution model has a half life of 7 days. It means that the interaction which happened 7 days prior to conversion gets half the credit of interaction that occurred on the day of conversion.
# Value formula -> N(t) = N(0)(1/2)^(t/t1/2) = N(0)* 2^-(t/t1/2)
# Here N(0) = 100%
# t1/2 = 7
# t = Days_to_acquisition
half_life <- 7
all_acquired_td <- all_acquired_td %>%
mutate(decay_weight = 100 * (2 ^ (-1*Days_to_acquisition/half_life)))
# Get total decay weight to get a ratio of decay_weight/total_weight as percent_weight
all_acquired_total_weight_td <- all_acquired_td %>%
select(c(REAgentID, decay_weight)) %>%
group_by(REAgentID) %>%
summarise(Total_weight = sum(decay_weight))
# Joining the two dataframes
all_acquired_td <- all_acquired_td %>%
left_join(all_acquired_total_weight_td, by="REAgentID")
all_acquired_td <- all_acquired_td %>%
mutate(percent_weight = decay_weight*100/Total_weight)
# Get revenue and cost by channel
acquired_channels_revenue_cost_td <- all_acquired_td %>%
mutate(lead_revenue = percent_weight*npv/10000) %>%
select(Channel = LeadChannel_SalesActivity, lead_revenue, channel_cost) %>%
group_by(Channel) %>%
summarise(revenue = sum(lead_revenue), cost = sum(channel_cost))
final_td <- getTotalCostRevenue(acquired_channels_revenue_cost_td, not_acquired_channels_cost)
final_td %>%
kable(align = c("c", "c", "c")) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "bordered")) %>%
column_spec(1:2, color = "#000000") %>%
add_header_above(c( "Time Decay - ROI" = 2))
#Time Decay graph
TimeDecayROIGraph <-
final_td %>%
filter(Channel != "Unknown") %>%
ggplot(aes(x = reorder(Channel, ROI), y = ROI)) +
geom_bar(position = "dodge", stat = "identity", fill = "#F25F09")+
coord_flip() +
basic_theme +
xlab("")+
ylab("") +
ggtitle("Time Decay ROI") +
scale_y_continuous(labels = function(x) paste0(x, "%")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
TimeDecayROIGraph