Last active
August 12, 2016 08:23
-
-
Save benhawker/e0679cb6b3ec6eec3a552059b8f6583b to your computer and use it in GitHub Desktop.
Pulls values on views to inquiry to booking conversion into a CSV for analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
class RoomStats | |
VALUES = %w(num_views num_inquiries num_bookings | |
views_to_inquiry_conversion inquiry_to_booking_conversion | |
views_to_booking_conversion) | |
attr_reader :counts, :averages | |
def initialize | |
@counts = [] | |
@averages = {} | |
end | |
def run | |
get_room_data | |
calculate_averages | |
export_averages | |
export_top_conversions(export_top_views_to_inquiry_conversion, path_builder("views_to_inquiry", "csv")) | |
export_top_conversions(export_top_inquiry_to_booking_conversion, path_builder("inquiries_to_booking", "csv")) | |
end | |
def get_room_data | |
Room.active.visible.find_each do |room| | |
if room.views_count > 0 && room.inquiries.count > 0 | |
city = Destination.find(room.city_destination_id).name if room.city_destination_id.is_a?(Integer) | |
counts << { | |
room_id: room.id, | |
title: room.title, | |
city: city, | |
num_views: room.views_count, | |
num_inquiries: room.inquiries.count, | |
num_bookings: room.inquiries.where(state: "completed").count, | |
year_listed: room.created_at.strftime("%Y"), | |
views_to_inquiry_conversion: ((room.inquiries.count.to_f / room.views_count.to_f) * 100).round(2), | |
inquiry_to_booking_conversion: ((room.inquiries.where(state: "completed").count.to_f / room.inquiries.count.to_f) * 100).round(2), | |
views_to_booking_conversion: ((room.inquiries.where(state: "completed").count.to_f / room.views_count.to_f) * 100).round(2) | |
} | |
end | |
end | |
end | |
private | |
def calculate_averages | |
VALUES.each do |attribute| | |
averages["#{attribute}"] = self.send("average_#{attribute}") | |
end | |
end | |
# Only export properties that have >= 4 bookings. The average is however taken on all that have >= 1 bookings. | |
# Take the best 1000 converting properties for a closer look. | |
def export_top_views_to_inquiry_conversion | |
filtered_counts = counts.reject { |h| h[:num_bookings] < 4 } | |
sorted_counts = filtered_counts.sort_by { |h| h[:views_to_inquiry_conversion] }.reverse.take(1000) | |
sorted_counts | |
end | |
# Only export properties that have >= 4 bookings. The average is however taken on all that have >= 1 bookings. | |
# Take the best 1000 converting properties for a closer look. | |
def export_top_inquiry_to_booking_conversion | |
filtered_counts = counts.reject { |h| h[:num_bookings] < 4 } | |
sorted_counts = filtered_counts.sort_by { |h| h[:inquiry_to_booking_conversion] }.reverse.take(1000) | |
sorted_counts | |
end | |
# Defines method to get the average for the `VALUES` defined. | |
VALUES.each do |attribute| | |
define_method("average_#{attribute}") do | |
values = [] | |
counts.each do |room| | |
values << room["#{attribute}".to_sym] if room["#{attribute}".to_sym] > 0 | |
end | |
(values.reduce(:+) / values.size.to_f).round(2) | |
end | |
end | |
def export_top_conversions(sorted_counts, path) | |
CSV.open(path, "w+") do |csv| | |
sorted_counts.each do |room_stats| | |
csv << room_stats.values | |
end | |
end | |
end | |
def export_averages | |
File.open(path_builder("averages", "txt"), 'w+') do |file| | |
averages.each do |key, value| | |
file.write("#{key}: #{value}\n") | |
end | |
end | |
end | |
def path_builder(filename, type) | |
Rails.root.join("tmp", "#{filename}.#{type}") | |
end | |
end | |
a = RoomStats.new; a.run | |
# scp files from the server to local with... | |
# scp -r deploy@ip:/data/roomorama/current/tmp/averages.txt ~/Desktop | |
# scp -r deploy@ip:/data/roomorama/current/tmp/inquiries_to_booking.csv ~/Desktop | |
# scp -r deploy@ip:/data/roomorama/current/tmp/views_to_inquiries.csv ~/Desktop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment