Skip to content

Instantly share code, notes, and snippets.

@brycemcd
Last active September 21, 2016 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycemcd/5d6c9e77cd8597ff7027 to your computer and use it in GitHub Desktop.
Save brycemcd/5d6c9e77cd8597ff7027 to your computer and use it in GitHub Desktop.
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