<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/marketing-multi-channel-attribution-model-based-on-sales-funnel-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://analyzecore.com/2017/09/28/marketing-multi-channel-attribution-model-based-on-sales-funnel-with-r/"> R language – AnalyzeCore – data is beautiful, data is a story</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div></div>
<p>This is the last post in the series of articles about using <strong>Multi-Channel Attribution</strong> in marketing. In previous two articles (<a rel="nofollow" href="http://analyzecore.com/2016/08/03/attribution-model-r-part-1/" target="_blank">part 1</a> and <a rel="nofollow" href="http://analyzecore.com/2017/05/31/marketing-multi-channel-attribution-model-r-part-2-practical-issues/" target="_blank">part 2</a>), we’ve reviewed a simple and powerful approach based on Markov chains that allows you to effectively attribute marketing channels.</p>
<p>In this article, we will review another fascinating approach that marries heuristic and probabilistic methods. Again, the core idea is straightforward and effective.</p>
<div><em><strong>Sales Funnel</strong></em></div>
<div><span>Usually, companies have some kind of idea on how their clients move along the user journey from first visiting a website to closing a purchase. This sequence of steps is called a </span><b>Sales</b><span> (</span><b>purchasing </b><span>or</span><b> conversion</b><span>) </span><b>Funnel</b><span>. </span><span>Classically, the Sales Funnel includes at least four steps:</span></div>
<ul>
<li>Awareness – the customer becomes aware of the existence of a product or service (<span>“I didn’t know there was an app for that”</span>),</li>
<li>Interest – actively expressing an interest in a product group (<span>“I like how your app does X”</span>),</li>
<li>Desire – aspiring to a particular brand or product (<span>“Think I might buy a yearly membership”</span>),</li>
<li>Action – taking the next step towards purchasing the chosen product (<span>“Where do I enter payment details?”</span>).</li>
</ul>
<p>For an e-commerce site, we can come up with one or more conditions (events/actions) that serve as an evidence of passing each step of a Sales Funnel.</p>
<p><span>For some extra information about Sales Funnel, you can take a look at my (rather ugly) approach of <a rel="nofollow" href="http://analyzecore.com/2015/06/23/sales-funnel-visualization-with-r/" target="_blank">Sales Funnel visualization with R</a>.</span></p>
<p><span>Companies, naturally, lose some share of visitors on each following step of a Sales Funnel as it gets narrower. That’s why it looks like a string of bottlenecks. We can calculate a probability of transition from the previous step to the next one based on recorded history of transitions. On the other hand, customer journeys are sequences of sessions (visits) and these sessions are attributed to different marketing channels.</span></p>
<p><span>Therefore, we can link marketing channels with a probability of a customer passing through each step of a Sales Funnel. And here goes the core idea of the concept. The probability of moving through each “bottleneck” represents the value of the marketing channel which leads a customer through it. The higher probability of passing a “neck”, the lower the value of a channel that provided the transition. And vice versa, the lower probability, the higher value of a marketing channel in question. </span></p>
<p>Let’s study the concept with the following example. First off, we’ll define the Sales Funnel and a set of conditions which will register as customer passing through each step of the Funnel.</p>
<ul>
<li>0 step (necessary condition) – customer visits a site for the first time</li>
<li>1st step (awareness) – visits two site’s pages</li>
<li>2nd step (interest) – reviews a product page</li>
<li>3rd step (desire) –  adds a product to the shopping cart</li>
<li>4th step (action) – completes purchase</li>
</ul>
<p>Second, we need to extract the data that includes sessions where corresponding events occurred. We’ll simulate this data with the following code:</p>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
library(tidyverse)
library(purrrlyr)
library(reshape2)

##### simulating the &quot;real&quot; data #####
set.seed(454)
df_raw &lt;- data.frame(customer_id = paste0('id', sample(c(1:5000), replace = TRUE)),
date = as.POSIXct(rbeta(10000, 0.7, 10) * 10000000, origin = '2017-01-01', tz = &quot;UTC&quot;),
channel = paste0('channel_', sample(c(0:7), 10000, replace = TRUE, prob = c(0.2, 0.12, 0.03, 0.07, 0.15, 0.25, 0.1, 0.08))),
site_visit = 1) %>%

mutate(two_pages_visit = sample(c(0,1),
10000,
replace = TRUE,
prob = c(0.8, 0.2)),
product_page_visit = ifelse(two_pages_visit == 1,
sample(c(0, 1),
length(two_pages_visit[which(two_pages_visit == 1)]),
replace = TRUE, prob = c(0.75, 0.25)),
0),
add_to_cart = ifelse(product_page_visit == 1,
sample(c(0, 1),
length(product_page_visit[which(product_page_visit == 1)]),
replace = TRUE, prob = c(0.1, 0.9)),
0),
purchase = ifelse(add_to_cart == 1,
sample(c(0, 1),
length(add_to_cart[which(add_to_cart == 1)]),
replace = TRUE, prob = c(0.02, 0.98)),
0)) %>%
dmap_at(c('customer_id', 'channel'), as.character) %>%
arrange(date) %>%
mutate(session_id = row_number()) %>%
arrange(customer_id, session_id)
df_raw &lt;- melt(df_raw, id.vars = c('customer_id', 'date', 'channel', 'session_id'), value.name = 'trigger', variable.name = 'event') %>%
filter(trigger == 1) %>%
select(-trigger) %>%
arrange(customer_id, date)
</pre>
</div>
</div>
<p>And the data sample looks like:</p>
<p><a rel="nofollow" href="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_1.png" target="_blank"><img sizes="(max-width: 648px) 100vw, 648px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_1.png?resize=648%2C323" alt="" class="wp-image-1826" data-recalc-dims="1"></a></p>
<p><span>Next up, the data needs to be preprocessed. For example, it would be useful to replace NA/direct channel with the previous one or separate first-time purchasers from current customers, or even create different Sales Funnels based on new and current customers, segments, locations and so on. I will omit this step but you can find some ideas on preprocessing in my previous <a rel="nofollow" href="http://analyzecore.com/2017/05/31/marketing-multi-channel-attribution-model-r-part-2-practical-issues/" target="_blank">blogpost</a>.</span></p>
<p>The important thing about this approach is that we only have to attribute the initial marketing channel, one that led the customer through their first step. For instance, a customer initially reviews a product page (step 2, interest) and is brought by channel_1. That means any future product page visits from other channels won’t be attributed until the customer makes a purchase and starts a new Sales Funnel journey.</p>
<p><span>Therefore, we will filter records for each customer and save the first unique event of each step of the Sales Funnel using the following code:</span></p>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
### removing not first events ###
df_customers &lt;- df_raw %>%
 group_by(customer_id, event) %>%
 filter(date == min(date)) %>%
 ungroup()
</pre>
</div>
</div>
<p><span>I point your attention that in this way we assume that all customers were first-time buyers, therefore every next purchase as an event will be removed with the above code.</span></p>
<p><span>Now, we can use the obtained data frame to compute Sales Funnel’s transition probabilities, importance of Sale Funnel steps, and their weighted importance. According to the method, the higher probability, the lower value of the channel. Therefore, we will calculate the importance of an each step as 1 minus transition probability. After that, we need to weight importances because their sum will be higher than 1. We will do these calculations with the following code:</span></p>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
### Sales Funnel probabilities ###
sf_probs &lt;- df_customers %>%
 
 group_by(event) %>%
 summarise(customers_on_step = n()) %>%
 ungroup() %>%
 
 mutate(sf_probs = round(customers_on_step / customers_on_step[event == 'site_visit'], 3),
 sf_probs_step = round(customers_on_step / lag(customers_on_step), 3),
 sf_probs_step = ifelse(is.na(sf_probs_step) == TRUE, 1, sf_probs_step),
 sf_importance = 1 - sf_probs_step,
 sf_importance_weighted = sf_importance / sum(sf_importance)
 )
</pre>
</div>
<p>A hint: it can be a good idea to compute Sales Funnel probabilities looking at a limited prior period, for example, 1-3 months. The reason is that customers’ flow or “necks” capacities could vary due to changes on a company’s site or due to changes in marketing campaigns and so on. Therefore, you can analyze the dynamics of the Sales Funnel’s transition probabilities in order to find the appropriate time period.</p>
<p>I can’t publish a blogpost without visualization. This time I suggest another approach for the Sales Funnel visualization that represents all customer journeys through the Sales Funnel with the following code:</p>
</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
### Sales Funnel visualization ###
df_customers_plot &lt;- df_customers %>%
 
 group_by(event) %>%
 arrange(channel) %>%
 mutate(pl = row_number()) %>%
 ungroup() %>%
 
 mutate(pl_new = case_when(
 event == 'two_pages_visit' ~ round((max(pl[event == 'site_visit']) - max(pl[event == 'two_pages_visit'])) / 2),
 event == 'product_page_visit' ~ round((max(pl[event == 'site_visit']) - max(pl[event == 'product_page_visit'])) / 2),
 event == 'add_to_cart' ~ round((max(pl[event == 'site_visit']) - max(pl[event == 'add_to_cart'])) / 2),
 event == 'purchase' ~ round((max(pl[event == 'site_visit']) - max(pl[event == 'purchase'])) / 2),
 TRUE ~ 0
 ),
 pl = pl + pl_new)

df_customers_plot$event &lt;- factor(df_customers_plot$event, levels = c('purchase',
 'add_to_cart', 
 'product_page_visit',
 'two_pages_visit',
 'site_visit'
 ))

# color palette
cols &lt;- c('#4e79a7', '#f28e2b', '#e15759', '#76b7b2', '#59a14f',
 '#edc948', '#b07aa1', '#ff9da7', '#9c755f', '#bab0ac')

ggplot(df_customers_plot, aes(x = event, y = pl)) +
 theme_minimal() +
 scale_colour_manual(values = cols) +
 coord_flip() +
 geom_line(aes(group = customer_id, color = as.factor(channel)), size = 0.05) +
 geom_text(data = sf_probs, aes(x = event, y = 1, label = paste0(sf_probs*100, '%')), size = 4, fontface = 'bold') +
 guides(color = guide_legend(override.aes = list(size = 2))) +
 theme(legend.position = 'bottom',
 legend.direction = &quot;horizontal&quot;,
 panel.grid.major.x = element_blank(),
 panel.grid.minor = element_blank(),
 plot.title = element_text(size = 20, face = &quot;bold&quot;, vjust = 2, color = 'black', lineheight = 0.8),
 axis.title.y = element_text(size = 16, face = &quot;bold&quot;),
 axis.title.x = element_blank(),
 axis.text.x = element_blank(),
 axis.text.y = element_text(size = 8, angle = 90, hjust = 0.5, vjust = 0.5, face = &quot;plain&quot;)) +
 ggtitle(&quot;Sales Funnel visualization - all customers journeys&quot;)
</pre>
</div>
</div>
<div></div>
<div></div>
<div><a rel="nofollow" href="https://i2.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_2_sf_viz.png" target="_blank"><img sizes="(max-width: 648px) 100vw, 648px" src="https://i2.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_2_sf_viz.png?resize=648%2C486" alt="" class="wp-image-1829" data-recalc-dims="1"></a></div>
<div></div>
<p><span>Ok, seems we now have everything to make final calculations. In the following code, we will remove all users that didn’t make a purchase. Then, we’ll link weighted importances of the Sales Funnel steps with sessions by event and, at last, summarize them.</span></p>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
### computing attribution ###
df_attrib &lt;- df_customers %>%
 # removing customers without purchase
 group_by(customer_id) %>%
 filter(any(as.character(event) == 'purchase')) %>%
 ungroup() %>%
 
 # joining step's importances
 left_join(., sf_probs %>% select(event, sf_importance_weighted), by = 'event') %>%
 
 group_by(channel) %>%
 summarise(tot_attribution = sum(sf_importance_weighted)) %>%
 ungroup()
</pre>
</div>
</div>
<p><span>As the result, we’ve obtained the number of conversions that have been distributed by marketing channels:</span></p>
<div><a rel="nofollow" href="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_3_attrib.png" target="_blank"><img sizes="(max-width: 319px) 100vw, 319px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/09/pic_3_attrib.png?resize=319%2C313" alt="" class="wp-image-1834" data-recalc-dims="1"></a></div>
<div></div>
<p>In the same way you can distribute the revenue by channels.</p>
<div></div>
<p>At the end of the article, I want to share OWOX company’s blog where you can read more about the approach: <a rel="nofollow" href="https://www.owox.com/blog/articles/funnel-based-attribution-model/" target="_blank">Funnel Based Attribution Model</a>.</p>
<p>In addition, you can find that <a rel="nofollow" href="https://www.owox.com/" target="_blank">OWOX</a> provides an automated system for Marketing Multi-Channel Attribution based on BigQuery. Therefore, if you are not familiar with R or don’t have a suitable data warehouse, I can recommend you to test their service.</p>
<p><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<p><span>Save</span><span>Save</span></p>
<div></div>
<p>The post <a rel="nofollow" href="http://analyzecore.com/2017/09/28/marketing-multi-channel-attribution-model-based-on-sales-funnel-with-r/" target="_blank">Marketing Multi-Channel Attribution model based on Sales Funnel with R</a> appeared first on <a rel="nofollow" href="http://analyzecore.com/" target="_blank">AnalyzeCore – data is beautiful, data is a story</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://analyzecore.com/2017/09/28/marketing-multi-channel-attribution-model-based-on-sales-funnel-with-r/"> R language – AnalyzeCore – data is beautiful, data is a story</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/pE6NRRDGQBY" height="1"></a></div>