Skip to content

Instantly share code, notes, and snippets.

@wetzler
Created November 19, 2013 07:56
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/7541840 to your computer and use it in GitHub Desktop.
Save wetzler/7541840 to your computer and use it in GitHub Desktop.
This script runs a group_by query and outputs the results to a CSV
require 'rubygems'
require 'net/http'
require 'net/https'
require 'keen'
require 'uri'
require 'json'
require 'date'
# This script runs a group_by query and outputs the results to a CSV
#================================oOo===================================
# Enter your Keen Project Info
Keen.project_id = "<ID>"
Keen.read_key = "<READ KEY>"
$projectID = Keen.project_id
$collection = "purchases"
timeframe = {
:start => "2013-11-17T06:00Z",
:end => "2013-11-17T07:00Z"
}
data1 = Keen.count($collection,
:group_by => "country",
:filters => [{
:property_name => "Category",
:operator => "eq",
:property_value => "Shoes",
}],
:timeframe => timeframe,
)
puts "got data 1"
fileResults = File.open('results_'+Time.now.to_s+'.csv', 'w')
data1.each do |country|
country_name = country['country_name']
count = country['result']
puts "#{country_name}, #{count}"
fileResults.puts "#{country_name}, #{count}"
end
fileResults.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment