Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Analyze retention by finding the percentage of users who started your game X days ago that are actively playing it today. Calculating that percentage over time in a line chart so you can see how your retention is increasing or decreasing. For example, in "D1" retention you’d measure what percentage of users come back and play the game 1 day afte…
var Keen=Keen||{configure:function(e){this._cf=e},addEvent:function(e,t,n,i){this._eq=this._eq||[],this._eq.push([e,t,n,i])},setGlobalProperties:function(e){this._gp=e},onChartsReady:function(e){this._ocrq=this._ocrq||[],this._ocrq.push(e)}};(function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src=("https:"==document.location.protocol?"https://":"http://")+"dc8na2hxrj29i.cloudfront.net/code/keen-2.1.0.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})();
Keen.configure({ // configure the Keen Client
projectId: projectId,
readKey: readKey
});
Keen.onChartsReady(function() {
var daysInChart = 30
var step1CollectionName = "new_user"
var step2CollectionName = "level_start"
var actorProperty = "uuid"
calculateRetention(daysInChart, step1CollectionName, step2CollectionName, 1, actorProperty, "chart4A")
calculateRetention(daysInChart, step1CollectionName, step2CollectionName, 3, actorProperty, "chart4B")
calculateRetention(daysInChart, step1CollectionName, step2CollectionName, 7, actorProperty, "chart4C")
function calculateRetention(daysInChart, step1CollectionName, step2CollectionName, retentionPeriod, actorProperty, div) {
var dataForLineChart = []
var i = 0
while (i < daysInChart) { // Cycle through each day in the chart and calculate retention for that day
// Figure out the start and end timestamps for each of the funnel steps
var firstStepDate = new Date();
firstStepDate.setDate(firstStepDate.getDate() - daysInChart - retentionPeriod + i)
firstStepDate.setHours(0,0,0)
var firstStepDateEnd = new Date(firstStepDate)
firstStepDateEnd.setDate(firstStepDateEnd.getDate() + 1)
var secondStepDate = new Date(firstStepDate);
secondStepDate.setDate(firstStepDate.getDate() + retentionPeriod)
var secondStepDateEnd = new Date(secondStepDate)
secondStepDateEnd.setDate(secondStepDateEnd.getDate() + 1)
// Define the steps for the funnel analysis
var s1 = new Keen.Step(step1CollectionName, {
timeframe: {start: firstStepDate, end: firstStepDateEnd}
});
var s2 = new Keen.Step(step2CollectionName, {
timeframe: {start: secondStepDate, end: secondStepDateEnd}
});
//Create a new Keen.Funnel for those steps.
var myFunnel = new Keen.Funnel([s1, s2], {
actorProperty: actorProperty
});
myFunnel.getResponse(function(response){
var percentage = response.result[1]/response.result[0]
dataForLineChart.push({
"value" : percentage,
"timeframe" : {
"start" : response.steps[1].timeframe["start"],
"end" : response.steps[1].timeframe["end"]
}
})
if (dataForLineChart.length == daysInChart) {
var title = "D" + retentionPeriod + " Retention"
// Need to sort data for line chart so that data points show up in order
dataForLineChart.sort(function(x, y){
date1 = new Date(x.timeframe["start"]);
date2 = new Date(y.timeframe["start"]);
return date1 - date2;
})
drawMyLineChart(dataForLineChart, "daily", div , title)
}
});
i++
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment