From 736140e112b0522216fe9a9171dc6c6bace4d8de Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Fri, 23 Aug 2024 10:49:56 +1000 Subject: [PATCH 1/2] remove unused --- app/views/analytics/index.html.erb | 34 ++---------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/app/views/analytics/index.html.erb b/app/views/analytics/index.html.erb index 58ad3f8..022dfb4 100644 --- a/app/views/analytics/index.html.erb +++ b/app/views/analytics/index.html.erb @@ -99,25 +99,7 @@ -
- -
-

Visitor Locations

- <%= pie_chart @geographic_data, - colors: ["#84CC16", "#22D3EE", "#E879F9", "#F87171", "#A78BFA"], - library: { - backgroundColor: 'transparent', - legend: { position: 'bottom', labels: { color: 'white', fontSize: 12 } }, - title: { display: true, text: 'Visitor Locations', color: 'white', fontSize: 16 }, - responsive: true, - plugins: { datalabels: { color: 'white', font: { weight: 'bold' } } } - }, - donut: true, - prefix: "", - thousands: ",", - round: 2, - height: "300px" %> -
+
@@ -143,7 +125,7 @@
-
+

Browser Usage

@@ -159,16 +141,4 @@ donut: true, height: "250px" %>
- - -
-

Top Sources

-
    - <% @top_referrers.each do |referrer, count| %> -
  • - <%= format_referrer(referrer) %>: <%= number_with_delimiter(count) %> visits -
  • - <% end %> -
