retention_magic 0.1beta2 → 0.1beta3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 202a0352d4733122ab78e0761e9ef41d5477d13c
4
- data.tar.gz: bcd0445686ed5e9bb71528a937d3e0fcc2911840
3
+ metadata.gz: 62db181a49bfa473329ed2b6b1b29a8a74245a82
4
+ data.tar.gz: d4ffc7246b4af371d0aeb94c78b5b87e583c0a50
5
5
  SHA512:
6
- metadata.gz: abc384b5ef612dee76e75ec282d5ffd2381ede9612e00bf5d48e215996fa9a201cd7804f760962504bcc416c2e575b9c6086a92a588d410d027dec581e24bed2
7
- data.tar.gz: 80dd27806353b8d83801cd0b666f0e10929935829b88dd3e97d1c1d0d1ad69853339a2baa88f9a4bd2700f8c520a1dca318a4ec390f26148d8a27d9075b7165f
6
+ metadata.gz: 9de9f5e839cabb2ab01dd6d1070eace1fe0e6b77ba234c4c7c5a62f45f5149f80bb6a3e07443f974c555b7ab36376bbed9411be43294c28fb218e5d65e5c36db
7
+ data.tar.gz: fe7f26162a7814ed340ac0f44b6ac072645f52ba1dff147d9de986f9dee4d7a9214f79e72355cf732c0e75097c73ad2471364b75b1a4ad5e022a0cefe3c05d4c
@@ -6,15 +6,15 @@ module RetentionMagic
6
6
  # RetentionMagic configuration setup
7
7
  @user_class = RetentionMagic.user_class.constantize
8
8
 
9
-
10
- first_user = @user_class.order(:created_at).first
11
- first_cohort = @first_user.created_at.beginning_of_month
9
+ @first_user = @user_class.order(:created_at).first
10
+ first_cohort_week = 12.weeks.ago.to_date.beginning_of_week
12
11
  today = Date.today
13
12
 
13
+ difference_in_days_between_now_and_first_cohort = (today.beginning_of_week - first_cohort_week).to_i
14
+
14
15
  # For presenting the widgets
15
- @first_cohort_label = first_cohort.strftime("%h %Y")
16
- @current_cohort_label = today.strftime("%h %Y")
17
- @number_of_months = (today.year * 12 + today.month) - (first_cohort.year * 12 + first_cohort.month)
16
+ @current_cohort_label = today.strftime("%U/%Y")
17
+ @number_of_weeks = difference_in_days_between_now_and_first_cohort / 7
18
18
 
