Skip to content

Instantly share code, notes, and snippets.

@wetzler
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wetzler/2ef7d45072cf4d92c16f to your computer and use it in GitHub Desktop.
Save wetzler/2ef7d45072cf4d92c16f to your computer and use it in GitHub Desktop.
Pull and sort of bunch of session events from Keen IO
require 'rubygems'
require 'net/http' # This is probably not needed, did not validate which things are truly required...
require 'net/https'
require 'keen'
require 'json'
require 'date'
require 'time'
require 'set'
NUM_LATEST_SESSIONS = 1000
# First, we enter the Keen IO project info for the data we'll be reading.
keen_project = Keen::Client.new(
:project_id => <project_id>,
:read_key => <read_key>
)
# Extract from Keen IO a bunch of sessions IDs that we would like study
completed_sessions = keen_project.extraction('session_end',
:target_property => 'session.id',
:latest => NUM_LATEST_SESSIONS,
)
puts completed_sessions.length # How many sessions are we looking at?
# Each flow starts with a 'session_start', but beyond that, the following events
# can occur in varying orders (aka flows).
completed_sessions.each do |data|
puts session_id = data['session']['id']
# Create a bucket to store all the session events for each session
# Start by filling it with screen_view events
session_events = keen_project.extraction('screen_views',
:filters => [{
:property_name => 'session.id',
:operator => 'eq',
:property_value => session_id,
}],
# This screenviews collection happens to have a label for each screen called "session.current.type"
:property_names => (['session.current.type', 'keen.timestamp']).to_json
)
# Now go get other types of events, such as payment events
payment = keen_project.extraction('payment',
:filters => [{
:property_name => 'session.id',
:operator => 'eq',
:property_value => session_id,
}],
:property_names => (['keen.timestamp']).to_json
)
# Add the payment events to the array of events.
payment.each do |m|
# To match the existing events we have in session_events, we're adding new "session.current.type" events with the label "money"
# We should probably re-write this script with something more simple and generic like "label" for each session event
m['session'] = {'current' => {'type' => 'money'}}
session_events << m
end
end
# Add plays type events to the set of session events
plays = keen_project.extraction('play',
:filters => [{
:property_name => 'session.id',
:operator => 'eq',
:property_value => session_id,
}],
:property_names => (['event.play_type', 'keen.timestamp']).to_json
)
unless plays.empty?
plays.each do |p|
# Add the play events to our array using a label based on the "play_type"
p['session'] = {'current' => {'type' => p['event']['play_type']}}
session_events << p
end
end
# Now let's convert the timestamps we get from Keen IO into Epoch integers.
session_events.each do |e|
e['keen']['timestamp_epoch'] = DateTime.parse(e['keen']['timestamp']).to_time.to_i
end
# Sort the events by their timestamps so that we can see them in the order they happened
sorted_events = session_events.sort_by { |hsh| hsh['keen']['timestamp_epoch']}
# Do something with the sorted events!
puts sorted_events
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment