Plotting Weekly Mobile Retention from the Localytics API using R and ggplot2

Part of building mobile web apps is understanding the myriad of mobile analytics and in part visualizing the data to shed light on trends that my otherwise be difficult to see in tabular data or even a colorful cohort table. I’ve been building a dashboard using R, RStudio, Shiny, and Shiny Dashboards aggregating data from MSSQL, Postgres, Google Analytics, and Localytics.

Within the Product section of the dashboard I’ve included a retention chart and found some great articles at R-Blogger like this one. The retention data comes from the Localytics API which I discussed previously though getting the data into the proper format took a few steps. Let’s start with the data, here’s an example of the REST response from Localytics looks like for a weekly retention cohort:

{
"results": [
{
"birth_week": "2014-09-08",
"users": 1,
"week": "2014-12-29"
},
{
"birth_week": "2014-09-29",
"users": 1640,
"week": "2014-12-29"
},
{
"birth_week": "2014-10-06",
"users": 2988,
"week": "2014-12-29"
},
{
"birth_week": "2014-10-13",
"users": 4747,
"week": "2014-12-29"
},
{
"birth_week": "2014-10-20",
"users": 2443,
"week": "2014-12-29"
},
…

Below is the main function to fetch the Localytics sample data and convert it into a data frame that’s suitable for plotting. Now, admittedly I’m not an R expert so there may well be better ways to slice this JSON response but this is a fairly straight forward approach. Essentially, this fetches the data, converts it from JSON to an R object, extracts the weeks, preallocates a matrix and then iterates over the data filling the matrix to build a data frame.

retentionDF <- function() {
  # Example data from: http://docs.localytics.com/dev/query-api.html#query-api-example-users-by-week-and-birth_week
  localyticsExampleJSON <- getURL('https://gist.githubusercontent.com/strefethen/180efcc1ecda6a02b1351418e95d0a29/raw/1ad93c22488e48b5e62b017dc5428765c5c3ba0f/localyticsexampledata.json')
  cohort <- fromJSON(localyticsExampleJSON)
  weeks <- unique(cohort$results$week)
  numweeks <- length(weeks)
  # Take the JSON response and convert it to a retention matrix (all numeric for easy conversion to a dataframe) like so:
  # Weekly.Cohort Users Week.1
  # 1    2014-12-29  7187   4558
  # 2    2015-01-05  5066     NA
  i <- 1
  # Create a matrix big enough to hold all of the data
  m <- matrix(nrow=numweeks, ncol=numweeks + 1)
  for (week in weeks) {
    # Get data for all weeks of this cohort
    d <- cohort$results[cohort$results$birth_week==week,][,2]
    lencohort <- length(d)
    for (n in 1:lencohort) {
      # Skip the first column using "+ 1" below which will be Weekly.Cohort (date)
      m[i,n + 1] <- d[n]
    }
    i <- i + 1
  }
  # Convert matrix to a dataframe
  df <- as.data.frame(m)
  # Set values of the first column to the cohort dates
  df$V1 <- weeks
  # Set the column names accordingly
  colnames(df) <- c("Weekly.Cohort", "Users", paste0("Week.", rep(1:(numweeks-1))))
  return(df)
}

To make things easy I put together a gist and if you’re using R you can runGist it yourself. It requires several other packages so be sure to check the sources in case you’re missing any.
Fair warning the Localytics API demo has very limited data so the chart, let’s just say simplistic however given many weeks worth of data it will fill out nicely (see example below).

> library(shiny)
> runGist("180efcc1ecda6a02b1351418e95d0a29")
Localytics Retention Plot Example
Retention Chart

Fetching Localytics API Data in R and working with the JSON result

Recently, I’ve been building a dashboard for Find&Save using R, RStudio and Shiny Dashboards and one of our core mobile metrics is 30 day active users for both iOS and Android which I can get from Localytics (our analytics platform) via their API. Googling around I found I’m not reinventing the wheel (via SO) though I did run into a problem where the response failed to convert properly thus the post.
Here’s the original function from SO with some slight tweaks for my needs:
Continue reading Fetching Localytics API Data in R and working with the JSON result

ReactNativeJNI: Check failed: *m_isDestroyed JSCExecutor::destroy() must be called before its destructor

Short post to get this indexed as we didn’t find it elsewhere on the web.
While working to ship a new release of the Find&Save Android app we ran into a crash on launch which only occurred in our release build. The crash was in JSCExecutor.cpp which is part of React Native and left very few hints as to what was wrong with no call stack, Crashlytics logs etc. The only error we had was:
ReactNativeJNI: Check failed: *m_isDestroyed JSCExecutor::destroy() must be called before its destructor
Continue reading ReactNativeJNI: Check failed: *m_isDestroyed JSCExecutor::destroy() must be called before its destructor

Dimming the insanely bright LED’s on a Motorola SB6141 cable modem

Motorola SB6141 cable modem
Blinding LED’s which fill the room with cool blue light.

A few weeks ago I decided to upgrade an aging
Motorola SB5101 cable modem to a new Motorola SURFboard 400 Series SB6141. After the obligatory call to Comcast to register the new MAC address everything was going swimmingly until I turned off the lights and suddenly landed in the world of TRON. The entire room was basked in cool blue light from the “stupidly, ridiculously, blindingly bright” LED’s. Seriously, they’re that bad and more, blinking incessantly and with a case full of holes it nearly filled the room. Continue reading Dimming the insanely bright LED’s on a Motorola SB6141 cable modem

Upgrading an iOS project from Xcode 6 to 7

At work I’m in the midst of upgrading Find&Save to Xcode 7 and in this post I thought I would capture the issues I ran into while migrating our app to the latest build tools. I decided to keep my Xcode 6 install by renaming it “Xcode 6” and installing Xcode 7 along side it and all that worked pretty seamlessly (though keep reading because the AppStore updates will bite you). Once installed I opened our project workspace and hit Project | Build and thus began the journey of our migration.
Continue reading Upgrading an iOS project from Xcode 6 to 7

bad interpreter: No such file or directory

In XCode I ran into an error which was the result of a (my) bad merge that caused this problem when building an app:

/bin/sh: /Users/strefethen/github/iPhoneGeo/Build/iPhoneGeo/Build/Intermediates/iPhoneGeo.build/Debug-iphonesimulator/iPhoneGeo.build/Script-22C5773E1BB449FF00E72A71.sh: (null): bad interpreter: No such file or directory

The issue was that the Shell command for the Build Phase had gotten removed:
Continue reading bad interpreter: No such file or directory

React Native Packager TypeError: Cannot read property 'root' of null

While working on Find&Save‘s Cash Dash Inbox feature which is built using React Native I started the node packager and launched the app into SIM but got this error as soon as the app attempted to load the bundle:

TypeError: Cannot read property 'root' of null

Turns out the issue was an outdated watchman. Ok so off to brew update
Continue reading React Native Packager TypeError: Cannot read property 'root' of null