-
From 35f6b70df5d9576e00372467ebafb34f2bef655b Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Fri, 23 Aug 2024 14:09:49 +1000 Subject: [PATCH 2/2] enhance --- app/controllers/analytics_controller.rb | 134 +++--------------------- 1 file changed, 15 insertions(+), 119 deletions(-) diff --git a/app/controllers/analytics_controller.rb b/app/controllers/analytics_controller.rb index 40ab376..912687b 100644 --- a/app/controllers/analytics_controller.rb +++ b/app/controllers/analytics_controller.rb @@ -2,46 +2,30 @@ class AnalyticsController < ApplicationController before_action :authenticate_user! def index - @total_page_views = Rails.cache.fetch("#{cache_key_with_version}/total_page_views", expires_in: CACHE_EXPIRATION) do - current_user.page_views.count - end - - @total_link_clicks = Rails.cache.fetch("#{cache_key_with_version}/total_link_clicks", expires_in: CACHE_EXPIRATION) do - current_user.link_clicks.count - end - - @total_achievement_views = Rails.cache.fetch("#{cache_key_with_version}/total_achievement_views", expires_in: CACHE_EXPIRATION) do - current_user.achievement_views.count - end - - @unique_visitors = Rails.cache.fetch("#{cache_key_with_version}/unique_visitors", expires_in: CACHE_EXPIRATION) do - current_user.page_views.select(:ip_address).distinct.count - end - - @latest_daily_metric = Rails.cache.fetch("#{cache_key_with_version}/latest_daily_metric", expires_in: CACHE_EXPIRATION) do - current_user.daily_metrics.order(date: :desc).first - end - - @link_analytics = Rails.cache.fetch("#{cache_key_with_version}/link_analytics", expires_in: CACHE_EXPIRATION) { fetch_link_analytics } - @achievement_analytics = Rails.cache.fetch("#{cache_key_with_version}/achievement_analytics", expires_in: CACHE_EXPIRATION) { fetch_achievement_analytics } - @geographic_data = Rails.cache.fetch("#{cache_key_with_version}/geographic_data", expires_in: CACHE_EXPIRATION) { fetch_geographic_data } - @daily_views = Rails.cache.fetch("#{cache_key_with_version}/daily_views", expires_in: CACHE_EXPIRATION) { fetch_daily_views } - @hourly_distribution = Rails.cache.fetch("#{cache_key_with_version}/hourly_distribution", expires_in: CACHE_EXPIRATION) { fetch_hourly_distribution } - @browser_data = Rails.cache.fetch("#{cache_key_with_version}/browser_data", expires_in: CACHE_EXPIRATION) { fetch_browser_data } - @top_referrers = Rails.cache.fetch("#{cache_key_with_version}/top_referrers", expires_in: CACHE_EXPIRATION) { fetch_top_referrers } + @total_page_views = fetch_cached_data("total_page_views") { current_user.page_views.count } + @total_link_clicks = fetch_cached_data("total_link_clicks") { current_user.link_clicks.count } + @total_achievement_views = fetch_cached_data("total_achievement_views") { current_user.achievement_views.count } + @unique_visitors = fetch_cached_data("unique_visitors") { current_user.page_views.select(:ip_address).distinct.count } + @latest_daily_metric = fetch_cached_data("latest_daily_metric") { current_user.daily_metrics.order(date: :desc).first } + @link_analytics = fetch_cached_data("link_analytics") { fetch_link_analytics } + @achievement_analytics = fetch_cached_data("achievement_analytics") { fetch_achievement_analytics } + @daily_views = fetch_cached_data("daily_views") { fetch_daily_views } + @browser_data = fetch_cached_data("browser_data") { fetch_browser_data } end private + def fetch_cached_data(key, &block) + Rails.cache.fetch("#{cache_key_with_version}/#{key}", expires_in: CACHE_EXPIRATION, &block) + end + def fetch_link_analytics current_user.links.includes(:link_clicks).map do |link| { id: link.id, title: link.title, total_clicks: link.link_clicks.count, - unique_visitors: link.link_clicks.select(:ip_address).distinct.count, - top_referrers: link.link_clicks.group(:referrer).count.sort_by { |_, v| -v }.take(5), - browser_breakdown: link.link_clicks.group(:browser).count + unique_visitors: link.link_clicks.select(:ip_address).distinct.count } end end @@ -57,21 +41,10 @@ def fetch_achievement_analytics end end - def fetch_geographic_data - current_user.page_views.group(:ip_address).count.transform_keys do |ip| - location = Geocoder.search(ip).first - location ? "#{location.city}, #{location.country}" : "Unknown" - end - end - def fetch_daily_views current_user.page_views.group_by_day(:visited_at, range: 30.days.ago..Time.now).count end - def fetch_hourly_distribution - current_user.page_views.group_by_hour_of_day(:visited_at).count - end - def fetch_browser_data current_user.page_views.group(:browser).count.transform_keys do |user_agent| case user_agent @@ -111,84 +84,7 @@ def fetch_browser_data end end - def fetch_top_referrers - referrers = current_user.page_views.group(:referrer).count - - # Normalize referrers to group similar ones together - normalized_referrers = referrers.each_with_object(Hash.new(0)) do |(referrer, count), hash| - normalized_referrer = normalize_referrer(referrer) - hash[normalized_referrer] += count - end - - normalized_referrers.sort_by { |_, v| -v }.take(10) - end - - def normalize_referrer(referrer) - return 'Direct' if referrer.blank? - - uri = URI.parse(referrer) - host = uri.host.downcase - - # Remove 'www.' prefix for consistency - host = host.start_with?('www.') ? host[4..-1] : host - - # Normalize common referrers - case host - when 't.co' - 'Twitter' - when 'facebook.com', 'fb.com' - 'Facebook' - when 'instagram.com' - 'Instagram' - when 'linkedin.com' - 'LinkedIn' - when 'pinterest.com' - 'Pinterest' - when 'youtube.com', 'youtu.be' - 'YouTube' - when 'reddit.com' - 'Reddit' - when 'tumblr.com' - 'Tumblr' - when 'snapchat.com' - 'Snapchat' - when 'whatsapp.com' - 'WhatsApp' - when 'telegram.org' - 'Telegram' - when 'discord.com', 'discordapp.com' - 'Discord' - when 'google.com', 'google.co.uk', 'google.fr', 'google.de', 'google.es', - 'google.it', 'google.ca', 'google.com.au', 'google.co.in', 'google.co.jp' - 'Google' - when 'bing.com' - 'Bing' - when 'yahoo.com', 'yahoo.co.jp' - 'Yahoo' - when 'duckduckgo.com' - 'DuckDuckGo' - when 'baidu.com' - 'Baidu' - when 'yandex.com', 'yandex.ru' - 'Yandex' - when 'amazon.com', 'amazon.co.uk', 'amazon.de', 'amazon.fr', 'amazon.co.jp' - 'Amazon' - when 'ebay.com', 'ebay.co.uk' - 'eBay' - when 'quora.com' - 'Quora' - when 'medium.com' - 'Medium' - when 'wikipedia.org' - 'Wikipedia' - else - host.capitalize # Default to capitalized host if not matched - end - rescue URI::InvalidURIError - 'Unknown' - end - def cache_key_with_version "user_#{current_user.id}_analytics_v1" end -end +end \ No newline at end of file