Skip to content

Commit 44a08db

Browse files
author
Job Hammer
committed
Update analytics and settings
1 parent 725e31f commit 44a08db

21 files changed

Lines changed: 599 additions & 335 deletions

app/helpers/ruby_cms/settings_helper.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,33 @@ def render_integer_setting_field(entry:, value:, tab:)
8585

8686
def render_boolean_setting_field(entry:, value:, tab:)
8787
checked = ActiveModel::Type::Boolean.new.cast(value)
88+
input_id = setting_input_id(entry)
89+
8890
hidden = hidden_field_tag("preferences[#{entry.key}]", "false")
8991
checkbox = check_box_tag(
9092
"preferences[#{entry.key}]",
9193
"true",
9294
checked,
93-
id: setting_input_id(entry),
95+
id: input_id,
9496
class: "peer sr-only",
9597
data: autosave_data(entry.key, tab)
9698
)
97-
label = content_tag(
98-
:label,
99-
"",
100-
for: setting_input_id(entry),
101-
class: "relative inline-flex h-7 w-12 cursor-pointer items-center rounded-full " \
102-
"border border-border bg-muted transition-colors peer-checked:bg-primary peer-checked:border-primary"
103-
) do
104-
content_tag(
105-
:span,
106-
"",
107-
class: "inline-block h-5 w-5 transform rounded-full bg-background shadow-sm ring-1 ring-border transition " \
108-
"translate-x-1 peer-checked:translate-x-6"
109-
)
110-
end
11199

112-
content_tag(:div, class: "inline-flex items-center shrink-0") { hidden + checkbox + label }
100+
track = content_tag(:label, "", for: input_id,
101+
class: "relative inline-flex h-6 w-11 shrink-0 cursor-pointer rounded-full " \
102+
"border-2 border-transparent bg-input transition-colors " \
103+
"peer-checked:bg-primary " \
104+
"peer-focus-visible:outline-none peer-focus-visible:ring-2 peer-focus-visible:ring-ring " \
105+
"peer-focus-visible:ring-offset-2 peer-focus-visible:ring-offset-background")
106+
107+
thumb = content_tag(:span, "",
108+
class: "pointer-events-none absolute left-[3px] top-1/2 -translate-y-1/2 h-5 w-5 rounded-full " \
109+
"bg-background shadow-lg ring-0 transition-transform " \
110+
"translate-x-0 peer-checked:translate-x-5")
111+
112+
content_tag(:div, class: "relative inline-flex items-center shrink-0") do
113+
safe_join([hidden, checkbox, track, thumb])
114+
end
113115
end
114116

115117
def render_json_setting_field(entry:, value:, tab:)

app/services/ruby_cms/analytics/report.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ def initialize(start_date:, end_date:, period: nil)
2525

2626
def dashboard_stats
2727
Rails.cache.fetch(cache_key("dashboard"), expires_in: cache_duration) do
28+
total_views = page_view_events.count
29+
total_sessions = visits.distinct.count(:visit_token)
30+
unique_visitors = visits.distinct.count(:visitor_token)
31+
2832
{
29-
total_page_views: page_view_events.count,
30-
unique_visitors: visits.distinct.count(:visitor_token),
31-
total_sessions: visits.distinct.count(:visit_token),
33+
total_page_views: total_views,
34+
unique_visitors: unique_visitors,
35+
total_sessions: total_sessions,
36+
pages_per_session: total_sessions.positive? ? (total_views.to_f / total_sessions).round(1) : 0,
37+
bounce_rate: compute_bounce_rate,
38+
new_visitor_percentage: compute_new_visitor_percentage,
39+
avg_daily_views: days_in_range.positive? ? (total_views.to_f / days_in_range).round(0).to_i : 0,
3240
popular_pages: popular_pages_data,
3341
top_visitors: top_visitors_data,
3442
hourly_activity: hourly_activity_data,
@@ -339,6 +347,32 @@ def extra_cards_data
339347
[]
340348
end
341349

350+
def compute_bounce_rate
351+
total = visits.distinct.count(:visit_token)
352+
return 0 unless total.positive?
353+
354+
event_counts = page_view_events.group(:visit_id).count
355+
single_page = event_counts.count { |_, c| c == 1 }
356+
((single_page.to_f / total) * 100).round(1)
357+
rescue StandardError
358+
0
359+
end
360+
361+
def compute_new_visitor_percentage
362+
total = visits.distinct.count(:visitor_token)
363+
return 0 unless total.positive?
364+
365+
returning_tokens = Ahoy::Visit
366+
.where("started_at < ?", @start_date)
367+
.distinct
368+
.pluck(:visitor_token)
369+
370+
new_count = visits.where.not(visitor_token: returning_tokens).distinct.count(:visitor_token)
371+
((new_count.to_f / total) * 100).round(0).to_i
372+
rescue StandardError
373+
0
374+
end
375+
342376
def days_in_range
343377
(@end_date.to_date - @start_date.to_date + 1).to_i
344378
end

0 commit comments

Comments
 (0)