Class: RailsOnboarding::Analytics

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_onboarding/analytics.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =

Default pagination settings to prevent memory issues

1000
MAX_PAGE_SIZE =
10000

Class Method Summary collapse

Class Method Details

.average_completion_time(date_range: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/rails_onboarding/analytics.rb', line 32

def average_completion_time(date_range: nil)
  events = base_events_query(date_range)
  completion_events = events.by_event_type(AnalyticsEvent::ONBOARDING_COMPLETED)

  # properties is serialized JSON stored in a plain text column, so
  # filtering by a key inside it has to happen in Ruby, not SQL -
  # JSON_EXTRACT is a MySQL/SQLite function name; PostgreSQL has no
  # such function (it uses ->/->> on an actual json/jsonb column).
  # Process in batches to prevent memory issues.
  total_time = 0.0
  count = 0

  completion_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    completion_time = event.properties&.dig("completion_time_seconds")
    next if completion_time.nil?

    total_time += completion_time.to_f
    count += 1
  end

  return 0.0 if count.zero?

  (total_time / count).round(2)
end

.average_step_completion_times(date_range: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/rails_onboarding/analytics.rb', line 97

def average_step_completion_times(date_range: nil)
  events = base_events_query(date_range)

  # properties is serialized JSON stored in a plain text column, so
  # filtering by a key inside it has to happen in Ruby, not SQL (see
  # average_completion_time above) - the time_spent > 0 check below
  # already excludes events with no time_spent_seconds recorded.
  step_events = events.by_event_type(AnalyticsEvent::ONBOARDING_STEP_COMPLETED)

  # Process in batches to prevent memory issues
  step_times = Hash.new { |h, k| h[k] = { total: 0.0, count: 0 } }
  step_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    step_name = event.properties&.dig("step_name")
    time_spent = event.properties&.dig("time_spent_seconds").to_f
    if step_name && time_spent > 0
      step_times[step_name][:total] += time_spent
      step_times[step_name][:count] += 1
    end
  end

  step_times.transform_values do |data|
    data[:count] > 0 ? (data[:total] / data[:count]).round(2) : 0.0
  end
end

.daily_summary(date: Date.current) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/models/rails_onboarding/analytics.rb', line 227

def daily_summary(date: Date.current)
  date_range = date.beginning_of_day..date.end_of_day

  {
    date: date,
    onboarding_started: base_events_query(date_range).by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count,
    onboarding_completed: base_events_query(date_range).by_event_type(AnalyticsEvent::ONBOARDING_COMPLETED).count,
    onboarding_skipped: base_events_query(date_range).by_event_type(AnalyticsEvent::ONBOARDING_SKIPPED).count,
    tooltips_shown: base_events_query(date_range).by_event_type(AnalyticsEvent::TOOLTIP_SHOWN).count,
    tooltips_clicked: base_events_query(date_range).by_event_type(AnalyticsEvent::TOOLTIP_CLICKED).count,
    milestones_achieved: base_events_query(date_range).by_event_type(AnalyticsEvent::MILESTONE_ACHIEVED).count
  }
end

.funnel_analysis(date_range: nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/rails_onboarding/analytics.rb', line 191

def funnel_analysis(date_range: nil)
  events = base_events_query(date_range)

  started = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count

  # Build step completion counts in batches
  step_completed_events = events.by_event_type(AnalyticsEvent::ONBOARDING_STEP_COMPLETED)
  step_completion_counts = Hash.new(0)
  step_completed_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    step_name = event.properties&.dig("step_name").to_s
    step_completion_counts[step_name] += 1 if step_name.present?
  end

  steps = RailsOnboarding.configuration.steps.map.with_index do |step, index|
    completed = step_completion_counts[step[:name].to_s] || 0
    retention_rate = started > 0 ? (completed.to_f / started * 100).round(2) : 0.0

    {
      step_name: step[:name],
      step_index: index,
      step_title: step[:title],
      users_reached: completed,
      retention_rate: retention_rate
    }
  end

  completed = events.by_event_type(AnalyticsEvent::ONBOARDING_COMPLETED).count
  overall_completion = started > 0 ? (completed.to_f / started * 100).round(2) : 0.0

  {
    total_started: started,
    overall_completion_rate: overall_completion,
    steps: steps
  }
end

.milestone_achievement_rates(date_range: nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/rails_onboarding/analytics.rb', line 171

def milestone_achievement_rates(date_range: nil)
  events = base_events_query(date_range)

  milestone_events = events.by_event_type(AnalyticsEvent::MILESTONE_ACHIEVED)

  # Process in batches to prevent memory issues
  milestone_counts = Hash.new(0)
  milestone_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    milestone_key = event.properties&.dig("milestone_key")
    milestone_counts[milestone_key] += 1 if milestone_key
  end

  total_users = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count
  return {} if total_users.zero?

  milestone_counts.transform_values do |count|
    (count.to_f / total_users * 100).round(2)
  end
end

.onboarding_completion_rate(date_range: nil) ⇒ Object

Summary methods for analytics reporting



10
11
12
13
14
15
16
17
18
19
# File 'app/models/rails_onboarding/analytics.rb', line 10

def onboarding_completion_rate(date_range: nil)
  events = base_events_query(date_range)

  started_count = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count
  return 0.0 if started_count.zero?

  completed_count = events.by_event_type(AnalyticsEvent::ONBOARDING_COMPLETED).count

  (completed_count.to_f / started_count * 100).round(2)
end

.onboarding_skip_rate(date_range: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'app/models/rails_onboarding/analytics.rb', line 21

def onboarding_skip_rate(date_range: nil)
  events = base_events_query(date_range)

  started_count = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count
  return 0.0 if started_count.zero?

  skipped_count = events.by_event_type(AnalyticsEvent::ONBOARDING_SKIPPED).count

  (skipped_count.to_f / started_count * 100).round(2)
end

.step_completion_rates(date_range: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/rails_onboarding/analytics.rb', line 57

def step_completion_rates(date_range: nil)
  events = base_events_query(date_range)

  step_events = events.by_event_type(AnalyticsEvent::ONBOARDING_STEP_COMPLETED)

  # Process in batches to prevent memory issues
  step_counts = Hash.new(0)
  step_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    step_name = event.properties&.dig("step_name")
    step_counts[step_name] += 1 if step_name
  end

  total_users = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count
  return {} if total_users.zero?

  step_counts.transform_values do |count|
    (count.to_f / total_users * 100).round(2)
  end
end

.step_skip_rates(date_range: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/rails_onboarding/analytics.rb', line 77

def step_skip_rates(date_range: nil)
  events = base_events_query(date_range)

  skip_events = events.by_event_type(AnalyticsEvent::ONBOARDING_STEP_SKIPPED)

  # Process in batches to prevent memory issues
  step_skips = Hash.new(0)
  skip_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    step_name = event.properties&.dig("step_name")
    step_skips[step_name] += 1 if step_name
  end

  total_users = events.by_event_type(AnalyticsEvent::ONBOARDING_STARTED).count
  return {} if total_users.zero?

  step_skips.transform_values do |count|
    (count.to_f / total_users * 100).round(2)
  end
end

.tooltip_engagement_rate(date_range: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/models/rails_onboarding/analytics.rb', line 122

def tooltip_engagement_rate(date_range: nil)
  events = base_events_query(date_range)

  shown_count = events.by_event_type(AnalyticsEvent::TOOLTIP_SHOWN).count
  return 0.0 if shown_count.zero?

  clicked_count = events.by_event_type(AnalyticsEvent::TOOLTIP_CLICKED).count

  (clicked_count.to_f / shown_count * 100).round(2)
end

.tooltip_metrics_by_feature(date_range: nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/rails_onboarding/analytics.rb', line 133

def tooltip_metrics_by_feature(date_range: nil)
  events = base_events_query(date_range)

  tooltip_events = events.where(event_type: [
    AnalyticsEvent::TOOLTIP_SHOWN,
    AnalyticsEvent::TOOLTIP_CLICKED,
    AnalyticsEvent::TOOLTIP_DISMISSED
  ])

  # Process in batches to prevent memory issues
  feature_metrics = Hash.new { |h, k| h[k] = { shown: 0, clicked: 0, dismissed: 0 } }
  tooltip_events.find_each(batch_size: DEFAULT_PAGE_SIZE) do |event|
    feature = event.properties&.dig("tooltip_feature")
    next unless feature

    case event.event_type
    when AnalyticsEvent::TOOLTIP_SHOWN
      feature_metrics[feature][:shown] += 1
    when AnalyticsEvent::TOOLTIP_CLICKED
      feature_metrics[feature][:clicked] += 1
    when AnalyticsEvent::TOOLTIP_DISMISSED
      feature_metrics[feature][:dismissed] += 1
    end
  end

  feature_metrics.map do |feature, metrics|
    engagement_rate = metrics[:shown] > 0 ? (metrics[:clicked].to_f / metrics[:shown] * 100).round(2) : 0.0

    {
      feature: feature,
      shown: metrics[:shown],
      clicked: metrics[:clicked],
      dismissed: metrics[:dismissed],
      engagement_rate: engagement_rate
    }
  end
end