Skip to content

Instantly share code, notes, and snippets.

@benhowes
Created May 8, 2014 14:44
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 benhowes/8b4f0b91c9b4cab432e8 to your computer and use it in GitHub Desktop.
Save benhowes/8b4f0b91c9b4cab432e8 to your computer and use it in GitHub Desktop.
/* Sample of the events which will be used to replay */
[
{
"collection": "viewChange",
"delta": 0,
"frame": 31,
"timestamp": 1398613195820
},
.....
{
"collection": "zoomTracking",
"delta": 8,
"timestamp": 1398613201611,
"x": 507,
"y": 271
},
....
]
#!/usr/bin/python2.7
""" Quick and dirty keen collection downloader for Zoetrope engage viewer tracking
Ben Howes - http://zoetrope.io """
import keen
import json
from dateutil import parser
# The UUID of a session - generated on page load in the browser.
target_view = "F7C8928E-7942-4A75-9F26-47B22BC57E21"
keen_filters = [{
'property_name' : 'view_id',
'operator' : 'eq',
'property_value' : target_view
}]
# KEYS REMOVED!
keen.project_id=""
keen.write_key=""
keen.read_key=""
# The names of the collections we need data from
collectionsToGet = ['zoom', 'zoomTracking', 'help', 'viewChange']
filtered_data = []
extracted_data = []
last_event_time = None
#get all the data from keen
for collection in collectionsToGet:
print "Fetching collection {}....".format(collection)
data = keen.extraction(collection, filters=keen_filters)
for event in data:
event['collection'] = collection # Add the collection name to the event
# Some events contain multiple movements which we need to flatten
# They're collected as one event to save on sending 100's of nearly
# identical events.
if collection == 'zoomTracking':
for moveEvent in event['trackingData']:
moveEvent['collection'] = collection
extracted_data.append(moveEvent)
elif collection == 'viewChange':
for moveEvent in event['frameTracker']:
moveEvent['collection'] = collection
extracted_data.append(moveEvent)
else:
extracted_data.append(event)
print "done."
# The events are in collection order rather than time, sort the events by time
extracted_data = sorted(extracted_data, key=lambda k: k['timestamp'])
for i,event in enumerate(extracted_data):
#give an overview of the events order
print event['collection'],",",
# Create a time delta
if last_event_time == None:
delta = 0
last_event_time = event['timestamp']
else:
current_time = event['timestamp']
delta = (current_time - last_event_time)
last_event_time = current_time
event['delta'] = delta
filtered_data.append(event)
print "end"
# Write a JSON file with the results
with open('data.json', 'wt') as out:
res = json.dumps(filtered_data, sort_keys=True, indent=4, separators=(',', ': '))
out.write(res)
function nextEvent(){
if(replayData.length > eventIndex){
var collection = replayData[eventIndex]['collection'],
reelEvent = replayData[eventIndex];
console.log(collection);
switch(collection){
case 'viewChange':
// Simulates a mouse drag, removed to make it more readable
case 'zoom':
$zoom.simulate('click', function(){console.log("Simualted zoom click")});
break;
case 'zoomTracking':
$zoomer = $('.zoe-zoom');
coord = {
clientY: Math.round(reelEvent['y'] - $zoomer.height()/2),
clientX: Math.round(reelEvent['x'] - $zoomer.width()/2),
};
$zoomer.simulate('mousemove',coord);
break;
case 'help':
$help.simulate('click', function(){console.log("Simualted help click")});
break;
}
}
//setup the next event
if(replayData.length > eventIndex+1){
setTimeout(nextEvent, replayData[eventIndex+1]['delta']);
}
eventIndex++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment