Skip to content

Instantly share code, notes, and snippets.

@wetzler
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wetzler/4befe1a8afdaa2429702 to your computer and use it in GitHub Desktop.
Save wetzler/4befe1a8afdaa2429702 to your computer and use it in GitHub Desktop.
Mashup two Keen IO queries using Keen IO JS library v3
Keen.ready(function(){
// ----------------------------------------
// Total Session Count
// How it works:
// Given a timeframe, calculate the number of unique session IDs found in the collection "screen_view"
// We use the "screen_view" collection because we know there is at least 1 screenview for every session.
// ----------------------------------------
var sessions_count = new Keen.Query("count_unique", {
eventCollection: "screen_view",
targetProperty: "session.id",
timeframe: timeframe,
timezone: timezone
});
// ----------------------------------------
// Total Paid Session Count
// How it works:
// Given a timeframe, calculate the number of unique session IDs that are found in the collection of payment events
// ----------------------------------------
var paid_sessions_count = new Keen.Query("count_unique", {
eventCollection: "payment",
targetProperty: "session.id",
timeframe: timeframe,
timezone: timezone
});
var mashup = client.run([sessions_count, paid_sessions_count], function(res){ // Send query to Keen IO
$('#total-session-count').html(this.data[0].result) // Print the query result to appropriate div
$('#paid-session-count').html(this.data[1].result) // Print the query result to appropriate div
// divide paid sessions by sessions to get conversion rate
var conversion_rate = (this.data[1].result/this.data[0].result).toFixed(2)*100
window.chart = new Keen.Visualization({result: conversion_rate}, document.getElementById('paid-session-conversion'), {
chartType: 'metric',
title: "Conversion %",
width: 400,
colors: ['#6ab975']
});
// Or simply pass the value into a div
// $('#paid-session-conversion').html(conversion_rate)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment