Aggiunge heatmaps first-party (click/scroll) con pagina admin Analytics.
Raccoglie eventi aggregati dietro consenso cookie, senza PII né tracking su admin/regia/live/replay; GA4 resta invariato. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Admin
|
||||
class AnalyticsController < Admin::BaseController
|
||||
def index
|
||||
@filters = {
|
||||
from: parse_date(params[:from]) || 7.days.ago.to_date,
|
||||
to: parse_date(params[:to]) || Time.zone.today,
|
||||
device: params[:device].presence
|
||||
}
|
||||
|
||||
scope = AnalyticsPageStat.where(day: @filters[:from]..@filters[:to])
|
||||
scope = scope.where(device: @filters[:device]) if @filters[:device].present? && AnalyticsEvent::DEVICES.include?(@filters[:device])
|
||||
|
||||
click_scope = AnalyticsPageCell.where(day: @filters[:from]..@filters[:to])
|
||||
click_scope = click_scope.where(device: @filters[:device]) if @filters[:device].present? && AnalyticsEvent::DEVICES.include?(@filters[:device])
|
||||
|
||||
clicks_by_path = click_scope.group(:page_path).sum(:click_count)
|
||||
pageviews_by_path = scope.group(:page_path).sum(:pageview_count)
|
||||
scroll_samples_by_path = scope.group(:page_path).sum(:scroll_samples)
|
||||
scroll_sum_by_path = scope.group(:page_path).sum(:scroll_sum_pct)
|
||||
max_scroll_by_path = scope.group(:page_path).maximum(:max_scroll_pct)
|
||||
|
||||
paths = (pageviews_by_path.keys + clicks_by_path.keys).uniq
|
||||
@pages = paths.map do |path|
|
||||
samples = scroll_samples_by_path[path].to_i
|
||||
{
|
||||
page_path: path,
|
||||
pageviews: pageviews_by_path[path].to_i,
|
||||
clicks: clicks_by_path[path].to_i,
|
||||
scroll_samples: samples,
|
||||
scroll_sum: scroll_sum_by_path[path].to_i,
|
||||
max_scroll: max_scroll_by_path[path].to_i
|
||||
}
|
||||
end.sort_by { |r| [-r[:pageviews], -r[:clicks], r[:page_path]] }
|
||||
end
|
||||
|
||||
def show
|
||||
@page_path = params[:page_path].to_s
|
||||
redirect_to admin_analytics_path, alert: t("admin.analytics.missing_path") and return if @page_path.blank?
|
||||
|
||||
@filters = {
|
||||
from: parse_date(params[:from]) || 7.days.ago.to_date,
|
||||
to: parse_date(params[:to]) || Time.zone.today,
|
||||
device: params[:device].presence
|
||||
}
|
||||
|
||||
cells = AnalyticsPageCell.where(page_path: @page_path, day: @filters[:from]..@filters[:to])
|
||||
cells = cells.where(device: @filters[:device]) if @filters[:device].present? && AnalyticsEvent::DEVICES.include?(@filters[:device])
|
||||
@cells = cells.group(:cell_x, :cell_y).sum(:click_count)
|
||||
@max_clicks = @cells.values.max.to_i
|
||||
|
||||
stats = AnalyticsPageStat.where(page_path: @page_path, day: @filters[:from]..@filters[:to])
|
||||
stats = stats.where(device: @filters[:device]) if @filters[:device].present? && AnalyticsEvent::DEVICES.include?(@filters[:device])
|
||||
@pageviews = stats.sum(:pageview_count)
|
||||
@scroll_samples = stats.sum(:scroll_samples)
|
||||
@scroll_sum = stats.sum(:scroll_sum_pct)
|
||||
@max_scroll = stats.maximum(:max_scroll_pct).to_i
|
||||
@avg_scroll = @scroll_samples.positive? ? (@scroll_sum.to_f / @scroll_samples).round : 0
|
||||
@grid = AnalyticsPageCell::GRID_SIZE
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_date(value)
|
||||
return nil if value.blank?
|
||||
|
||||
Date.parse(value.to_s)
|
||||
rescue ArgumentError, TypeError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user