Last active
April 6, 2017 17:38
-
-
Save brentchow/8994617 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.Metric)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Keen.onChartsReady(function(){ | |
// Create a Metric 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.Metric("Stripe_Event", { | |
analysisType: "sum", | |
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 Metric containing our total number of refunds | |
var myMetric2 = new Keen.Metric("Stripe_Event", { | |
analysisType: "sum", | |
targetProperty: "data.object.amount_refunded", | |
filters: [ | |
{"property_name":"type","operator":"eq","property_value":"charge.refunded"}] | |
}); | |
// Find the difference and convert to dollars | |
myMetric.getResponse(function(response){ | |
firstValue = response.result; | |
myMetric2.getResponse(function(nextResponse){ | |
secondValue = nextResponse.result; | |
difference = firstValue-secondValue; | |
dollar = difference / 100; | |
console.log(dollar); | |
//Create a Number visualization for that metric. | |
var myNumberVisualization = new Keen.Number(myMetric2, { | |
height: "300", | |
width: "600", | |
prefix:"$", | |
label: "Revenue" | |
}); | |
//Draw the visualization in a div | |
myNumberVisualization.draw(document.getElementById("myDiv"), | |
{"result":dollar}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment