Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically take a waterfall graph in Mixpanel and create a user retention curve
# Plots a graph of retention after exporting data from Mixpanel's retention
# feature. See Thoughtbot blog: https://robots.thoughtbot.com/create-a-retention-curve-with-mixpanel-and-google-sheets
# and my blog: http://www.brycemcdonnell.com/plotting-user-retention-curves-in-R/
library(lubridate)
library(tidyr)
library(dplyr)
library(ggplot2)
##
# functions
##
# TODO - acquire through Mixpanel's API
juggleData <- function(path_to_csv) {
# read in data
retention <- read.csv(path_to_csv, header=TRUE, stringsAsFactors=TRUE)
# type data correctly
retention$start.date <- ymd(retention$start.date)
# Transform data for graphing
retentionv <- retention[, -2] %>% gather(t, retained, -start.date)
retentionc <- retention[, 1:2]
retentiong <- inner_join(retentionv, retentionc, by='start.date') %>%
mutate(retention_percent = retained / cohort.size,
weeks_ago = as.integer(round(difftime(now(), start.date, units='weeks')))
)
retentiong
}
# plotting function plots n number of weeks depending on data set passed in
plotRetention <- function(dataSet) {
ggplot(data=dataSet) +
geom_line(aes(group=start.date, x=t, y=(retention_percent * 100),
color=weeks_ago),
size=1
) +
labs(title="Retention Over Time", y="% Retained", x="Weeks Since Event") +
theme_bw()
}
retention <- juggleData("retention_week_2015-04-06_to_2015-07-05.csv")
# Plot all the things
plotRetention(retention)
# Plot interesting time scale
plotRetention( retention %>% filter(t != 'X0.weeks.later' & weeks_ago != 27))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment