Skip to content

Instantly share code, notes, and snippets.

@wetzler
Last active December 15, 2015 05:38
Show Gist options
  • Save wetzler/5210061 to your computer and use it in GitHub Desktop.
Save wetzler/5210061 to your computer and use it in GitHub Desktop.
This script creates a retention funnel query in Keen IO and outputs both the funnel query name and the results of the query.
require 'rubygems'
require 'net/http'
require 'net/https'
require 'uri'
require 'json'
require 'date'
require 'active_support/all' #for datetime calculation e.g. weeks.ago.at_beginning_of_week
# Keen Project Info
$projectID = "your project ID"
$key = "your project api key"
# Query params
$api_version = "3.0"
$api_url = "https://api.keen.io"
#================================oOo===================================
# Function which assembles and saves the funnel query in Keen IO
def save_keen_query(query_name, steps)
uri = URI.parse($api_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE #Be better than this
request = Net::HTTP::Put.new "/#{$api_version}/projects/#{$projectID}/saved_queries/#{query_name}"
request.add_field("Authorization", $key)
request.add_field("Content-Type", "application/json")
meat = {
:analysis_type => "funnel",
:steps => steps,
}
request.body = meat.to_json
http.request(request)
end
#================================oOo===================================
# Function to get Keen IO funnel result
# Given a funnel query URL it will return the response from Keen IO
def get_keen_value(keen_query_url)
uri = URI.parse(keen_query_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE #Be better than this
http.start() { |http|
response = http.get(uri.request_uri)
}
end
#================================oOo===================================
# Funnel Steps
steps = [
{
:event_collection => "create_organization",
:actor_property => "organization.id",
:timeframe => {
:end => 14.days.ago
},
},
{
:event_collection => "analysis_api_call",
:actor_property => "organization.id",
:timeframe => {
:start => 7.days.ago
}
}
]
query_name = "This_wks_active_users_setup_over_7_days_ago"
save_query_result = save_keen_query(query_name, steps)
if not save_query_result.to_s.include? 'HTTPCreated' # Error Handling
puts "Issue with Saving Query!!!"
puts save_query_result.to_s
puts save_query_result.body.to_s
else
saved_query_url = "https://api.keen.io/#{$api_version}/projects/#{$projectID}/saved_queries/#{query_name}/result?api_key=#{$key}"
print "Query URL: "
puts saved_query_url = "https://api.keen.io/#{$api_version}/projects/#{$projectID}/saved_queries/#{query_name}/result?api_key=#{$key}"
query_result = get_keen_value(saved_query_url)
if query_result.to_s.include? 'HTTPOK' # Error Handling
puts JSON.parse(query_result.body)['result']
else
puts query_result.to_s
puts query_result.body.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment