|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SiteAdmin |
| 4 | + # SEO Audit Dashboard Controller |
| 5 | + # Provides an overview of SEO health across properties and pages |
| 6 | + # |
| 7 | + # Features: |
| 8 | + # - Summary statistics for SEO completeness |
| 9 | + # - Lists of items missing SEO data |
| 10 | + # - Quick links to fix issues |
| 11 | + class SeoAuditController < SiteAdminController |
| 12 | + def index |
| 13 | + # Get all properties for this website |
| 14 | + @properties = Pwb::ListedProperty |
| 15 | + .where(website_id: current_website&.id) |
| 16 | + .order(created_at: :desc) |
| 17 | + |
| 18 | + # Get all pages for this website |
| 19 | + @pages = Pwb::Page.where(website_id: current_website&.id).order(:slug) |
| 20 | + |
| 21 | + # Calculate statistics |
| 22 | + calculate_property_stats |
| 23 | + calculate_page_stats |
| 24 | + calculate_image_stats |
| 25 | + |
| 26 | + # Overall SEO score |
| 27 | + calculate_overall_score |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def calculate_property_stats |
| 33 | + total = @properties.count |
| 34 | + |
| 35 | + # Count properties with SEO fields |
| 36 | + # We need to check through listings (sale_listing or rental_listing) |
| 37 | + properties_with_seo_title = 0 |
| 38 | + properties_with_meta_desc = 0 |
| 39 | + |
| 40 | + @properties.each do |prop| |
| 41 | + has_seo_title = prop.respond_to?(:seo_title) && prop.seo_title.present? |
| 42 | + has_meta_desc = prop.respond_to?(:meta_description) && prop.meta_description.present? |
| 43 | + |
| 44 | + properties_with_seo_title += 1 if has_seo_title || prop.title.present? |
| 45 | + properties_with_meta_desc += 1 if has_meta_desc || prop.description.present? |
| 46 | + end |
| 47 | + |
| 48 | + @property_stats = { |
| 49 | + total: total, |
| 50 | + with_title: properties_with_seo_title, |
| 51 | + with_description: properties_with_meta_desc, |
| 52 | + missing_title: total - properties_with_seo_title, |
| 53 | + missing_description: total - properties_with_meta_desc, |
| 54 | + title_percentage: total.positive? ? (properties_with_seo_title * 100.0 / total).round : 0, |
| 55 | + description_percentage: total.positive? ? (properties_with_meta_desc * 100.0 / total).round : 0 |
| 56 | + } |
| 57 | + |
| 58 | + # Get list of properties missing SEO (limited for display) |
| 59 | + @properties_missing_seo = @properties.select do |p| |
| 60 | + title_missing = p.title.blank? && (!p.respond_to?(:seo_title) || p.seo_title.blank?) |
| 61 | + desc_missing = p.description.blank? && (!p.respond_to?(:meta_description) || p.meta_description.blank?) |
| 62 | + title_missing || desc_missing |
| 63 | + end.first(10) |
| 64 | + end |
| 65 | + |
| 66 | + def calculate_page_stats |
| 67 | + total = @pages.count |
| 68 | + with_seo_title = @pages.count { |p| p.seo_title.present? || p.page_title.present? } |
| 69 | + with_meta_desc = @pages.count { |p| p.meta_description.present? } |
| 70 | + |
| 71 | + @page_stats = { |
| 72 | + total: total, |
| 73 | + with_title: with_seo_title, |
| 74 | + with_description: with_meta_desc, |
| 75 | + missing_title: total - with_seo_title, |
| 76 | + missing_description: total - with_meta_desc, |
| 77 | + title_percentage: total.positive? ? (with_seo_title * 100.0 / total).round : 0, |
| 78 | + description_percentage: total.positive? ? (with_meta_desc * 100.0 / total).round : 0 |
| 79 | + } |
| 80 | + |
| 81 | + # Pages missing SEO |
| 82 | + @pages_missing_seo = @pages.select do |p| |
| 83 | + (p.seo_title.blank? && p.page_title.blank?) || p.meta_description.blank? |
| 84 | + end.first(10) |
| 85 | + end |
| 86 | + |
| 87 | + def calculate_image_stats |
| 88 | + # Get all prop photos for this website's properties |
| 89 | + property_ids = @properties.pluck(:id) |
| 90 | + |
| 91 | + # Count photos via RealtyAsset association |
| 92 | + total_photos = Pwb::PropPhoto |
| 93 | + .joins("INNER JOIN pwb_realty_assets ON pwb_prop_photos.realty_asset_id = pwb_realty_assets.id") |
| 94 | + .where(pwb_realty_assets: { website_id: current_website&.id }) |
| 95 | + .count |
| 96 | + |
| 97 | + photos_with_alt = Pwb::PropPhoto |
| 98 | + .joins("INNER JOIN pwb_realty_assets ON pwb_prop_photos.realty_asset_id = pwb_realty_assets.id") |
| 99 | + .where(pwb_realty_assets: { website_id: current_website&.id }) |
| 100 | + .where.not(description: [nil, '']) |
| 101 | + .count |
| 102 | + |
| 103 | + @image_stats = { |
| 104 | + total: total_photos, |
| 105 | + with_alt: photos_with_alt, |
| 106 | + missing_alt: total_photos - photos_with_alt, |
| 107 | + alt_percentage: total_photos.positive? ? (photos_with_alt * 100.0 / total_photos).round : 0 |
| 108 | + } |
| 109 | + |
| 110 | + # Properties with photos missing alt text (limited) |
| 111 | + @properties_with_missing_alt = Pwb::RealtyAsset |
| 112 | + .where(website_id: current_website&.id) |
| 113 | + .joins(:prop_photos) |
| 114 | + .where(pwb_prop_photos: { description: [nil, ''] }) |
| 115 | + .distinct |
| 116 | + .limit(10) |
| 117 | + end |
| 118 | + |
| 119 | + def calculate_overall_score |
| 120 | + # Weight: Properties 40%, Pages 30%, Images 30% |
| 121 | + property_score = (@property_stats[:title_percentage] + @property_stats[:description_percentage]) / 2.0 |
| 122 | + page_score = (@page_stats[:title_percentage] + @page_stats[:description_percentage]) / 2.0 |
| 123 | + image_score = @image_stats[:alt_percentage] |
| 124 | + |
| 125 | + @overall_score = (property_score * 0.4 + page_score * 0.3 + image_score * 0.3).round |
| 126 | + |
| 127 | + @score_grade = case @overall_score |
| 128 | + when 90..100 then { letter: 'A', color: 'green', message: 'Excellent SEO health!' } |
| 129 | + when 75..89 then { letter: 'B', color: 'blue', message: 'Good, but room for improvement' } |
| 130 | + when 60..74 then { letter: 'C', color: 'yellow', message: 'Needs attention' } |
| 131 | + when 40..59 then { letter: 'D', color: 'orange', message: 'Significant improvements needed' } |
| 132 | + else { letter: 'F', color: 'red', message: 'Critical SEO issues' } |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments