Skip to content

Commit

Permalink
update analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Dec 10, 2024
1 parent cc0bcbd commit 14c89f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
16 changes: 9 additions & 7 deletions app/controllers/analytics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ def fetch_cached_data(key, &block)
end

def fetch_location_data
@user.page_views.group(:country, :state).count.map do |location, count|
{
country: location[0] || 'Unknown', # Fallback to 'Unknown' if no country
state: location[1] || 'Unknown', # Fallback to 'Unknown' if no state
count: count
}
end.sort_by { |location| -location[:count] }.take(10)
@user.page_views
.group(:country)
.count
.map do |country, count|
{
country: country || 'Unknown',
count: count
}
end.sort_by { |location| -location[:count] }.take(10)
end

# Update this method to exclude hidden links
Expand Down
10 changes: 4 additions & 6 deletions app/views/analytics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,22 @@
</div>
</div>

<!-- Top Visitor Locations (Updated Section) -->
<!-- Top Visitor Locations Section -->
<div class="bg-gray-800 rounded-lg shadow p-4 mb-6">
<h2 class="text-xl font-semibold mb-4">Top Visitor Locations</h2>
<div class="overflow-x-auto">
<table class="w-full text-sm text-center table-auto">
<thead class="text-xs uppercase bg-gray-700">
<tr>
<th scope="col" class="px-4 py-3 rounded-tl-lg">State</th>
<th scope="col" class="px-4 py-3">Country</th>
<th scope="col" class="px-4 py-3 rounded-tl-lg">Country</th>
<th scope="col" class="px-4 py-3 rounded-tr-lg">Views</th>
</tr>
</thead>
<tbody>
<% @location_data.each_with_index do |location, index| %>
<% unless location[:state] == 'Unknown' && location[:country] == 'Unknown' %>
<% unless location[:country] == 'Unknown' %>
<tr class="<%= index.even? ? 'bg-gray-800' : 'bg-gray-900' %> border-b border-gray-700">
<td class="px-4 py-3"><%= location[:state].present? && location[:state] != 'Unknown' ? location[:state] : '' %></td>
<td class="px-4 py-3"><%= location[:country].present? && location[:country] != 'Unknown' ? location[:country] : '' %></td>
<td class="px-4 py-3"><%= location[:country].present? ? location[:country] : '' %></td>
<td class="px-4 py-3"><%= number_with_delimiter(location[:count]) %></td>
</tr>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.fixture_paths = ["#{::Rails.root}/spec/fixtures"]
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
Expand Down

0 comments on commit 14c89f0

Please sign in to comment.