19
19
  @graphs = {
20
20
  activation: {
@@ -28,39 +28,68 @@ module RetentionMagic
28
28
  retention_models = RetentionMagic.retention_models.map { |class_name| class_name.constantize }
29
29
  activation_fields_for_query = RetentionMagic.activation_counter_columns.join(" + ")
30
30
 
31
- 0.upto(@number_of_months - 1) do |month|
32
- cohort_start = @first_user.created_at.beginning_of_month + month.months
33
- cohort_start_end = (cohort_start + 1.month).beginning_of_month
31
+ 0.upto(@number_of_weeks) do |week|
32
+ cohort_start = first_cohort_week + week.weeks
33
+ cohort_start_end = (cohort_start + 1.week).beginning_of_week
34
34
 
35
35
  cohort_users = @user_class.where(created_at: cohort_start..cohort_start_end)
36
+ number_of_cohort_users = cohort_users.count
37
+
38
+ if number_of_cohort_users == 0
39
+ signups_without_activation = 0
40
+ one_activation = 0
41
+ five_activations = 0
42
+ else
43
+ signups_without_activation = cohort_users.where("#{activation_fields_for_query} = 0").count
44
+ one_activation = cohort_users.where("#{activation_fields_for_query} >= 1 AND #{activation_fields_for_query} < 5").count
45
+ five_activations = cohort_users.where("#{activation_fields_for_query} >= 5").count
46
+ end
47
+
48
+ if number_of_cohort_users > 0
49
+ @graphs[:activation][:one_activation][cohort_start] = (one_activation.to_f / number_of_cohort_users.to_f) * 100.0
50
+ @graphs[:activation][:five_activations][cohort_start] = (five_activations.to_f / number_of_cohort_users.to_f) * 100.0
51
+ @graphs[:activation][:signup_without_activation][cohort_start] = (signups_without_activation.to_f / number_of_cohort_users.to_f) * 100.0
52
+ else
53
+ @graphs[:activation][:one_activation][cohort_start] = 0
54
+ @graphs[:activation][:five_activations][cohort_start] = 0
55
+ @graphs[:activation][:signup_without_activation][cohort_start] = 0
56
+ end
36
57
 
37
- @graphs[:activation][:signup_without_activation][cohort_start] = cohort_users.where("#{activation_fields_for_query} = 0").count
38
- @graphs[:activation][:one_activation][cohort_start] = cohort_users.where("#{activation_fields_for_query} = 1").count
39
- @graphs[:activation][:five_activations][cohort_start] = cohort_users.where("#{activation_fields_for_query} >= 5").count
40
58
 
41
- cohort_start_label = cohort_start.strftime("%h %y")
59
+ cohort_start_label = cohort_start.strftime("w%U %Y")
42
60
  @graphs[:retention][cohort_start_label] = {}
43
61
  cohort_user_ids = cohort_users.pluck(:id)
62
+ cohort_size = cohort_users.count
63
+
64
+ difference_in_days_between_now_and_cohort_start = (today.beginning_of_week - cohort_start.to_date).to_i
65
+ number_of_weeks_in_this_cohort = difference_in_days_between_now_and_cohort_start / 7
66
+
67
+ 0.upto(number_of_weeks_in_this_cohort) do |week|
68
+ retention_week = (cohort_start + week.weeks).beginning_of_week
69
+ if week == 0
70
+ @graphs[:retention][cohort_start_label][retention_week] = cohort_size
71
+ else
72
+ retention_week_end = (retention_week + 1.week).beginning_of_week
73
+
74
+ records_per_user = {}
75
+
76
+ retention_models.each do |model|
77
+ count_per_user = model.where(user_id: cohort_user_ids).where(created_at: retention_week..retention_week_end).group("user_id").count
78
+ count_per_user.each do |uid, count|
79
+ records_per_user[uid] ||= 0
80
+ records_per_user[uid] += count
81
+ end
82
+ end
44
83
 
45
- -1.upto((Date.today.year * 12 + Date.today.month) - (cohort_start.year * 12 + cohort_start.month) - 1) do |month|
46
- retention_month = (cohort_start + month.months).beginning_of_month
47
- retention_month_end = (retention_month + 1.month).beginning_of_month
48
-
49
- records_per_user = {}
50
-
51
- retention_models.each do |model|
52
- count_per_user = model.where(user_id: cohort_user_ids).where(created_at: retention_month..retention_month_end).group("user_id").count
53
- count_per_user.each do |uid, count|
54
- records_per_user[uid] ||= 0
55
- records_per_user[uid] += count
84
+ if cohort_user_ids.empty?
85
+ @graphs[:retention][cohort_start_label][retention_week] = 0
86
+ else
87
+ @graphs[:retention][cohort_start_label][retention_week] = records_per_user.keys.size
56
88
  end
57
89
  end
58
-
59
- @graphs[:retention][cohort_start_label][retention_month] = records_per_user.keys.size
60
90
  end
61
91
  end
62
92
 
63
-
64
93
  end
65
94
  end
66
95
  end
@@ -24,15 +24,7 @@
24
24
  <div class="panel panel-default">
25
25
  <div class="panel-heading">Cohorts</div>
26
26
  <div class="panel-body">
27
- <%= @number_of_months %>
28
- </div>
29
- </div>
30
- </div>
31
- <div class="col-xs-6 col-sm-3">
32
- <div class="panel panel-default">
33
- <div class="panel-heading">First Cohort</div>
34
- <div class="panel-body">
35
- <%= @first_cohort %>
27
+ <%= @number_of_weeks %> weeks
36
28
  </div>
37
29
  </div>
38
30
  </div>
@@ -40,7 +32,7 @@
40
32
  <div class="panel panel-default">
41
33
  <div class="panel-heading">Current Cohort</div>
42
34
  <div class="panel-body">
43
- <%= @current_cohort %>
35
+ <%= @current_cohort_label %>
44
36
  </div>
45
37
  </div>
46
38
  </div>
@@ -61,7 +53,7 @@
61
53
  data: @graphs[:activation][:five_activations]
62
54
  },
63
55
  {
64
- name: "One interaction",
56
+ name: "<= 5 interactions",
65
57
  data: @graphs[:activation][:one_activation]
66
58
  },
67
59
  {
@@ -78,7 +70,7 @@
78
70
  <h3 class="panel-title">Retention</h3>
79
71
  </div>
80
72
  <div class="panel-body">
81
- <%= area_chart(@graphs[:retention].map { |k,v| {name: k, data: v}}, library: { legend: { position: "bottom" } } ) %>
73
+ <%= area_chart(@graphs[:retention].map { |k,v| {name: k, data: v}}, stacked: true, library: { bar: { groupWidth: "98%" }, legend: { position: "bottom" } } ) %>
82
74
 
83
75
  </div>
84
76
  </div>
@@ -1,3 +1,3 @@
1
1
  module RetentionMagic
2
- VERSION = "0.1beta2"
2
+ VERSION = "0.1beta3"
3
3
  end
@@ -6,6 +6,7 @@ module RetentionMagic
6
6
  end
7
7
  mattr_accessor :activation_counter_columns do
8
8
  []
9
+ end
9
10
  mattr_accessor :retention_models do
10
11
  []
11
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retention_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1beta2
4
+ version: 0.1beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michiel Sikkes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails