Paid Session Metrics
You can use Keen to learn about user sessions where your customers make one or more purchase. The JavaScript examples below provide some ideas for how you can find:
- Average paid session length
- Average total purchases per paid session
- Average price per unit
Average Paid Session Length
In the example below, the query uses an event collection for completed sessions (i.e. session_end) and filters on sessions where there was a purchase at some point over the course of the session.
var client = new Keen({
projectId: 'YOUR_PROJECT_ID',
readKey: 'YOUR_READ_KEY'
});
Keen.ready(function(){
var avgPaidSessionLength = new Keen.Query('average', {
event_collection: 'session_end',
target_property: 'session.age', // in seconds
timeframe: 'last_7_days',
filters: [
{
property_name: 'session.event_rollup.payment',
operator: 'gt',
property_value: 0
}
]
});
var avgPaidSessionMetric = new Keen.Dataviz()
.el(document.getElementById('chart-paid-sessions'))
.prepare();
client.run(avgPaidSessionLength, function(err, response) {
// if (err) throw('Error!');
avgPaidSessionMetric
.parseRawData({ result: response.result / 60 })
.title('Minutes')
.render();
});
});
Average Purchases Per Paid Session
You can combine different queries to calculate the average total purchases per paid session, as well as the average price per unit.
First we write the queries.
var client = new Keen({
projectId: "YOUR_PROJECT_ID",
readKey: "YOUR_READ_KEY"
});
Keen.ready(function(){
// all of our queries will use the same timeframe
var timeframe = 'last_3_days';
var paidSessionsCount = new Keen.Query('count_unique', {
event_collection: 'payment',
target_property: 'session.id',
timeframe: timeframe,
});
var totalPayment = new Keen.Query('sum', {
event_collection: 'payment',
target_property: 'event.amount_cents',
timeframe: timeframe,
});
var totalPlays = new Keen.Query('count', {
event_collection: 'song_play', // i.e. a unit of purchase
filters: [
{
property_name : 'session.event_rollup.payment',
operator : 'gt',
property_value : 0
}
],
timeframe: timeframe,
});
});
Next, we run two of the queries defined above, and combine the results.
var client = new Keen({
projectId: "YOUR_PROJECT_ID",
readKey: "YOUR_READ_KEY"
});
Keen.ready(function(){
// These queries are defined above for brevity, but in practice
// these should be defined within the same Keen.ready() callback
var queries = [
paidSessionsCount,
totalPlays
];
// Create a new Dataviz instance
var avgPurchasesMetric = new Keen.Dataviz()
.el(document.getElementById('chart-session-purchases'))
.prepare();
// Run multiple queries together
client.run(queries, function(err, res){
// if (err) throw('Error!');
avgPurchasesMetric
.parseRawData({ result: res[1].result / res[0].result })
.title('Plays')
.render();
});
});
Average Price Per Unit
Using two of the queries defined above, you can also calculate the average price per unit.
var client = new Keen({
projectId: "YOUR_PROJECT_ID",
readKey: "YOUR_READ_KEY"
});
Keen.ready(function(){
// These queries are defined above for brevity, but in practice
// these should be defined within the same Keen.ready() callback
var queries = [
totalPayment,
totalPlays
];
// Create a new Dataviz instance
var avgUnitPriceMetric = new Keen.Dataviz()
.el(document.getElementById('chart-unit-price'))
.chartOptions({
prefix: '$'
})
.prepare();
client.run(queries, function(err, res){
// if (err) throw('Error!');
avgUnitPriceMetric
.parseRawData({ result: (res[0].result / 100) / res[21].result })
.title('per unit')
.render();
});
});
Here’s an example of what this type of metric returns: