cohort_me 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/cohort_me.gemspec +17 -0
- data/lib/cohort_me.rb +87 -0
- data/lib/cohort_me/_cohort_table.html.erb +41 -0
- data/lib/cohort_me/version.rb +3 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 nate
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# CohortMe
|
2
|
+
|
3
|
+
Provides tools to Ruby and Rails developers to perform cohort analysis.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'cohort_me'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install cohort_me
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
|
23
|
+
Help's you do a cohort analysis of user retention in a Rails app.
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/cohort_me.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/cohort_me/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["nate"]
|
6
|
+
gem.email = ["nate.kontny@gmail.com"]
|
7
|
+
gem.description = %q{Cohort analysis for a Rails app}
|
8
|
+
gem.summary = %q{Provides tools to Ruby and Rails developers to perform cohort analysis.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "cohort_me"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = CohortMe::VERSION
|
17
|
+
end
|
data/lib/cohort_me.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require "cohort_me/version"
|
2
|
+
|
3
|
+
module CohortMe
|
4
|
+
|
5
|
+
def self.analyze(options={})
|
6
|
+
|
7
|
+
interval_name = options[:period] || "weeks"
|
8
|
+
activation_class = options[:activation_class]
|
9
|
+
activation_table_name = ActiveModel::Naming.plural(activation_class)
|
10
|
+
activation_user_id = options[:activation_user_id] || "user_id"
|
11
|
+
activation_conditions = options[:activation_conditions]
|
12
|
+
|
13
|
+
activity_class = options[:activity_class] || activation_class
|
14
|
+
activity_table_name = ActiveModel::Naming.plural(activity_class)
|
15
|
+
activity_user_id = options[:activity_user_id] || "user_id"
|
16
|
+
|
17
|
+
start_from = nil
|
18
|
+
time_conversion = nil
|
19
|
+
cohort_label = nil
|
20
|
+
|
21
|
+
if interval_name == "weeks"
|
22
|
+
start_from = 12.weeks.ago
|
23
|
+
time_conversion = 604800
|
24
|
+
elsif interval_name == "days"
|
25
|
+
start_from = 12.days.ago
|
26
|
+
time_conversion = 86400
|
27
|
+
cohort_label = "%Y-%j"
|
28
|
+
elsif interval_name == "months"
|
29
|
+
start_from = 12.months.ago
|
30
|
+
time_conversion = 1.month.seconds
|
31
|
+
cohort_label = "%Y-%b"
|
32
|
+
end
|
33
|
+
|
34
|
+
cohort_query = activation_class.select("#{activation_table_name}.#{activation_user_id}, MIN(#{activation_table_name}.created_at) as cohort_date").group("#{activation_user_id}").where("created_at > ?", start_from)
|
35
|
+
|
36
|
+
if activation_conditions
|
37
|
+
cohort_query = cohort_query.where(activation_conditions)
|
38
|
+
end
|
39
|
+
|
40
|
+
if %(mysql mysql2).include?(ActiveRecord::Base.connection.instance_values["config"][:adapter])
|
41
|
+
|
42
|
+
select_sql = "#{activity_table_name}.#{activity_user_id}, #{activity_table_name}.created_at, cohort_date, FLOOR(TIMEDIFF(#{activity_table_name}.created_at, cohort_date)/#{time_conversion}) as periods_out"
|
43
|
+
elsif ActiveRecord::Base.connection.instance_values["config"][:adapter] == "postgresql"
|
44
|
+
select_sql = "#{activity_table_name}.#{activity_user_id}, #{activity_table_name}.created_at, cohort_date, FLOOR(extract(epoch from (#{activity_table_name}.created_at - cohort_date))/#{time_conversion}) as periods_out"
|
45
|
+
else
|
46
|
+
raise "database not supported"
|
47
|
+
end
|
48
|
+
|
49
|
+
data = activity_class.where("created_at > ?", start_from).select(select_sql).joins("JOIN (" + cohort_query.to_sql + ") AS cohorts ON #{activity_table_name}.#{activity_user_id} = cohorts.#{activation_user_id}")
|
50
|
+
|
51
|
+
unique_data = data.all.uniq{|d| [d.user_id, d.cohort_date, d.periods_out] }
|
52
|
+
|
53
|
+
analysis = unique_data.group_by{|d| convert_to_cohort_date(Time.parse(d.cohort_date.to_s), interval_name)}
|
54
|
+
cohort_hash = Hash[analysis.sort_by { |cohort, data| cohort }]
|
55
|
+
|
56
|
+
table = {}
|
57
|
+
cohort_hash.each do |r|
|
58
|
+
|
59
|
+
periods = []
|
60
|
+
table[r[0]] = {}
|
61
|
+
|
62
|
+
cohort_hash.size.times{|i| periods << r[1].count{|d| d.periods_out.to_i == i} if r[1]}
|
63
|
+
|
64
|
+
table[r[0]][:count] = periods
|
65
|
+
table[r[0]][:data] = r[1]
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
return table
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.convert_to_cohort_date(datetime, interval)
|
74
|
+
if interval == "weeks"
|
75
|
+
year_and_week = datetime.strftime("%Y-%U").split("-")
|
76
|
+
return Date.commercial(year_and_week[0].to_i, year_and_week[1].to_i + 1)
|
77
|
+
|
78
|
+
elsif interval == "days"
|
79
|
+
return Date.parse(datetime.strftime("%Y-%m-%d"))
|
80
|
+
|
81
|
+
elsif interval == "months"
|
82
|
+
return Date.parse(datetime.strftime("%Y-%m-1"))
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<table class="table table-bordered">
|
2
|
+
<thead>
|
3
|
+
<tr>
|
4
|
+
<th>
|
5
|
+
|
6
|
+
</th>
|
7
|
+
|
8
|
+
<% @cohorts.size.times do |i| %>
|
9
|
+
<th>
|
10
|
+
<%= i %> <%= @period %>
|
11
|
+
</th>
|
12
|
+
<% end %>
|
13
|
+
|
14
|
+
</tr>
|
15
|
+
</thead>
|
16
|
+
|
17
|
+
<% @cohorts.each_with_index do |row, index| %>
|
18
|
+
|
19
|
+
<% start = row[1][:count][0] %>
|
20
|
+
|
21
|
+
<tr>
|
22
|
+
<td>
|
23
|
+
<%= row[0] %> <br/>
|
24
|
+
<%= start %> users
|
25
|
+
</td>
|
26
|
+
|
27
|
+
<% (@cohorts.size).times do |i| %>
|
28
|
+
<td>
|
29
|
+
<% if @cohorts.size - index > (i + 1) %>
|
30
|
+
<%= "#{((row[1][i].to_f/start.to_f) * 100.00).round(0)}%" %>
|
31
|
+
<% else %>
|
32
|
+
--
|
33
|
+
<% end %>
|
34
|
+
</td>
|
35
|
+
<% end %>
|
36
|
+
</tr>
|
37
|
+
|
38
|
+
<% end %>
|
39
|
+
|
40
|
+
|
41
|
+
</table>
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cohort_me
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- nate
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Cohort analysis for a Rails app
|
15
|
+
email:
|
16
|
+
- nate.kontny@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- cohort_me.gemspec
|
27
|
+
- lib/cohort_me.rb
|
28
|
+
- lib/cohort_me/_cohort_table.html.erb
|
29
|
+
- lib/cohort_me/version.rb
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Provides tools to Ruby and Rails developers to perform cohort analysis.
|
54
|
+
test_files: []
|