Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brentchow/9147264 to your computer and use it in GitHub Desktop.
Save brentchow/9147264 to your computer and use it in GitHub Desktop.
Keen IO and Stripe - How to get the total volume of purchases after refunds and declined cards, and convert to dollars (Keen.Series)
Keen.onChartsReady(function() {
// Create a Series containing our total $ amount in Keen IO credit card transactions (to and from)
// Excludes: refund transactions from Stripe to card and declined cards
var myMetric = new Keen.Series("Stripe_Event", {
analysisType: "sum",
timeframe:"this_month",
interval:"daily",
targetProperty: "data.object.amount",
filters: [
{"property_name":"data.object.captured","operator":"eq","property_value":true},
{"property_name":"data.object.amount_refunded","operator":"eq","property_value":0}]
});
// Create a Series containing our total number of refunds
var myMetric2 = new Keen.Series("Stripe_Event", {
analysisType: "sum",
timeframe:"this_month",
interval:"daily",
targetProperty: "data.object.amount_refunded",
filters: [
{"property_name":"type","operator":"eq","property_value":"charge.refunded"}]
});
// (series1, series2, interval, div Id, title)
differenceTwoLineSeriesChart(myMetric, myMetric2, "daily", "myDiv", "Revenue");
});
function differenceTwoLineSeriesChart(series1, series2, interval, div, title) {
var solutionArray = [] // variable to store results of the divided queries
var result1 = null // Create variables for holding query results
var result2 = null
series1.getResponse(function(response) { // Get the values for the first Series query
result1 = response.result
differenceResults()
});
series2.getResponse(function(response) { // Get the values for the second Series query
result2 = response.result
differenceResults()
});
function differenceResults() {
// Results should look like:
// [{
// "value": 266,
// "timeframe": {
// "start": "2014-01-20T08:00:00.000Z",
// "end": "2014-01-21T08:00:00.000Z"
// }
// },
// {
// "value": 838,
// "timeframe": {
// "start": "2014-01-21T08:00:00.000Z",
// "end": "2014-01-22T08:00:00.000Z"
// }
// }]
if ((result1 != null) && (result2 != null)) {
i = 0
while (i < result1.length) {
solutionArray[i] = {
timeframe: result1[i]["timeframe"],
value: (result1[i]["value"] - result2[i]["value"])/100
}
i++;
}
drawMyLineChart(solutionArray, interval, div, title);
}
else {
// do nothing
};
};
};
function drawMyLineChart(data, interval, div, title) {
// The final formatting required so that the result can be processed by the Keen.MultiLineChart draw method.
var formattedResult = {
result: data
};
// Create a Series object so that it can be referenced by the draw method.
// This is kind of a hack since we are passing in our own result object and not really querying the collection specified here.
// The "placeholder" is used instead of a collection name, since this is not used.
var placeholderSeries = new Keen.Series("placeholder", {
// analysisType: "count",
interval: interval
});
var placeholderLineChart = new Keen.LineChart(placeholderSeries, {
height: 300,
width: 600,
// lineWidth: lineWidth,
// chartAreaWidth: chartAreaWidth,
// chartAreaLeft: chartAreaLeft,
// title: title,
color: "rgb(75, 107, 148)", // kongregate blue
// color: "#fc9", // kongregate light orange
// color: "#FF9900" // orange peel,
showLegend: false,
xAxisLabelAngle: 45
});
placeholderLineChart.draw(document.getElementById(div), formattedResult);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment